带有Xcode和Swift的iOS 8核心数据+ iCloud

时间:2015-01-17 03:34:01

标签: ios xcode swift core-data icloud

我一直在阅读很多CoreData + iCloud并且理解它的任何内容都很简单。但是,我很早就陷入困境,如果你们有任何想法,我很好奇。

我的工作台: Xcode6.1.1,Swift,CoreData,添加了iCloud权利(在iOS8.1模拟器和iOS8.1设备上测试)

要亲眼看看,只需在Xcode中启动一个新的“Singe View应用程序”项目,选中“使用核心数据”复选框。为了启用iCloud,我添加了

let storeOptions = [
    NSPersistentStoreUbiquitousContentNameKey: "iCloudTest"
]

在调用coordinator!.addPersistentStoreWithType(NSSQLiteStoreType, configuration:URL:options:error:)

之前

我还为NSPersistentStoreCoordinatorStoresWillChangeNotificationNSPersistentStoreCoordinatorStoresDidChangeNotificationNSPersistentStoreDidImportUbiquitousContentChangesNotification添加了观察员。

当我第一次运行应用程序时,我的控制台输出如下所示:

storesDidChange: NSConcreteNotification 0x15d6b890 {name = NSPersistentStoreCoordinatorStoresDidChangeNotification; object = <NSPersistentStoreCoordinator: 0x15e96380>; userInfo = {
added =     (
    "<NSSQLCore: 0x15e9a1b0> (URL: file:///var/mobile/Containers/Data/Application/F2E684E6-6B7B-4429-B9CD-24C45DE18D27/Documents/CoreDataUbiquitySupport/mobile~BB56F5F3-5448-41A3-A142-921D99A85855/iCloudTest/1660860A-83E1-4906-BEB8-DAF02DAC62B0/store/iCloudTest.sqlite)"
     );
}}
2015-01-16 22:17:51.367 iCloudTest[783:414835] -[PFUbiquitySwitchboardEntryMetadata setUseLocalStorage:](808): CoreData: Ubiquity:  mobile~BB56F5F3-5448-41A3-A142-921D99A85855:iCloudTest
Using local storage: 1
managedObjectContext: Optional(<NSManagedObjectContext: 0x15d69340>)
2015-01-16 22:17:51.815 iCloudTest[783:414857] -[PFUbiquitySwitchboardEntryMetadata setUseLocalStorage:](808): CoreData: Ubiquity:  mobile~BB56F5F3-5448-41A3-A142-921D99A85855:iCloudTest
Using local storage: 0

正如Design for iCloud Programming Guide的检查点中所述,我应该看到Using local store: 1,然后在Using local storage: 0之后不久(在1-5个问题内)。到现在为止还挺好。我很高兴。

现在,在不更改一行代码的情况下,只需重新运行应用程序,就会导致以下日志输出:

 storesDidChange: NSConcreteNotification 0x166606f0 {name = NSPersistentStoreCoordinatorStoresDidChangeNotification; object = <NSPersistentStoreCoordinator: 0x165485b0>; userInfo = {
added =     (
    "<NSSQLCore: 0x1658dfb0> (URL: file:///var/mobile/Containers/Data/Application/A742A53E-6785-40AC-A21A-16D4BB5F332E/Documents/CoreDataUbiquitySupport/mobile~9CB93B5A-F976-4AB8-B1B3-290F18DE796A/iCloudTest/0A12A44B-599B-49DD-8AD8-546EFD4E26DC/store/iCloudTest.sqlite)"
    );
}}
2015-01-16 22:08:55.133 iCloudTest[770:412842] -[PFUbiquitySwitchboardEntryMetadata setUseLocalStorage:](808): CoreData: Ubiquity:  mobile~9CB93B5A-F976-4AB8-B1B3-290F18DE796A:iCloudTest
Using local storage: 1
managedObjectContext: Optional(<NSManagedObjectContext: 0x1658f2e0>)
2015-01-16 22:11:29.005 iCloudTest[770:412855] -[PFUbiquitySafeSaveFile waitForFileToDownload:](489): CoreData: Ubiquity:  <PFUbiquityBaseline: 0x165a5950>(0)
    permanentLocation: <PFUbiquityLocation: 0x165a6690>: /var/mobile/Library/Mobile Documents/iCloud~com~mycompany~iCloudTest/CoreData/iCloudTest/.baseline/iCloudTest/47DEQpj8HBSa~_TImW~5JCeuQeRkm5NMpJWZG3hSuFU=/baseline.zip
    safeLocation: <PFUbiquityLocation: 0x165a5ac0>: /var/mobile/Library/Mobile Documents/iCloud~com~mycompany~iCloudTest/CoreData/iCloudTest/.baseline/iCloudTest/47DEQpj8HBSa~_TImW~5JCeuQeRkm5NMpJWZG3hSuFU=/mobile~9CB93B5A-F976-4AB8-B1B3-290F18DE796A
    currentLocation: <PFUbiquityLocation: 0x165a6690>: /var/mobile/Library/Mobile Documents/iCloud~com~mycompany~iCloudTest/CoreData/iCloudTest/.baseline/iCloudTest/47DEQpj8HBSa~_TImW~5JCeuQeRkm5NMpJWZG3hSuFU=/baseline.zip

    storeName: iCloudTest
    modelVersionHash: 47DEQpj8HBSa~_TImW~5JCeuQeRkm5NMpJWZG3hSuFU=
    baselineArchiveLocation: <PFUbiquityLocation: 0x165a6690>: /var/mobile/Library/Mobile Documents/iCloud~com~mycompany~iCloudTest/CoreData/iCloudTest/.baseline/iCloudTest/47DEQpj8HBSa~_TImW~5JCeuQeRkm5NMpJWZG3hSuFU=/baseline.zip

Download failed for file, error: Error Domain=NSCocoaErrorDomain Code=512 "The file download timed out." UserInfo=0x16670090 {NSLocalizedDescription=The file download timed out.}
2015-01-16 22:11:29.009 iCloudTest[770:412855] -[PFUbiquitySwitchboardEntryMetadata setUseLocalStorage:](808): CoreData: Ubiquity:  mobile~9CB93B5A-F976-4AB8-B1B3-290F18DE796A:iCloudTest
Using local storage: 0

突然间,iCloud正在表演!我在Using local storage: 1Using local storage 0之间约定了90秒。

这有什么值得担心的吗?我担心如果sync'ing对我的用户来说花费那么长时间,那将是一个非常讨厌的经历(只想使用应用程序1分钟+突然iCloud内容弹出...)我想,因为Using local storage 0最终做到了火,iCloud同步启用?但是,调试导航器中的iCloud Report显示Status: iCloud not configured

我做错了吗?

PS:这是直接的苹果样板,只有额外的代码来添加iCloud权利。

1 个答案:

答案 0 :(得分:0)

没什么好担心的。与iCloud同步本质上是异步的并且容易出错(就像你处于飞行模式一样)。最终本地和iCloud版本将同步,您将需要处理相同记录冲突的一些情况。