CKSIncrementalStore 0.5.2

CKSIncrementalStore 0.5.2

测试已测试
Lang语言 SwiftSwift
许可证 MIT
发布最后发布2015年9月
SPM支持SPM

Nofel Mahmood维护。



CKSIncrementalStore

CKSIncrementalStore是的一个子类。它使用CoreData自动创建和管理SQLite存储,以在CloudKit服务器上存储和同步与用户的私有数据库。

它有助于您重用您的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实例来执行操作,并在操作开始和结束时抛出两个通知。

通知
  • CKSIncrementalStoreDidStartSyncOperationNotification

同步操作开始时发布此通知。

示例

NSNotificationCenter.defaultCenter().addObserver(self, selector: "syncFinished:", name: CKSIncrementalStoreDidStartSyncOperationNotification, object: self.cksIncrementalStore)
  • CKSIncrementalStoreDidFinishSyncOperationNotification

同步操作结束时发布此通知。

示例

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种同步冲突解决策略。

  • GreaterModifiedDateWins

这是默认的。具有较大修改日期的记录被认为是真实记录。

  • UserTellsWhichWins

设置此同步策略需要您设置 CKSIncrementalStorerecordConflictResolutionBlock 实例。

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 的 公共数据库 不支持。

  1. 使用默认区域存储记录的缺点是它没有任何特殊的功能。您不能在默认区域原子性地保存一组记录到iCloud。同样,您不能在默认区域的记录上使用 CKFetchRecordChangesOperation 对象。

  2. 您不能在公共数据库中创建自定义区域。

入门指南

查看示例 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 制作。

联系我们

TwitterGitHub 上关注 Nofel Mahmood,或通过电子邮件[email protected]联系他

许可协议

CKSIncrementalStore 在 MIT 许可下可用。有关更多信息,请参阅 LICENSE 文件。