ios - 将 NSPersistentStore 从应用程序沙箱迁移到共享组容器

标签 ios objective-c core-data watchkit

我正在尝试将我的 NSPersistentStore 从我的应用程序的沙箱移动到共享组容器,以便我可以从我的 WatchKit 扩展访问 CoreData。我目前正在将 Core Data 与 iCloud 一起使用,并希望将用户数据移动到共享组容器中。目前我正在创建 NSPersistentStoreCoordinator,如下所示:

if (__persistentStoreCoordinator != nil) {
    return __persistentStoreCoordinator;
}

NSURL *url = [self storeURL]; // app's Documents directory
NSError *error = nil;
__persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
if (![__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:url options:[self iCloudPersistentStoreOptions] error:&error]) {
    NSLog(@"Error adding persistent store: %@", [error localizedDescription]);
}

// rest of setup

return __persistentStoreCoordinator;

我已经在我的应用程序目标和 WatchKit 扩展目标中设置了共享组容器,并且可以使用 - (NSURL *)containerURLForSecurityApplicationGroupIdentifier:(NSString *)groupIdentifier 获取新商店位置的 NSURL .

如何检查我是否需要迁移或我是否已经迁移以便不尝试多次迁移?最初我在想这样的事情,但这不起作用,因为旧商店 URL 不存在

if (__persistentStoreCoordinator != nil) {
    return __persistentStoreCoordinator;
}

NSURL *newURL = [self newStoreURL]; // shared group container
NSURL *oldURL = [self oldStoreURL]; // app's Documents directory

__persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];

NSFileManager *fileManager = [NSFileManager defaultManager];

if (![fileManager fileExistsAtPath:newURL.path] && [fileManager fileExistsAtPath:oldURL.path]) {
    NSLog(@"performing migration...");
    NSPersistentStore *oldStore = [__persistentStoreCoordinator persistentStoreForURL:oldURL];
    NSError *migrateError = nil;
    NSPersistentStore *newStore = [__persistentStoreCoordinator migratePersistentStore:oldStore toURL:newURL options:[self iCloudPersistentStoreOptions] withType:NSSQLiteStoreType error:&migrateError];
    if (!newStore) {
        NSLog(@"Error migrating store: %@", [migrateError localizedDescription]);
    }
}

// rest of setup

return __persistentStoreCoordinator;

最佳答案

据我所知,如果您发布的迁移逻辑正常运行,那么您似乎已经成功了。您似乎缺少的是 else if 来处理您没有持久存储的情况。下面应该处理这种情况。

if (__persistentStoreCoordinator != nil) {
    return __persistentStoreCoordinator;
}

NSURL *newURL = [self newStoreURL]; // shared group container
NSURL *oldURL = [self oldStoreURL]; // app's Documents directory

__persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];

NSFileManager *fileManager = [NSFileManager defaultManager];

if (![fileManager fileExistsAtPath:newURL.path] && [fileManager fileExistsAtPath:oldURL.path]) {
    NSLog(@"performing migration...");
    NSPersistentStore *oldStore = [__persistentStoreCoordinator persistentStoreForURL:oldURL];
    NSError *migrateError = nil;
    NSPersistentStore *newStore = [__persistentStoreCoordinator migratePersistentStore:oldStore toURL:newURL options:[self iCloudPersistentStoreOptions] withType:NSSQLiteStoreType error:&migrateError];
    if (!newStore) {
        NSLog(@"Error migrating store: %@", [migrateError localizedDescription]);
    }
} else if (![fileManager fileExistsAtPath:newURL.path]) {
    if (![__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:newURL options:[self iCloudPersistentStoreOptions] error:&error]) {
        NSLog(@"Error adding persistent store: %@", [error localizedDescription]);
    }
}

// rest of setup

return __persistentStoreCoordinator;

关于ios - 将 NSPersistentStore 从应用程序沙箱迁移到共享组容器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28708870/

相关文章:

ios - 将 imageView、图像和背景图像保存到相机胶卷?

html - 为什么此CSS动画无法在IOS上运行?

objective-c - 如何从异步 NSURLConnection 返回到调用类?

objective-c - Mac 桌面连接 IOS 设备

ios - 批量更新后更新托管对象上下文中的托管对象

swift - 如何允许用户仅打开以编程方式构建的单个 uiSwitch 并存储开关的数据

ios - 如何对齐表格 View 行中的自定义单元格-左或右-IOS

ios - Firebase 测试实验室是否支持 XCUITests?

ios - UICollectionView deleteItemsAtIndexPaths 失败

ios - 如何在不使用 contextForCurrentThread 的情况下使用 Magical Record 创建和更新对象并保存它们