CKSIncrementalStore是
它有助于您重用您的CoreData知识,并用CloudKit为您的应用提供动力。它处理几乎所有与CloudKit相关的麻烦。
var coordinator: NSPersistentStoreCoordinator? = NSPersistentStoreCoordinator(managedObjectModel:self.managedObjectModel)
let url = self.applicationDocumentsDirectory.URLByAppendingPathComponent("CKSIncrementalStore_iOSDemo.sqlite")
var error: NSError? = nil
var failureReason = "There was an error creating or loading the application's saved data."
var persistentStore:NSPersistentStore? = coordinator!.addPersistentStoreWithType(CKSIncrementalStore.type, configuration: nil, URL: url, options: nil, error: &error)
if persistentStore != nil
{
self.cksIncrementalStore = persistentStore as? CKSIncrementalStore
// Store it in a property.
}
同步操作使用一个私有的NSManagedObjectContext实例来执行操作,并在操作开始和结束时抛出两个通知。
同步操作开始时发布此通知。
示例
NSNotificationCenter.defaultCenter().addObserver(self, selector: "syncFinished:", name: CKSIncrementalStoreDidStartSyncOperationNotification, object: self.cksIncrementalStore)
同步操作结束时发布此通知。
示例
NSNotificationCenter.defaultCenter().addObserver(self, selector: "syncFinished:", name: CKSIncrementalStoreDidFinishSyncOperationNotification, object: self.cksIncrementalStore)
同步以两种方式工作。一种是手动,另一种是自动。
在任何时候,在CKSIncrementalStore的一个实例上调用triggerSync()即可。
self.cksIncrementalStore.triggerSync()
当您在NSManagedObjectContext的一个实例上调用save时,CKSIncrementalStore会自动调用triggerSync()。
但是如果服务器上的某些其他设备改变了服务器上的某些记录,不用担心,我们已经为您考虑到了,同时也需要您的帮助。
为您的应用启用推送通知。然后在您的AppDelegate方法中
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject])
{
self.cksIncrementalStore?.handlePush(userInfo: userInfo)
}
生活中充满了冲突,数据也是如此。好事是“数据”很容易通过这样的库冲突轻松解决。
CKSIncrementalStore默认支持4种同步冲突解决策略。
这是默认的。具有较大修改日期的记录被认为是真实记录。
设置此同步策略需要您设置 CKSIncrementalStore
的 recordConflictResolutionBlock
实例。
var recordConflictResolutionBlock:((clientRecord:CKRecord,serverRecord:CKRecord)->CKRecord)?
它为您提供两种记录版本。客户端记录和服务器记录。您可以在服务器记录上做出任何更改,然后返回。
它简单地将服务器记录视为真实记录。
它简单地将客户端记录视为真实记录。
您可以通过在将 CKSIncrementalStore
添加到 NSPersistentStoreCoordinator
时作为选项传递来设置任何策略。
var options:Dictionary<NSObject,AnyObject> = Dictionary<NSObject,AnyObject>()
options[CKSIncrementalStoreSyncConflictPolicyOption] = NSNumber(short: CKSStoresSyncConflictPolicy.ClientRecordWins.rawValue)
var persistentStore:NSPersistentStore? = coordinator!.addPersistentStoreWithType(CKSIncrementalStore.type, configuration: nil, URL: url, options: options, error: &error)
CQSIncrementalStore 当前仅支持用户的 CloudKit 私有数据库
。它创建并使用一个自定义区域来存储数据并从服务器获取更改。
这里是文档中直接给出的两个原因,为什么 CloudKit 的 公共数据库
不支持。
查看示例 iOS 演示应用。在两个设备上运行它,开始添加、删除和修改记录并体验魔法。
推荐使用 CocoaPods
方法将 CQSIncrementalStore 添加到您的项目中。
您想在 Podfile 中添加 pod 'CKSIncrementalStore', '~> 0.5.2'
,如下所示
target 'MyApp' do
pod 'CKSIncrementalStore', '~> 0.5.2'
end
然后在您的终端或从 CocoaPods.app 中运行 [sudo] pod install
。
CKSIncrementalStore 由 Nofel Mahmood 制作。
在 Twitter 和 GitHub 上关注 Nofel Mahmood,或通过电子邮件[email protected]联系他
CKSIncrementalStore 在 MIT 许可下可用。有关更多信息,请参阅 LICENSE 文件。