Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
var connectionRef: ThreadSafeReference<NetworkConnection>? = nil
var connection: NetworkConnection? {
get {
guard let ref = self.connectionRef else {
return nil
}
self.mutex.lock()
defer {
self.mutex.unlock()
}
let realm = try! Realm()
guard let conection = realm.resolve(ref) else {
return nil // person was deleted
}
self.connectionRef = ThreadSafeReference(to: conection)
return conection
}
set {
self.mutex.lock()
defer {
self.mutex.unlock()
}
guard let value = newValue else {
self.connectionRef = nil
return
}
self.connectionRef = ThreadSafeReference(to: value)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment