gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:16:06 25 4
gpt4 key购买 nike

我正在尝试将我的 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com