Created
June 13, 2017 17:24
-
-
Save hishnash/4b4d162389799ffcc7a5fcf8b221be54 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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