【问题标题】:What is the correct way to delete a UIManagedDocument?删除 UIManagedDocument 的正确方法是什么?
【发布时间】:2013-10-16 02:45:40
【问题描述】:

我似乎找不到任何可靠的文档来解释删除 UIManagedDocument 的正确过程,尤其是在 iCloud 选项已打开的情况下。

我了解此选项会删除此 fileURL 处的文件。如果不使用 iCloud,这似乎很好。

[[NSFileManager defaultManager] removeItemAtURL:fileURL error:&error];

如果使用 iCloud,CoreData 会在所有位置创建文件,包括在 /Document/CoreDataUbiquitySupport 和 iCloud /CoreData 文件夹中。所以在这种情况下,我是否可以在调用[NSFileManager removeItemAtURL] 之前为UIManagedDocument 中的每个商店调用removeUbiquitousContentAndPersistentStoreAtURL。如果是这样,这是否记录在某处?

[NSPersistentStoreCoordinator removeUbiquitousContentAndPersistentStoreAtURL:storeURL
                                                                     options:@{NSPersistentStoreUbiquitousContentNameKey:fileName,
   NSMigratePersistentStoresAutomaticallyOption:@YES,
         NSInferMappingModelAutomaticallyOption:@YES,
                          NSSQLitePragmasOption:@{ @"journal_mode" : @"DELETE" }}
                                                                       error:&error];

【问题讨论】:

    标签: core-data icloud uimanageddocument


    【解决方案1】:

    对于 iCloud 核心数据内容,您要在 NSPersistentStoreCoordinator 类上调用静态方法 removeUbiquitousContentAndPersistentStoreAtURL:options:error:,然后调用 removeItemAtURL:error:

    在我的APManagedDocument 项目中查看deleteManagedDocumentWithIdentifier:。这是在 ubiquitous_experiment 分支上,我目前正在将其合并回主分支之前完成。

    【讨论】:

    • 这是否也应该从其他设备上删除文件,或者我应该在其他设备上的应用程序中有逻辑来检测文件不再在 iCloud 中并在该设备上执行相同的操作?检测文件已被删除的逻辑是什么? Apple 提供的任何文档?
    • 调用removeUbiquitousContentAndPersistentStoreAtURL:options:error 将从iCloud 中删除Core Data 事务日志并删除持久存储文件,调用removeItemAtURL:error 将删除UIManagedDocument 包。现在我考虑了一下,因为我将文档存储在无处不在的存储中,所以我需要先setUbiquitous:NO 文档,以便 iCloud 也忘记这一点。有关removeUbiquitousContentAndPersistentStoreAtURL:options:error的更多信息,请参阅 WWDC 2013 Session 207
    • 当然,但是当应用程序在其他设备上启动时会发生什么?请记住,他们可能仍然有数据库的工作副本,我没有在 Core Data 的文档中看到通知告诉我们“糟糕,有人删除了云中的所有内容,你现在想做什么:a) 构建一个新副本并将其放回云中,b) 保留您的副本并使其仅本地化,c) 也删除您的副本或 d) 让我们决定 ?”
    • 这太奇怪了 - 现在我的 mac 也在线了!!这太疯狂了,我只是打开我现有的 sqlite 文件,然后在文件上运行迁移,这就是它们在云中,它们甚至不会自己移动到任何地方。完成,卡普特!太容易了!我确实必须关闭沙箱才能打开桌面上的任何文件。 Core Data 在与包含后备存储和内容的文档相同的目录中创建一个 CoreDataUbiquitySupport 文件夹。当然,他们不可能让这一切变得如此简单。
    • 你看过 WWDC 视频吗?他们有一个回调,当persistentStore 将被删除和/或更改并且应用程序仍在运行时发生。
    【解决方案2】:

    这是我在这个问题上的两分钱。我尝试了 dtrotzjr 推荐的方法,但并没有取得很大的成功。似乎 removeUbiquitousContentAndPersistentStoreAtURL:options:error: 非常适合清除 UIManagedDocument 中的数据,但 Logs 文件夹仍然存在,我要删除的文件的残余部分仍然存在。这是从 iCloud 或本地文档中完全删除 UIManagedDocument 的更简单方法:

    + (void)deleteDocumentURL:(NSURL *)url{
        //if we have an iCloud Document, remove it from the UbiquitouseKeyValueStore
        if ([self isiCloudURL:url]) {
            [[NSUbiquitousKeyValueStore defaultStore] removeObjectForKey:[url lastPathComponent]];
        }
    
        //do the delete on another thread
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            NSFileCoordinator *coordinator = [[NSFileCoordinator alloc] initWithFilePresenter:nil];
            NSError *coordinationError;
    
            [coordinator coordinateWritingItemAtURL:url
                                        options:NSFileCoordinatorWritingForDeleting
                                          error:&coordinationError
                                     byAccessor:^(NSURL *newURL) {
             NSError *removeError;
            //code for performing the delete
            [[NSFileManager defaultManager] removeItemAtURL:newURL error:&removeError];
    
            //if we have an iCloud file...
            if ([self isiCloudURL:url]) {
                //remove log files in CoreData directory in the cloud
                NSURL *changeLogsURL = [[self urlForiCloudLogFiles] URLByAppendingPathComponent:[url lastPathComponent]];
                    [[NSFileManager defaultManager] removeItemAtURL:changeLogsURL error:&removeError];
                }
            }];
        });
    }
    

    这几乎是斯坦福大学 2012 年 CS193 课程的代码 + changeLogs 文件夹的删除,它适用于本地和 iCloud 文档。如果您发现以这种方式执行删除有任何问题,请告诉我。

    【讨论】:

    • 看看这个链接,我在其中发布了一些代码示例和演示视频,说明了我如何让它非常可靠地工作。它显示了本地目录以及正在创建和清除的 iCloud 目录。ossh.com.au/design-and-technology/software-development/…
    • 酷,谢谢邓肯。这个文档看起来很有希望:)
    猜你喜欢
    • 2012-07-19
    • 2015-04-11
    • 2015-07-27
    • 2013-04-03
    • 1970-01-01
    • 2011-03-12
    • 1970-01-01
    • 2012-06-13
    • 1970-01-01
    相关资源
    最近更新 更多