I have a set of NSOperation
s which make network requests. Imagine a case where we have a User
object in Realm and I want to make some changes and then send a PATCH
request to the server:
let operation = UpdateUserOperation(updatedUser)
Then the operation runs on a different thread so it has to resolve a thread safe reference:
class UpdateUserOperation : Operation {
var userReference : ThreadSafeReference<User>
init(_ user: User) {
userReference = ThreadSafeReference(to: user)
}
func main() {
// We're probably on a different thread so resolve the reference
let user = try! Realm().resolve(userReference)
// That invalidated `userReference` so we need to create a new
// one to use below...
userReference = ThreadSafeReference(to: user)
sendUserPatchRequest(user) { response in
// We might be in _another_ thread again :(
let realm = try! Realm()
let user = realm.resolve(userReference)
try! realm.write {
user.updateFromResponse(response)
}
}
}
}
This feels like a really unclean way to do this – re-fetching the user so many times to do a pretty simple task. It feels especially onerous because we need to re-up the thread-safe reference – they aren't re-usable. In Core Data, we'd be able to choose a single NSManagedObjectContext
to do our work in and ensure thread safety by using managedObjectContext.perform { /* ... */ }
, but that sort of functionality is unavailable in Realm.
Am I missing anything? Is there a better way to do this, or am I stuck re-fetching the object each time I need to use it?