0

如何将数据从现有的非文档核心数据存储导入到 uimanageddocument?

问题是如何从旧的 managedObjectContext 复制到新的。

4

1 回答 1

3

答案是子类 UIManagedDocument,其中将现有存储导入(简单复制)到文档存储:

- (BOOL)configurePersistentStoreCoordinatorForURL:(NSURL *)storeURL ofType:(NSString *)fileType modelConfiguration:(NSString *)configuration storeOptions:(NSDictionary *)storeOptions error:(NSError **)error
{
    // If legacy store exists, copy it to the new location
    NSURL *legacyPersistentStoreURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"Guides.sqlite"];

    NSFileManager* fileManager = [NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:legacyPersistentStoreURL.path])
    {
        NSLog(@"Old db exists");

        NSError* thisError = nil;
        [fileManager replaceItemAtURL:storeURL withItemAtURL:legacyPersistentStoreURL backupItemName:nil options:NSFileManagerItemReplacementUsingNewMetadataOnly resultingItemURL:nil error:&thisError];
        NSLog([thisError localizedDescription]);
    }

    return [super configurePersistentStoreCoordinatorForURL:storeURL ofType:fileType modelConfiguration:configuration storeOptions:storeOptions error:error];
}
于 2012-02-04T19:15:24.190 回答