Core data one to many relationship getting set to nil
<p>I have 2 tables A and B.
A ->> B (AToBRelation, One to Many relationship)
A &lt;- B  (BToARelation, One to One relationship)</p>

<p>I try saving a row in A,which goes fine.
Then I try to save a row in B for which i make a fetch of A's entity and a then do addAToBRelationRelationshipObject(CoreDataGeneratedAccessors).This also goes fine.However when i try doing this very fast in a for loop for some 30-40 B's entity(each getting saved one by one as I told above), I get a core data error mainy 1550 error code .Error Description shows relation being nil.</p>

<p>Console Log:
        Unresolved error Error Domain=NSCocoaErrorDomain Code=1550 "The operation couldn’t be completed. (Cocoa error 1550.)" UserInfo={Dangling reference to an invalid object.=null, NSValidationErrorValue=Relationship 'messageRelationship' on managed object </p>

<pre><code>    - (RJobObject *)saveMessageToDB:(RMessageObject *)msgObject {

    NSManagedObjectContext *context = [self managedObjectContext];

    RJobEntity * jobEntity = [self getJobEntityWithId:msgObject.jobId];
    if (jobEntity == nil) {
        //this portion cannot be reached
        return nil;
    }


    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"RNewMessageEntity" inManagedObjectContext:context];
    [fetchRequest setEntity:entity];
    [fetchRequest setPredicate:[NSPredicate predicateWithFormat:@"id == %@", msgObject.id]];
    NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:nil];

    if (fetchedObjects.count == 0) {

        RKObjectMapping * msgMapping = [RMessageObject mapping];
        RKRequestDescriptor *requestDescriptor2 = [RKRequestDescriptor requestDescriptorWithMapping:msgMapping.inverseMapping objectClass:[RMessageObject class] rootKeyPath:nil method:RKRequestMethodPOST];
        NSDictionary *dict = [RKObjectParameterization parametersWithObject:msgObject requestDescriptor:requestDescriptor2 error:nil];

        RNewMessageEntity * msgEntity  = [NSEntityDescription insertNewObjectForEntityForName:@"RNewMessageEntity" inManagedObjectContext:context];
        msgEntity.dictionary = dict;
        msgEntity.id = msgObject.id;
        msgEntity.createdAt = [NSNumber numberWithLongLong:msgObject.createdAt];

        jobEntity.latestMessageCreatedAt = [NSNumber numberWithLongLong:msgObject.createdAt];
        [jobEntity addMessageRelationshipObject:msgEntity];
        if (jobEntity.messageRelationship == nil) {
             NSLog(@"swappy");
        }

        NSError *error;
        if (![context save:&amp;error]) {
            NSLog(@"Failed to save in core data : %@", error.localizedDescription);
            return nil;
        }

        RJobObject *jobObject = [self getJobObjectFromEntity:jobEntity];
        return jobObject;
    } 

    return nil;
}

- (RJobObject *)saveJobToDB:(RJobObject *)jobObject {

    NSManagedObjectContext *context = [self managedObjectContext];

    RJobEntity * jobEntity = [self getJobEntityWithId:jobObject.id];

    if (!jobEntity) {
        jobEntity = [NSEntityDescription insertNewObjectForEntityForName:@"RJobEntity" inManagedObjectContext:context];
    }

    RKObjectMapping * itemMapping = [RJobObject mapping];
    RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:itemMapping.inverseMapping objectClass:[RJobObject class] rootKeyPath:nil method:RKRequestMethodPOST];

    NSMutableDictionary *dict = (NSMutableDictionary *)[RKObjectParameterization parametersWithObject:jobObject requestDescriptor:requestDescriptor error:nil];

    jobEntity.pUserJobsDictionary = dict;
    jobEntity.id = jobObject.id;

    NSError *error;
    if (![context save:&amp;error]) {
        NSLog(@"Failed to save in core data : %@", error.localizedDescription);
        return nil;
    }

    jobObject.latestMessageCreatedAt = jobEntity.latestMessageCreatedAt;
    return jobObject;

}
</code></pre>

<p><a href="https://i.stack.imgur.com/i1J6n.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/i1J6n.png" alt="Core Data model"></a></p>