删除UIManagedDocument的正确方法是什么?

时间:2013-10-08 23:18:07

标签: core-data icloud uimanageddocument

我似乎无法找到任何可靠的文档来解释删除UIManagedDocument的正确程序,尤其是已启用iCloud选项的程序。

我知道此选项会删除此fileURL中的文件。如果没有使用iCloud,这似乎没问题。

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

如果正在使用iCloud,则CoreData会在整个地方创建文件,包括/ Document / CoreDataUbiquitySupport和iCloud / CoreData文件夹。因此,在这种情况下,我应该在调用removeUbiquitousContentAndPersistentStoreAtURL之前为UIManagedDocument中的每个商店致电[NSFileManager removeItemAtURL]。如果是这样在某处记录了吗?

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

2 个答案:

答案 0 :(得分: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];
            }
        }];
    });
}

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

答案 1 :(得分:1)

对于要在removeUbiquitousContentAndPersistentStoreAtURL:options:error:课程上调用静态方法NSPersistentStoreCoordinator的iCloud核心数据内容,请调用removeItemAtURL:error:

请参阅我的APManagedDocument项目中的deleteManagedDocumentWithIdentifier:。这是在ubiquitous_experiment分支上,我目前正在进行最终确定,然后将其合并回主分支。

相关问题