嗨,我有一个FriendsViewController,我显示从coreData获取的朋友记录。我有另一个视图控制器AddFriendViewController这是由FriendsViewController添加一个新的朋友,它保存在其中的上下文。我正在收听FriendsViewController中共享MOC的更改通知。多次调用NSManagedObjectContextDidSaveNotification
[[NSNotificationCenter defaultCenter]
addObserverForName:NSManagedObjectContextDidSaveNotification
object:appdelegate.context queue:nil
usingBlock:^(NSNotification * _Nonnull note) {
NSLog(@"Re-Fetch Whole Friends Array from core data and Sort it with UILocalizedIndexedCollation and reloadData into table");
}];
在AddFriendsViewController只需要创建一个朋友的对象,我
Friend *friend= [NSEntityDescription
insertNewObjectForEntityForName:@"Friend"
inManagedObjectContext:appdelegate.context];
friend.name=nameTextfield.text;
[appdelegate.context save:&error];
[self.navigationController popViewControllerAnimated:YES];
现在,当我执行节省从AddFriendViewController上下文中FriendsViewController上述块被触发几次,而不是一个时间这会导致更多的处理,因为从核心数据中重新获取整个数据。我无法使用Fetched Results Controller,因为我使用UILocalizedIndexedCollation将我的数组排序。所以我的问题是为什么它被称为两次或有时甚至三次?或者有没有其他办法呢?
你有没有机会使用多个托管对象上下文,并且它们之间有父/子关系? –
我已经想通了,我必须删除这个观察者,我在didLoad中添加了这个观察者,但不知道在哪里删除它? PS。我在这个视图控制器后面有一个视图控制器,所以每当我回到这个视图时再添加一个观察者。 –