Skip to content

Instantly share code, notes, and snippets.

@hishnash
Created June 13, 2017 17:24
Show Gist options
  • Save hishnash/4b4d162389799ffcc7a5fcf8b221be54 to your computer and use it in GitHub Desktop.
Save hishnash/4b4d162389799ffcc7a5fcf8b221be54 to your computer and use it in GitHub Desktop.
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