简体   繁体   English

将Core Data App迁移到iCloud

[英]Migrate Core Data App to iCloud

This is my scenario: 这是我的情景:

My app use core data and i'm adding iCloud support, but i'm having problems trying to migrate users data to iCloud. 我的应用程序使用核心数据,我正在添加iCloud支持,但我在尝试将用户数据迁移到iCloud时遇到问题。

Here what i had tried: 这是我尝试过的:

- Added my current local database using addPersistentStoreWithType - 使用addPersistentStoreWithType添加了我当前的本地数据库

    NSError* error;

    NSURL* oldStoreURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:YES error:NULL];
    oldStoreURL =[oldStoreURL URLByAppendingPathComponent:@"Database.sqlite"];

    // add local store (local version)

   NSPersistentStore  *store =  [self.managedObjectContext.persistentStoreCoordinator  addPersistentStoreWithType:NSSQLiteStoreType
                                                                        configuration:nil
                                                                                  URL:oldStoreURL
                                                                              options:options
                                                                                error:&error];

- Use the method migratePersistentStore to migrate (probably i'm missing something here): - 使用migratePersistentStore方法进行迁移(可能我在这里遗漏了一些东西):

    NSURL* storeURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:YES error:NULL];
    storeURL =  [storeURL URLByAppendingPathComponent:@"Database.sqlite"];

[self.managedObjectContext.persistentStoreCoordinator migratePersistentStore:store toURL:storeURL options:iCloudOptions withType:NSSQLiteStoreType error:&error];

I'm getting a Cocoa error 134080, Can't add the same store twice. 我得到一个Cocoa错误134080,不能两次添加同一个商店。

Can someone explain how this migratePersistentStore method works and what i need to pass for each parameter? 有人可以解释这个migratePersistentStore方法如何工作以及我需要为每个参数传递什么? I found little about it on Apple docs. 我在Apple文档上找不到它。

The point of migratePersistentStore is that you're moving to a different store. migratePersistentStore是你要移动到另一个商店。 You can't migrate to and from the same place. 您无法迁移到同一个地方或从同一个地方迁移。

Since you're migrating to iCloud, use a different name and/or path for the iCloud-enabled store. 由于您要迁移到iCloud,因此请为启用iCloud的商店使用其他名称和/或路径。 For example, name the iCloud one Database-iCloud.sqlite . 例如,将iCloud命名为Database-iCloud.sqlite Then migrate from your existing store to that one. 然后从现有商店迁移到该商店。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM