Created
February 15, 2018 15:22
-
-
Save dodikk/e6afb7f9432f5a7039cbb0f57c458b88 to your computer and use it in GitHub Desktop.
Realm Queries on Background Queue
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
public func storeChatDetailsAsync( | |
_ item: ChatDetailsInfoType, | |
callback: @escaping GroupChatDetailsFetchCompletion) | |
{ | |
self._queue.async | |
{ | |
[weak weakSelf = self] in | |
guard let strongSelf = weakSelf | |
else | |
{ | |
callback(.error(WeakifyError())) | |
return | |
} | |
if let saveError = strongSelf.implStoreChatDetails(item) | |
{ | |
callback(.error(saveError)) | |
} | |
else | |
{ | |
callback(.success(item)) | |
} | |
} | |
} | |
private func implStoreChatDetails(_ item: ChatDetailsInfoType) -> Error? | |
{ | |
precondition(!Thread.isMainThread) | |
let realm: Realm = self._realmBuilder() | |
do | |
{ | |
try realm.write | |
{ | |
let newItem = ChatDetailsInfoRealm(withChatDetails: item) | |
realm.add(newItem, update: true) | |
} | |
return nil | |
} | |
catch | |
{ | |
return error | |
} | |
} |
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
func buildChatDetailsController() | |
{ | |
// The repo class might be created on main thread | |
let chatDetailsRepo: ChatDetailsRepoType = | |
ChatDetailsRepoRealm( | |
withRealmBuilder: appSession.getRealmStorageBuilderBlock(), | |
queue: chatDetailsDbQueue) | |
} | |
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
public typealias RealmBuilderBlock = () -> RealmSwift.Realm | |
public func getRealmStorageBuilderBlock() -> RealmBuilderBlock | |
{ | |
let result: RealmBuilderBlock = | |
{ | |
// creates a default realm | |
// | |
return Realm() | |
} | |
return result | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment