gpt4 book ai didi

ios - 如何将现有核心数据 iOS 7 应用程序的数据迁移到 iCloud?

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:43:49 26 4
gpt4 key购买 nike

我有一个已经使用 Core Data 的 iOS 7 应用程序。我已经使用新的 iOS 7 方法将 iCloud 集成到我的应用程序中,以使用以下代码为例来同步存储在核心数据中的项目:

https://github.com/mluisbrown/iCloudCoreDataStack/blob/master/README.md

这很好用,只是设备上的所有原始数据都没有显示在 iCloud 商店中。我一直听说我需要迁移数据 - 但我找不到任何有关如何正确执行此操作的示例。有谁知道如何做到这一点?

我一直被指向使用 migratePersistentStore:toURL:options:withType:error: ,但我不知道如何使用它...

最佳答案

这是一个带有 iCloud 控制面板的示例应用程序,用于将商店移入或移出 iCloud。要移动现有商店,您需要使用现有选项打开它,但请确保您对目标商店使用 iOS7 选项。查看 OSCDStackManager 中的示例应用程序代码,如果您有具体问题,请提出。 http://ossh.com.au/design-and-technology/software-development/sample-library-style-ios-core-data-app-with-icloud-integration/

- (bool)moveStoreFileToICloud:(NSURL*)fileURL delete:(bool)shouldDelete backup:(bool)shouldBackup {
FLOG(@" called");

// Always make a backup of the local store before migrating to iCloud
if (shouldBackup)
[self backupLocalStore];

NSPersistentStoreCoordinator *migrationPSC = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:self.managedObjectModel];

// Open the existing local store using the original options
id sourceStore = [migrationPSC addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:fileURL options:[self localStoreOptions] error:nil];

if (!sourceStore) {

FLOG(@" failed to add old store");
return FALSE;
} else {
FLOG(@" Successfully added store to migrate");

bool moveSuccess = NO;
NSError *error;

FLOG(@" About to migrate the store...");
// Now migrate the store using the iCloud options
id migrationSuccess = [migrationPSC migratePersistentStore:sourceStore toURL:[self icloudStoreURL] options:[self icloudStoreOptions] withType:NSSQLiteStoreType error:&error];

if (migrationSuccess) {
moveSuccess = YES;
FLOG(@"store successfully migrated");
[self deregisterForStoreChanges];
_persistentStoreCoordinator = nil;
_managedObjectContext = nil;
self.storeURL = [self icloudStoreURL];
// Now delete the local file
if (shouldDelete) {
FLOG(@" deleting local store");
[self deleteLocalStore];
} else {
FLOG(@" not deleting local store");
}
return TRUE;
}
else {
FLOG(@"Failed to migrate store: %@, %@", error, error.userInfo);
return FALSE;
}

}
return FALSE;
}

关于ios - 如何将现有核心数据 iOS 7 应用程序的数据迁移到 iCloud?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22104403/

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