Merge commit 'fe9a32d1b99253de2e4b0b38653333317aa7e35a' into p2p

pull/20/head
Mikunj 5 years ago
commit ed85a0da3c

@ -1206,8 +1206,6 @@ static NSTimeInterval launchStartedAt;
{
OWSAssertIsOnMainThread();
OWSLogInfo(@"storageIsReady");
[LokiAPI loadSwarmCache];
[self checkIfAppIsReady];
}

@ -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 }
}
}

@ -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 {

Loading…
Cancel
Save