如果一个页面拥有一个 NSFetchedResultsController 的引用,那么在这个界面将要结束时,一定要 remove 它的 observer
- (void)dealloc { //... [[NSNotificationCenter defaultCenter] removeObserver:self.fetchedResultsController]; _fetchedResultsController.delegate = nil; [_fetchedResultsController release]; [super dealloc]; }
为什么呢?
看下崩溃日志(友盟统计捕获的崩溃日志)
2014-03-26 17:39:17.128 XiaoJiZhuoMi[13394:90b] Umeng: -[UIButton controllerWillChangeContent:]: unrecognized selector sent to instance 0xce92db0
(null)
(
0 CoreFoundation 0x01daa1e4 __exceptionPreprocess + 180
1 libobjc.A.dylib 0x041fb8e5 objc_exception_throw + 44
2 CoreFoundation 0x01e47243 -[NSObject(NSObject) doesNotRecognizeSelector:] + 275
3 CoreFoundation 0x01d9a50b ___forwarding___ + 1019
4 CoreFoundation 0x01d9a0ee _CF_forwarding_prep_0 + 14
5 CoreData 0x03fda640 -[NSFetchedResultsController(PrivateMethods) _managedObjectContextDidChange:] + 2080
6 Foundation 0x0395b049 __57-[NSNotificationCenter addObserver:selector:name:object:]_block_invoke + 40
7 CoreFoundation 0x01e05f04 __CFNOTIFICATIONCENTER_IS_CALLING_OUT_TO_AN_OBSERVER__ + 20
8 CoreFoundation 0x01d5defb _CFXNotificationPost + 2859
9 Foundation 0x03894e41 -[NSNotificationCenter postNotificationName:object:userInfo:] + 98
10 CoreData 0x03edda13 -[NSManagedObjectContext(_NSInternalNotificationHandling) _postObjectsDidChangeNotificationWithUserInfo:] + 83
11 CoreData 0x03f7cfaf -[NSManagedObjectContext(_NSInternalChangeProcessing) _createAndPostChangeNotification:withDeletions:withUpdates:withRefreshes:] + 367
12 CoreData 0x03ed8eb8 -[NSManagedObjectContext(_NSInternalChangeProcessing) _processRecentChanges:] + 2152
13 CoreData 0x03edc2fc -[NSManagedObjectContext save:] + 140
...
当 [NSManagedObjectContext save:] 后,会发送一个通知,这时与之绑定的 NSFetchedResultsController,拿到通知后,负责给它的代理更新页面(或其他)。而如果持有 NSFetchedResultsController 的页面已经被销毁,及 NSFetchedResultsController 已经被 release,那么它收到通知会怎么办?只有崩溃(如崩溃日志第一行所示)。