Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
extension RealmStorageContext {
func create<T: Storable>(_ model: T.Type, completion: @escaping ((T) -> Void)) throws {
guard let realm = self.realm else {
throw NSError()
}
try self.safeWrite {
let newObject = realm.create(model as! Object.Type, value: [], update: false) as! T
completion(newObject)
}
}
func save(object: Storable) throws {
guard let realm = self.realm else {
throw NSError()
}
try self.safeWrite {
realm.add(object as! Object)
}
}
func update(block: @escaping () -> Void) throws {
try self.safeWrite {
block()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment