爲了防止「突然施加停止」的問題,您可以實現類似的東西的方法:
- (void)saveContext {
NSError *error = nil;
NSManagedObjectContext *managedObjectContext = self.managedObjectContext;
if (managedObjectContext != nil) {
if ([managedObjectContext hasChanges] && ![managedObjectContext save:&error]) {
/*
Replace this implementation with code to handle the error appropriately.
abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button.
*/
LogError(@"Unresolved error %@, %@", error, [error userInfo]);
// abort();
}
}
}
,並用它內部的應用程序委託的方法有兩種:
- (void)applicationWillTerminate:(UIApplication *)application;
和
- (void)applicationDidEnterBackground:(UIApplication *)application;
認爲它可能不是100%的解決方案,但在大多數情況下,它將完成這項工作......
是的,關於突然停止/暫停的觀點正是我所擔心的。無論如何,我會同意每個文件最多保存一次就足夠了,將所有屬性分組在單個「運行」中。感謝您也指向性能部分的指針。 – Andy 2010-01-18 12:35:57
當前核心數據性能鏈接:https://developer.apple.com/library/prerelease/watchos/documentation/Cocoa/Conceptual/CoreData/Performance.html – jQwierdy 2015-10-19 21:13:19