|
|
|
@ -10,19 +10,25 @@ import Foundation |
|
|
|
|
|
|
|
|
|
final class RealmStore { |
|
|
|
|
private let queue: DispatchQueue |
|
|
|
|
private let config: Realm.Configuration |
|
|
|
|
private let realm: Realm |
|
|
|
|
|
|
|
|
|
public init(queue: DispatchQueue = DispatchQueue(label: "RealmStore.syncQueue", qos: .background), realm: Realm) { |
|
|
|
|
public init(queue: DispatchQueue = DispatchQueue(label: "com.RealmStore.syncQueue", qos: .background), realm: Realm) { |
|
|
|
|
self.queue = queue |
|
|
|
|
self.realm = realm |
|
|
|
|
self.config = realm.configuration |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func performSync(_ callback: (Realm) -> Void) { |
|
|
|
|
dispatchPrecondition(condition: .notOnQueue(queue)) |
|
|
|
|
queue.sync { [unowned self] in |
|
|
|
|
autoreleasepool { |
|
|
|
|
let realm = self.realm.threadSafe |
|
|
|
|
callback(realm) |
|
|
|
|
if Thread.isMainThread { |
|
|
|
|
callback(realm) |
|
|
|
|
} else { |
|
|
|
|
dispatchPrecondition(condition: .notOnQueue(queue)) |
|
|
|
|
queue.sync { [unowned self] in |
|
|
|
|
autoreleasepool { |
|
|
|
|
let realm = try! Realm(configuration: config) |
|
|
|
|
callback(realm) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|