Skip to content

Instantly share code, notes, and snippets.

@maltebucksch
Created June 26, 2018 08:01
Show Gist options
  • Save maltebucksch/9b35e2d0b8db13da67f87b4ff4ac501f to your computer and use it in GitHub Desktop.
Save maltebucksch/9b35e2d0b8db13da67f87b4ff4ac501f to your computer and use it in GitHub Desktop.
Realm Sync Service: Uploadable-Extension
extension Uploadable where Self: Object {
func getId() -> String {
guard let primaryKey = type(of: self).primaryKey() else {
fatalError("Object can't be managed without a primary key")
}
guard let id = self.value(forKey: primaryKey) else {
fatalError("Objects primary key isn't set")
}
return String(describing: id)
}
func encoded(using jsonEncoder: JSONEncoder = JSONEncoder()) -> Data? {
return try? jsonEncoder.encode(self)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment