UDSync 0.2.0

UDSync 0.2.0

测试测试
Lang语言 Obj-CObjective C
许可证 MIT
发布最后发布2016年12月
SwiftSwift 版本3.0

Alexandre de Oliveira 维护。



 
依赖关系
Alamofire~> 4
BrightFutures~> 5.1
SwiftyJSON~> 3.1
 

UDSync 0.2.0

  • Alexandre de Oliveira

UDSync for Swift

tl;dr: UDSync 包含实用的功能,以支持离线优先应用最常见的情况。


很多时候,我们需要构建离线优先的移动应用程序或使用实时通知,结果是反复构建自定义解决方案。这保证了我们不会陷入过时的库。

UDSync 不是一个数据库。

您将希望使用相应的服务器端库来简化您的工作: ud-sync-rails

特性

特性 描述
同步队列(即将推出) 离线保存记录,UDSync 将将其添加到队列,并在网络恢复时自动将其发送到服务器。
同步删除 在一个离线优先的应用中,当 DeviceA 在 DeviceB 离线时删除了一条记录,当 DeviceB 重连时,DeviceB 就需要知道这个情况。UDSync 会帮助处理这一点。

安装

在您的 Podfile 中使用

pod 'UDSync'

用法

同步删除

假设 DeviceA 在 DeviceB 离线时删除了 RecordN,那么当 DeviceB 重连时,它需要删除 RecordN。我们记录服务器上的所有保存/删除操作并通过 GET /ud_sync/operations 端点公开。

GET /operations

{
  "operations": [{
    "id": "operation-unique-id1",
    "name": "delete",
    "record_id": "record-id1",
    "entity": "user",
    "date": "2015-10-21T10:00:00Z"
  }, {
    "id": "operation-unique-id2",
    "name": "save",
    "record_id": "record-id2",
    "entity": "post",
    "date": "2015-10-21T10:00:00Z"
  }]
}

基于这些信息,您可以知道需要删除什么。

在您的 Swift 应用程序中,使用以下代码

// 1. fetch all operations
UDSentinel.sharedInstance.fetchOperations("http://api.myserver.com", accessToken: "") { (operations) in

    // `operations` is an array of UDOperation instances. Each UDOperation
    // responds to (check its test file for more details):
    //
    //   operation.entity() -> the name of the entity that was updated or deleted
    //   operation.operationName() -> either `delete` or `save`
    //   operation.recordId()

    // 2. iterate over each operation
    for operation in operations {
        if operation.operationName() == "delete" {
            if operation.entity() == "Customer" {
                // 3a. delete the record locally

                // Customer class here is your own code
                let id = operation.recordId()
                Customer.find(id).destroy()
            } else if operation.entity() == "Deal" {
                // 3b. delete the record according to the entity name
                //
                // ...
            }
        }
    }
}

许可证

它是 MIT 许可证,所以您可以随意使用。

作者

Alexandre de Oliveira (@kurko)