Objective c 复制SQLite存储

Objective c 复制SQLite存储,objective-c,core-data,Objective C,Core Data,我正在尝试对NSPersistentDocument的子类实现writesAffelyToUrl:of type:forSaveOperation:error:。我必须处理NSAUTOSAVEELSEWHERE操作条件,这实际上是SQLite存储的重复操作 下面的代码有一个问题,因为它创建了一个重复的存储,但是存储实际上是空的,我不知道为什么。当我使用“还原文档”选项时,我注意到了这个问题,但当我尝试使用“复制”菜单项时,它变得清晰起来 基本上,我正在尝试删除当前的存储,所以它不会被保存。然后我

我正在尝试对NSPersistentDocument的子类实现writesAffelyToUrl:of type:forSaveOperation:error:。我必须处理NSAUTOSAVEELSEWHERE操作条件,这实际上是SQLite存储的重复操作

下面的代码有一个问题,因为它创建了一个重复的存储,但是存储实际上是空的,我不知道为什么。当我使用“还原文档”选项时,我注意到了这个问题,但当我尝试使用“复制”菜单项时,它变得清晰起来

基本上,我正在尝试删除当前的存储,所以它不会被保存。然后我添加临时存储,保存上下文,删除临时存储,并重新添加原始存储

if (inSaveOperation == NSAutosaveElsewhereOperation) {
    NSPersistentStoreCoordinator *coordinator = [[self managedObjectContext] persistentStoreCoordinator];
    id tempStore = [coordinator addPersistentStoreWithType:@"SQLite" configuration:nil URL:storeURL options:nil error:outError];
    success = (tempStore != nil);
    id originalStore = [coordinator persistentStoreForURL:originalStoreURL];
    success = success && 
        [coordinator removePersistentStore:originalStore error:outError];

    success = success && 
        [[self managedObjectContext] save:outError];

    // restore original store
    success = success && 
        [coordinator removePersistentStore:tempStore error:outError];
    success = success && 
        [self configurePersistentStoreCoordinatorForURL:originalStoreURL ofType:inTypeName modelConfiguration:nil storeOptions:nil error:nil];

因为您试图在应用程序文件夹中服务存储,而该文件夹恰好是只读的。 我想你应该切换到文档文件夹

NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *docsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

它绝对不是应用程序文件夹。我有一些NSLog代码,可以打印像/private/var/folders/4z/7bzwcw5915g29x1363xhhfm40000gp/T/TemporaryItems这样的内容。保存操作是由自动保存触发的,所以我不选择位置。我目前对NSAutoSaveElSewhere操作的理解不是(仅)重复。它发生在自动保存之后,只要文档还没有位置。我现在看到了两个用例,它们发生在(1)尚未保存的新文档之后和(2)现有文档的副本之后