2014-04-12 23 views
0

我想知道如果你可以经常调用NSManagedObjectContext保存。我保存的代码是标准的UIApplicationDelegate代码:调用NSManagedObjectContext上的saveContext太多

- (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. 
      NSLog(@"Unresolved error %@, %@", error, [error userInfo]); 
      abort(); 
     } 
    } 
} 

我带来了一个模式,以创建一个新的报警类型对象的应用程序。模态有几个屏幕,像重复的日子。类似于Apple重复的日子观点,我推出了一个新的viewController,它显示了重复的日子。当我回到用户输入闹钟名称的模式主屏幕时,是否可以调用saveContext?我想我想知道是否需要担心为较小的屏幕调用saveContext,如重复的日子,然后用户在主模态屏幕上点击完成,然后立即再次调用saveContext。

回答

0

如果您在整个应用程序中只有一个NSManagedObjectContext,则不必在上下文中经常调用-save(只要您的应用程序不会崩溃!)。在这种情况下,我通常会在离开编辑屏幕时保存,如果一些信息已被更改,并保存在应用程序代理-applicationDidEnterBackground:中。

如果您有很多更改(新对象,更新等),那么调用save会有点贵,因为它必须将这些更改写入持久存储区(磁盘上的文件 - 假设您'通常使用NSPersistedStore)。