diff --git a/Signal/src/AppDelegate.m b/Signal/src/AppDelegate.m index 1801d86cd..5f9be61a3 100644 --- a/Signal/src/AppDelegate.m +++ b/Signal/src/AppDelegate.m @@ -1204,8 +1204,6 @@ static NSTimeInterval launchStartedAt; { OWSAssertIsOnMainThread(); OWSLogInfo(@"storageIsReady"); - - [LokiAPI loadSwarmCache]; [self checkIfAppIsReady]; } diff --git a/SignalServiceKit/src/Loki/API/LokiAPI+SwarmAPI.swift b/SignalServiceKit/src/Loki/API/LokiAPI+SwarmAPI.swift index e99b26862..6c09db7ad 100644 --- a/SignalServiceKit/src/Loki/API/LokiAPI+SwarmAPI.swift +++ b/SignalServiceKit/src/Loki/API/LokiAPI+SwarmAPI.swift @@ -11,21 +11,20 @@ public extension LokiAPI { private static let swarmCacheKey = "swarmCacheKey" private static let swarmCacheCollection = "swarmCacheCollection" - fileprivate static var swarmCache: [String:[Target]] = [:] - - @objc public static func loadSwarmCache() { - var result: [String:[Target]]? = nil - storage.dbReadConnection.read { transaction in - let intermediate = transaction.object(forKey: swarmCacheKey, inCollection: swarmCacheCollection) as! [String:[TargetWrapper]]? - result = intermediate?.mapValues { $0.map { Target(from: $0) } } + fileprivate static var swarmCache: [String:[Target]] { + get { + var result: [String:[Target]]? = nil + storage.dbReadConnection.read { transaction in + let intermediate = transaction.object(forKey: swarmCacheKey, inCollection: swarmCacheCollection) as! [String:[TargetWrapper]]? + result = intermediate?.mapValues { $0.map { Target(from: $0) } } + } + return result ?? [:] } - swarmCache = result ?? [:] - } - - private static func saveSwarmCache() { - let intermediate = swarmCache.mapValues { $0.map { TargetWrapper(from: $0) } } - storage.dbReadWriteConnection.readWrite { transaction in - transaction.setObject(intermediate, forKey: swarmCacheKey, inCollection: swarmCacheCollection) + set { + let intermediate = newValue.mapValues { $0.map { TargetWrapper(from: $0) } } + storage.dbReadWriteConnection.readWrite { transaction in + transaction.setObject(intermediate, forKey: swarmCacheKey, inCollection: swarmCacheCollection) + } } } @@ -41,10 +40,7 @@ public extension LokiAPI { return Promise<[Target]> { $0.fulfill(cachedSwarm) } } else { let parameters: [String:Any] = [ "pubKey" : hexEncodedPublicKey ] - return getRandomSnode().then { invoke(.getSwarm, on: $0, associatedWith: hexEncodedPublicKey, parameters: parameters) }.map { parseTargets(from: $0) }.get { swarm in - swarmCache[hexEncodedPublicKey] = swarm - saveSwarmCache() - } + return getRandomSnode().then { invoke(.getSwarm, on: $0, associatedWith: hexEncodedPublicKey, parameters: parameters) }.map { parseTargets(from: $0) }.get { swarmCache[hexEncodedPublicKey] = $0 } } } diff --git a/SignalServiceKit/src/Loki/Utilities/Promise+Hashable.swift b/SignalServiceKit/src/Loki/Utilities/Promise+Hashable.swift index 0d2ec3bd7..47bb42c62 100644 --- a/SignalServiceKit/src/Loki/Utilities/Promise+Hashable.swift +++ b/SignalServiceKit/src/Loki/Utilities/Promise+Hashable.swift @@ -3,8 +3,8 @@ import PromiseKit extension Promise : Hashable { public func hash(into hasher: inout Hasher) { - let reference = ObjectIdentifier(self).hashValue - hasher.combine(reference) + let reference = ObjectIdentifier(self) + hasher.combine(reference.hashValue) } public static func == (lhs: Promise, rhs: Promise) -> Bool {