【问题标题】:"Save" in CoreData-appsCoreData 应用程序中的“保存”
【发布时间】:2009-01-30 16:16:47
【问题描述】:

我在这里有一个 CoreData 应用程序(不基于文档)、1 个实体和 1 个表格视图,用于编辑/添加/删除实体的“实例”。现在我可以手动添加和保存,但我想

a) 自动保存更改
b) 首次启动时自动添加一些“实例”。

我认为a) 可以通过 NSNotifications 解决。但是实体使用哪个?

任何想法如何解决a)b)

感谢every 的回答。 =)

【问题讨论】:

    标签: objective-c cocoa core-data


    【解决方案1】:

    有时,自动保存可能比您最初预期的要复杂一些,因为有时您的应用程序数据处于无效状态(例如,当用户正在编辑实体时)并且无法保存或无法保存保存没有意义。不幸的是,没有简单的setAutosaves:YES 属性,因此您必须自己实现它。在某些操作后使用通知保存是一种方法,如果对您的应用程序有意义,您还可以设置一个计时器以定期保存。

    要填充一个空的数据文件,只需检查启动时数据存储是否为空(applicationDidFinishLaunchingawakeFromNib 是两个可能的放置位置),以及是否正常插入一些实体。唯一棘手的部分是在此过程中禁用撤消管理。这是我的一个应用程序中的一个示例:

    - (void)applicationDidFinishLaunching:(NSNotification *)aNotification;
    {       
        NSURL *fileURL = [NSURL fileURLWithPath:[self.applicationSupportFolder stringByAppendingPathComponent:WLDataFileName]];
    
        NSManagedObjectModel *model = [NSManagedObjectModel mergedModelFromBundles:nil];    
        NSPersistentStoreCoordinator *coordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:model];
        NSManagedObjectContext *context = [[NSManagedObjectContext alloc] init];
        NSFetchRequest *request = [[NSFetchRequest alloc] init];
    
        [coordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:fileURL options:nil error:NULL];
        [context setPersistentStoreCoordinator:coordinator];
        [request setEntity:[NSEntityDescription entityForName:@"Shelf" inManagedObjectContext:context]];
    
        if ( [context countForFetchRequest:request error:NULL] == 0 )
            [self _populateEmptyDataStore:context];
    
        _managedObjectContext = [context retain];
    
        [request release];
        [coordinator release];
        [context release];
    
        // finish loading UI, etc...
    }
    
    - (void)_populateEmptyDataStore:(NSManagedObjectContext *)context;
    {
        [[context undoManager] disableUndoRegistration];
    
        WLSmartShelfEntity *allItems = [NSEntityDescription insertNewObjectForEntityForName:@"SmartShelf" inManagedObjectContext:context];
        WLSmartShelfEntity *trash = [NSEntityDescription insertNewObjectForEntityForName:@"SmartShelf" inManagedObjectContext:context];
    
        allItems.name = NSLocalizedString( @"All Items", @"" );
        allItems.predicate = [NSPredicate predicateWithFormat:@"isTrash = FALSE"];
        allItems.sortOrder = [NSNumber numberWithInteger:0];
        allItems.editable = [NSNumber numberWithBool:NO];
    
        trash.name = NSLocalizedString( @"Trash", @"" );
        trash.predicate = [NSPredicate predicateWithFormat:@"isTrash = TRUE"];
        trash.sortOrder = [NSNumber numberWithInteger:2];
        trash.editable = [NSNumber numberWithBool:NO];
    
        [context processPendingChanges];
        [[context undoManager] enableUndoRegistration];
    
        DebugLog( @"Filled empty data store with initial values." );
    }
    

    【讨论】:

      【解决方案2】:

      查看 Apple 邮件列表中有关自动保存和核心数据的 this thread

      【讨论】:

      • AFAICS,那个链接没用——它指的是一个零信息的线程。
      • 考虑到我在 2 年前添加了这个答案,我认为文档的状态已经改变。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多