2

I have 2 tables A and B. A ->> B (AToBRelation, One to Many relationship) A <- B (BToARelation, One to One relationship)

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.

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

    - (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:&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:&error]) {
        NSLog(@"Failed to save in core data : %@", error.localizedDescription);
        return nil;
    }

    jobObject.latestMessageCreatedAt = jobEntity.latestMessageCreatedAt;
    return jobObject;

}

Core Data model

  • post SQL Debug output `-com.apple.CoreData.SQLDebug1` – Khundragpan Feb 24 '16 at 12:47
  • @Khundragpan please have a look. – Swappy Dwappy Feb 24 '16 at 14:32
  • You need to post your saving method and also error logs. Try CoreData debug from Product>Scheme>EditScheme>Argument Passed On Launch - `-com.apple.CoreData.SQLDebug1`. Also this similar question might help you [Question link](http://stackoverflow.com/questions/16498875/context-does-not-save-the-changes-with-error-1550) – Khundragpan Feb 24 '16 at 14:53
  • @Khundragpan olease have a look. Link to error logs http://pastebin.com/imKk7xjg – Swappy Dwappy Feb 24 '16 at 16:52
  • There is a temporary id "t" in `coredata:///RNewMessageEntity/t506462E6-91DC-4EDE-AEAA-9812288999336>`. Also note that `jobRelationship = nil;`. Is that an inverse relationship. Could you post your model. – Khundragpan Feb 25 '16 at 06:46
  • I am not sure about this but you have not mentioned `jobEntity = [NSEntityDescription insertNewObjectForEntityForName:@"RJobEntity"...` – Khundragpan Feb 25 '16 at 07:36
  • @Khundragpan..Have posted the model plus edited the question as per your query. However the jobEntity = [NSEntityDescription insertNewObjectForEntityForName:@"RJobEntity" is called all together in a different function which runs fine as I have mentioned at the start of my question.And you got it right that jobRelationship = nil.This is primarily my question why is it happening only in some cases. – Swappy Dwappy Feb 25 '16 at 08:30
  • Errors like these can occur due to different threads, or different context on same thread. Each context maintains its own cache. When a fetch is performed, it will receive the cache from closest ancestor. Are you using Apple's default Core Data template or your's own. – Khundragpan Feb 25 '16 at 10:03

0 Answers0