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

时间:2015-02-25 00:13:51

标签: ios objective-c core-data watchkit

我正在尝试将我的NSPersistentStore从我的应用程序的沙箱移动到共享组容器,以便我可以从WatchKit扩展程序访问CoreData。我目前正在使用iCloud的Core Data,并希望将用户数据移动到共享组容器。目前我正在创建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;

1 个答案:

答案 0 :(得分:1)

据我所知,如果您发布的迁移逻辑正常工作,您似乎就在那里。您似乎缺少的是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;