Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CoreData fails to save context, saying relationship is nil - however it is definitely set

When I attempt to save the context of my CoreData model, I receive an error

Unresolved error Error Domain=NSCocoaErrorDomain Code=1560 "The operation couldn’t be completed. (Cocoa error 1560.)" UserInfo=0x76924b0 {NSDetailedErrors=( "Error Domain=NSCocoaErrorDomain Code=1570 " The operation couldn\U2019t be completed. (Cocoa error 1570.)" UserInfo=0x768c410 {NSValidationErrorObject=<Posts: 0x7177990> (entity: Posts; id: 0x7177010 <x-coredata:///Posts/tEF7138E6-A968-45B7-95CF-116C2AA93D605> ; data: {\n " business_entity" = nil;\n "
created_at" = " 2012-01-21 03:31:30 +0000" ;\n " own_description" = " Roasted this morning- Panama Duncan Estate: A rose fragrance debuts flavors of pear, coconut and marshmallow with caramel throughout the cup." ;\n postsID = 4;\n " updated_at" = " 2012-01-21 03:31:30 +0000" ;\n}), NSValidationErrorKey=business_entity, NSLocalizedDescription=The operation couldn\U2019t be completed. (Cocoa error 1570.)}",

Key part being:

business_entity = nil;
NSValidationErrorKey=business_entity

However when I create my "Posts" object I set them such that:

    // First try to find a business for the post
NSNumber* businessID = [NSNumber numberWithInteger: [(NSString*)[jsonInfo objectForKey:@"business_id"] integerValue]];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setEntity: [NSEntityDescription entityForName:kCoreDataModelNameBusinesses inManagedObjectContext: self.managedObjectContext]];
[fetchRequest setPredicate: [NSPredicate predicateWithFormat:@"(businessesID == %i)", [businessID intValue]]];

NSError *error = nil;
NSArray *matchingManagedObjects = [__managedObjectContext executeFetchRequest:fetchRequest error:&error];

if(error) {
    NSLog(@"Error-createNewPostsWithJsonInfo: Unresolved error %@, %@", error, [error userInfo] );
    return;
}

// Check any results
if( [matchingManagedObjects count] == 0) {
    NSLog(@"Error-createNewPostsWithJsonInfo: Could not locate a matching businessId: %@ for postId:%@", businessID, (NSString*)[jsonInfo objectForKey:@"id"]);
}
// Check too many results    
if( [matchingManagedObjects count] > 1) {
    NSLog(@"Error-createNewPostsWithJsonInfo: Too many bussinesses match businessId: %@ for postId:%@", businessID, (NSString*)[jsonInfo objectForKey:@"id"]);
}

// Final sanity check outputs fine!!
NSLog(@"Business entity %@", (Businesses*)[matchingManagedObjects objectAtIndex:0] );

// Finally create the post 
Posts* newPosts = [NSEntityDescription
                           insertNewObjectForEntityForName:entityName inManagedObjectContext:self.managedObjectContext];

newPosts.created_at = [_dateFormater dateFromString: [jsonInfo objectForKey:@"created_at"] ];
newPosts.updated_at = [_dateFormater dateFromString: [jsonInfo objectForKey:@"updated_at"] ];
newPosts.own_description = [jsonInfo objectForKey:@"description"];
newPosts.postsID = [NSNumber numberWithInteger: [(NSString*)[jsonInfo objectForKey:@"id"] integerValue]];
[newPosts setBusiness_entity: (Businesses*)[matchingManagedObjects objectAtIndex:0] ];

So I know the business_entity is being set, and is correct. What would cause it to give an error when saving the context in such a scenario?

Here's the model if that helps:

enter image description hereenter image description here

like image 367
Onedayitwillmake Avatar asked Jan 24 '12 20:01

Onedayitwillmake


1 Answers

Incase anyone comes across this problem, the error was in the way that I was setting the relationship in coredatamodel.

These images will explain better than i can, but make sure you set the relationships as to-many in the owner entity, otherwise it won't create the mutable set dynamically for you

enter image description hereenter image description here

like image 105
Onedayitwillmake Avatar answered Sep 29 '22 04:09

Onedayitwillmake