caiyue1993 / IceCream
- четверг, 30 ноября 2017 г. в 03:13:33
Sync Realm Database with CloudKit
IceCream helps you sync Realm Database with CloudKit.
"It works like magic!"
Realm Database
Apple CloudKit
Reachability(Support Long-lived Operation)
Error Handling
Sync Automatically
Manually Synchronization is also supported
User Account Status Check
Complete Documentation
Be sure to have enrolled in Apple Developer Program
Turn on your iCloud in Capabilities and choose CloudKit
Turn on Background Modes and check Background fetch
and Remote notification
class Dog: Object {
@objc dynamic var id = NSUUID().uuidString
@objc dynamic var name = ""
@objc dynamic var age = 0
@objc dynamic var isDeleted = false
override class func primaryKey() -> String? {
return "id"
}
}
extension Dog: CKRecordConvertible {
/// recordName : this is the unique identifier for the record, used to locate records on the database. We can create our own ID or leave it to CloudKit to generate a random UUID.
/// For more: https://medium.com/@guilhermerambo/synchronizing-data-with-cloudkit-94c6246a3fda
var recordID: CKRecordID {
return CKRecordID(recordName: id, zoneID: Constants.customZoneID)
}
var record: CKRecord {
// Simultaneously init CKRecord with zoneID and recordID, thanks to this guy: https://stackoverflow.com/questions/45429133/how-to-initialize-ckrecord-with-both-zoneid-and-recordid
let r = CKRecord(recordType: Dog.recordType, recordID: recordID)
r[.id] = id as CKRecordValue
r[.age] = age as CKRecordValue
r[.name] = name as CKRecordValue
r[.isDeleted] = isDeleted as CKRecordValue
return r
}
static var customZoneID: CKRecordZoneID {
return CKRecordZoneID(zoneName: "DogsZone", ownerName: CKCurrentUserDefaultName)
}
static var recordType: String {
return "Dog"
}
}
extension Dog: CKRecordRecoverable {
static func objectFrom(record: CKRecord) -> Object? {
guard let id = record[.id] as? String,
let age = record[.age] as? Int,
let name = record[.name] as? String,
let isDeleted = record[.isDeleted] as? Bool
else { return nil }
let dog = Dog()
dog.id = id
dog.age = age
dog.name = name
return dog
}
}
var syncEngine: SyncEngine<Dog>?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
...
syncEngine = SyncEngine<Dog>()
application.registerForRemoteNotifications()
...
}
That's all you need to do! Everytime you write to Realm, the SyncEngine will get notified and handle sync stuffs!
For more details, clone the project to see the source code.
Yep, we highly recommend you use Soft Deletions. That's why we add a isDeleted
property to CKRecordConvertible protocol.
When you want to delete a object, you just need to set its isDeleted
property to true. And the rest of things are already taken care of.
You also don't need to worry about the clean-up things. It has also been considered.
Example project is provided to see the detailed usage.
It's true that debugging CloudKit is hard and tedious. But I have some tips for you guys when facing puzzles.
Warning: If you're going to launch your app onto App Store, don't forget to deploy your environment settings to production. You can do it easily in the CloudKit Dashboard. Write & Read permissions are also need to be considered.
How to debug CloudKit in production mode? See this post.
To run the example project, clone the repo, and run pod install
from the Example directory first.
IceCream is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'IceCream'
My app 小目标 - 量化你的进步(A lightweight task management app) is using IceCream. You can download it and try it on your muiltiple devices to see this magic.
If your app has adopted IceCream, feel free to raise a PR to add to this page.
Crypto currency donation is the best. Even 0.01 BTC helps
14J7KCR2x1Csh52SpPAvMWRh9EyNX5kxhE
Lisk is my preferred crypto currency.
Lisk Deposit Address: 10081270051711082114L
IceCream is available under the MIT license. See the LICENSE file for more info.