I'm having trouble debugging a NSFetchedResultsControllerDelegate
behavior.
My NSFetchedResultsController
has a fetch request concerning a Object1
entity. Whenever a cell is touch, I create (and save) a new relationship to a Object2
entity (or use if it exists already).
My problem is that controller:didChangeObject:atIndexPath:forChangeType:newIndexPath:
gets called randomly when such a relationship is created (meaning, sometimes it gets called, sometimes it doesn't).
My code creation the relationship is using MagicalRecord :
+ (void)createOrUpdateRelationship:(NSDictionary *)userInfo
forObject1:(NSManagedObject1 *)object1 {
[MagicalRecord saveWithBlock:^(NSManagedObjectContext *localContext) {
NSManagedObject2 *object2 = [NSManagedObject2 fetchObject2ForObject1WithID:object1.ID
inContext:localContext];
if (!object2) {
object2 = [NSManagedObject2 MR_createEntityInContext:localContext];
object2.createdAt = [NSDate date];
}
object2.updatedAt = [NSDate date];
// Store attributes from userInfo
object2.object1 = [object1 MR_inContext:localContext];
} completion:^(BOOL contextDidSave, NSError *error) {
// Celebrate
}];
}
Any idea what I could do wrong?
Dirty Henry,
Object1 is having a one to one relationship with Object2 and Object2 has inverse relationship with Object1 as well :)
So whenever you change the relationship value of any of these objects other object will also be modified :) Hence NSFetchedResultsController gets triggered :) Thats quite common :)
You cant skip these changes from being triggered.
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath {
switch(type) {
case NSFetchedResultsChangeUpdate:
//you might consider ignoring it but I wont prefer that though :)
break;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.