Created
June 26, 2018 08:02
-
-
Save maltebucksch/988dc9db879bba0b25c8c01dde0694bb to your computer and use it in GitHub Desktop.
Realm Sync Service: Detecting changes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
typealias Syncable = Object & Uploadable | |
struct Update { | |
let insertions: [Syncable] | |
let modifications: [Syncable] | |
let deletedIds: [String] | |
let type: Syncable.Type | |
} | |
extension Uploadable where Self: Object { | |
... | |
static func registerNotificationObserver(for realm: Realm, callback: @escaping (Update) -> Void) -> NotificationToken { | |
let objects = realm.objects(self) | |
var objectIds: [String]! | |
return objects.observe { changes in | |
switch changes { | |
case .initial(_): | |
objectIds = objects.map { $0.getId() } | |
case .update(let collection, let deletions, let insertions, let modifications): | |
let insertedObjects = insertions.map { collection[$0] } | |
let modifiedObjects = modifications.map { collection[$0] } | |
let deletedIds = deletions.map { objectIds[$0] } | |
let update = Update(insertions: insertedObjects, modifications: modifiedObjects, deletedIds: deletedIds, type: Self.self) | |
callback(update) | |
objectIds = objects.map { $0.getId() } | |
case .error(_): | |
break | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment