Last active
May 20, 2016 11:29
-
-
Save ErikLuimes/0d6f99dfac64ce88b43e134064565222 to your computer and use it in GitHub Desktop.
Realm DataStore
This file contains hidden or 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
| import Foundation | |
| import RealmSwift | |
| public struct DataStore | |
| { | |
| public static let sharedInstance: DataStore = DataStore() | |
| private (set) var writeQueue = dispatch_queue_create("nl.mayainteractive.project.realm.write", DISPATCH_QUEUE_SERIAL) | |
| private (set) var mainRealm = try! Realm() | |
| public init() | |
| {} | |
| private func performWriteBlock(writeBlock: (backgroundRealm:Realm) -> Void) | |
| { | |
| dispatch_async(self.writeQueue) { | |
| autoreleasepool { | |
| do { | |
| let realm = try Realm() | |
| try realm.write() { | |
| writeBlock(backgroundRealm: realm) | |
| try realm.commitWrite() | |
| } | |
| } catch let error as NSError { | |
| NSLog("DB Error: \(error.localizedDescription)") | |
| } catch { | |
| NSLog("DB Error: Generic Error") | |
| } | |
| } | |
| } | |
| } | |
| public func retrieveSomething() -> Results<SomeDomainModel> | |
| { | |
| return self.mainRealm.objects(SomeDomainModel.self) | |
| } | |
| public func storeSomething(something: SomeDomainModel) | |
| { | |
| performWriteBlock | |
| { | |
| (backgroundRealm) in | |
| backgroundRealm.add(something, update: true) | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment