I was curious how the code for @ObservedResults
handled exceptions when writing and found the following code by clicking “Jump to definition”:
private func write<Value>(_ value: Value, _ block: (Value) -> Void) where Value: ThreadConfined {
let thawed = value.realm == nil ? value : value.thaw() ?? value
if let realm = thawed.realm, !realm.isInWriteTransaction {
try! realm.write {
block(thawed)
}
} else {
block(thawed)
}
}
Is it safe to use try! realm.write
?
Throws
An NSError if the transaction could not be completed successfully. If block throws, the function throws the propagated ErrorType instead.
What would be an example of a transaction that couldn’t complete (without the block throwing an error)? (Is it simply because of an error while writing to a file)