将NSPersistentStore从应用程序沙箱迁移到共享组容器 [英] Migrating NSPersistentStore from application sandbox to shared group container

查看:146
本文介绍了将NSPersistentStore从应用程序沙箱迁移到共享组容器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to move my NSPersistentStore from my app's sandbox to a shared group container so I can access CoreData from my WatchKit extension. I am currently using Core Data with iCloud and want to move the users data to the shared group container. Currently I am creating the NSPersistentStoreCoordinator as follows:

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为新商店位置使用 - (NSURL *)containerURLForSecurityApplicationGroupIdentifier:(NSString *)groupIdentifier

I've already setup the shared group container in my app target and WatchKit extension target and can get the NSURL for the new store location using - (NSURL *)containerURLForSecurityApplicationGroupIdentifier:(NSString *)groupIdentifier.

我检查我是否需要迁移或我已经迁移,所以我不尝试迁移多次?最初我在想这样的东西,但这不工作,因为旧商店的URL不存在

How can I check whether I need to migrate or that I've already migrated so I don't attempt to migrate multiple times? Initially I was thinking of something like this, but this doesn't work as the old store URL doesn't exist

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 来处理你没有持久存储的情况。

From what I can tell, it looks like you're pretty much there if the migration logic you posted is working. What it seems you are missing is an else if to handle the case where you don't have a persistent store. The following should handle that case.

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;

这篇关于将NSPersistentStore从应用程序沙箱迁移到共享组容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆