我正在尝试将我的NSPersistentStore从我的应用程序的沙箱移动到一个共享群组容器,这样我就可以从我的WatchKit扩展访问CoreData了。我目前正在通过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;发布于 2015-02-26 00:44:58
据我所知,如果你发布的迁移逻辑正在工作,那么看起来你已经很好地完成了。您似乎缺少一个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;https://stackoverflow.com/questions/28708870
复制相似问题