2012-04-13 107 views
1

我正在使用核心数据和iCloud在多个iPad之间同步数据的应用程序。这一切都工作正常,我可以添加数据到每个iPad,它会同步它们之间的所有。核心数据和iCloud添加预先填充的sqlite文件

我有一个核心数据sqlite文件,预填充的国家列表,我想复制到应用程序的第一次运行的文档区域。我有这个工作,但已经改变了我的persistentStoreCoordinator实现,以测试是否支持iCloud以及其他一些小的改变。

但是现在,当我检查,看看是否SQLite的文件存在和复制的预先填充的sqlite的文件,如果没有我在

[psc addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:[NSURL fileURLWithPath:iCloudData] options:options error:&persistentStoreError]; 

NSPersistentStoreCoordinator addPersistentStoreWithType:configuration:URL:options:error:: CoreData: Ubiquity: An error occured while setting up the ubiquity integration: Error Domain=NSCocoaErrorDomain Code=134316 "The ubiquity container does not appear to match this persistent store, it is possible this is caused by switching to a different iCloud account, or logging out of iCloud entirely. The store should be re-opened with read-only attributes or removed from iCloud syncing permanently." UserInfo=0x1cb590 {storeUUID=31381598-EAFA-4550-9B96-F501800974D5, containerUUID=E3A8DC7D-41FD-405A-8D8A-C06C8B467CA2, NSLocalizedDescription=The ubiquity container does not appear to match this persistent store, it is possible this is caused by switching to a different iCloud account, or logging out of iCloud entirely. The store should be re-opened with read-only attributes or removed from iCloud syncing permanently.}

收到以下错误这是因为有仍在iCloud中引用不同的Core Data sqlite文件或事务日志文件?如果是这样,我将如何删除它们?

回答

4

您不应该将预先创建的Core Data存储复制到无处不在容器中。旧的方法是在Simulator/device/computer上创建一个SQLite文件,并将其复制到第一次运行的位置。如果您使用iCloud,则无法再进行此操作。每台iCloud都会收到一系列更改为“事务日志”,然后将其应用于自己的SQLite存储。它需要知道发生了什么变化,并且获取一个.sqlite文件不会告诉它。

icloud的方式做到这一点可以是:

  • 从代码(例如一个insertStartData:法)内创建的初始数据。你可以从plist中添加它,或者你喜欢的任何东西。您必须先检查数据是否已经存在。
  • 使用NSPersistentStoreCoordinatormigratePersistentStore:toURL:options:withType:error复制您的初始商店。

Here's the docs:

You should not seed initial content with a prepackaged database file. Instead, you should create the default items in code, or use NSPersistentStoreCoordinator's migratePersistentStore:toURL:options:withType:error: method to migrate a prepackaged database to the required location.

+0

的解释非常感谢,我只需要输入他们的纬度和长双的沿国家(200东西)名单到coredata在第一次启动时,将一个plist中更有效率比捆绑sqlite文件和使用migratePersistentStore? – 2012-04-15 17:55:16

+0

是的,一个plist会容易得多。记得检查它是否已经被导入。 – 2012-04-15 19:48:24

+0

嗨,你会用什么方法来测试商店需要添加第一次运行,另外,我有一个嵌套数据JSON文件 - 最好的方法是什么使用? – 2013-03-04 17:08:02

相关问题