No object found in coredata after adding to relationship

I have a problem retrieving an object after adding to a relationship. The first time I select a category, I always found, then when I added to this connection, the following categories were not found.

Attitude.

Example:

  • Category selection with categoryId = 10
  • Search Category Object
  • Added to parent entities
  • Next object
  • If several categories have the same id , categoryId = 10 , not found

     NSManagedObjectContext *private = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType]; [private setParentContext:self.model.context]; __block NSError *error = nil; [private performBlockAndWait:^{ GPDeal *deal = [EKManagedObjectMapper objectFromExternalRepresentation:dic withMapping:[GPDeal objectMapping] inManagedObjectContext:private]; for (NSDictionary *dic in responseObject[@"response"]) { GPCategory *category; //The first time always found if ((category = [GPCategory MR_findFirstByAttribute:@"catId" withValue:dic[@"mainAttribute"] inContext:private])) { NSLog(@"Found"); [category addDealsObject:deal]; } else { NSLog(@"Not Found"); } } }]; NSError *PrivateError = nil; if (![private save:&PrivateError]) { NSLog(@"Unresolved error %@, %@", PrivateError, [PrivateError userInfo]); abort(); } if (!error) { //Save on main moc [self.model saveWithErrorBlock:^(BOOL success, NSError *error) { if (!success) { NSLog(@"Error saving context: %@\n%@", [error localizedDescription], [error userInfo]); } }]; } else { NSLog(@"Error saving context: %@\n%@", [error localizedDescription], [error userInfo]); } 

EDIT:

Solved, I think, my problem is that I forgot to save the main context at the end of each iteration.

  NSManagedObjectContext *backgroundMOC = [self.model backgroundMOC:self.model.context]; [backgroundMOC performBlockAndWait:^{ for (NSDictionary *dic in responseObject[@"response"]) { GPDeal *deal = [EKManagedObjectMapper objectFromExternalRepresentation:dic withMapping:[GPDeal objectMapping] inManagedObjectContext:backgroundMOC]; GPCategory *category; if ((category = [GPCategory MR_findFirstByAttribute:@"catId" withValue:dic[@"mainAttribute"] inContext:backgroundMOC])) { NSLog(@"Found with mainAttribute %@", dic[@"mainAttribute"]); [deal addDealCategoryObject:category]; } if([backgroundMOC hasChanges]) { NSError * error; [backgroundMOC save:&error]; [self.model.context performBlockAndWait:^{ if([self.model.context hasChanges]) { NSError * error; [self.model.context save:&error]; } }]; } } }]; 
+6
source share
1 answer

You may miss the savings on the MOC chain. For clarity, I replaced the private keyword with the variable name backgroundMOC .

In the above question, I can only assume that the line NSManagedObjectContext *private = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType]; technically similar:

 - (NSManagedObjectContext *)backgroundMOC:(NSManagedObjectContext *)mainMOC { NSManagedObjectContext * threadManagedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType]; [threadManagedObjectContext setParentContext:mainMOC]; [threadManagedObjectContext setMergePolicy:NSMergeByPropertyObjectTrumpMergePolicy]; return threadManagedObjectContext; } 

passes self.model.context as mainMOC, with self.model.context and a setMergePolicy .
Similarly, I have to assume that self.model saveWithErrorBlock technically identical:

 [mainMOC performBlockAndWait:^{ if([mainMOC hasChanges]) { NSError * error; [mainMOC save:&error]; // handle error } }]; 

If yes, then the same should be said about backgroundMOC (your link is private ):

 [backgroundMOC performBlockAndWait:^{ if([backgroundMOC hasChanges]) { NSError * error; [backgroundMOC save:&error]; // handle error } }]; 

In other words, you want your backgroundMOC and mainMOC save operations to mainMOC performed from their respective threads using performBlockAndWait .

+1
source

All Articles