Attempt to fix background crash in a different way
This commit is contained in:
parent
ee38b3f1ae
commit
d039ecd87e
|
@ -120,26 +120,27 @@ public final class LokiAPI : NSObject {
|
|||
}.map { Set($0) }.retryingIfNeeded(maxRetryCount: maxRetryCount)
|
||||
}
|
||||
|
||||
public static func getDestinations(for hexEncodedPublicKey: String) -> Promise<[Destination]> {
|
||||
var result: Promise<[Destination]>!
|
||||
public static func getDestinations(for hexEncodedPublicKey: String) -> Promise<Set<Destination>> {
|
||||
var result: Promise<Set<Destination>>!
|
||||
storage.dbReadWriteConnection.readWrite { transaction in
|
||||
result = getDestinations(for: hexEncodedPublicKey, in: transaction)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
public static func getDestinations(for hexEncodedPublicKey: String, in transaction: YapDatabaseReadWriteTransaction) -> Promise<[Destination]> {
|
||||
public static func getDestinations(for hexEncodedPublicKey: String, in transaction: YapDatabaseReadWriteTransaction) -> Promise<Set<Destination>> {
|
||||
// All of this has to happen on DispatchQueue.global() due to the way OWSMessageManager works
|
||||
let (promise, seal) = Promise<[Destination]>.pending()
|
||||
let (promise, seal) = Promise<Set<Destination>>.pending()
|
||||
func getDestinations(in transaction: YapDatabaseReadTransaction? = nil) {
|
||||
func getDestinationsInternal(in transaction: YapDatabaseReadTransaction) {
|
||||
var destinations: [Destination] = []
|
||||
let masterHexEncodedPublicKey = storage.getMasterHexEncodedPublicKey(for: hexEncodedPublicKey, in: transaction) ?? hexEncodedPublicKey
|
||||
let masterDestination = Destination(hexEncodedPublicKey: masterHexEncodedPublicKey, kind: .master)
|
||||
destinations.append(masterDestination)
|
||||
let deviceLinks = storage.getDeviceLinks(for: masterHexEncodedPublicKey, in: transaction)
|
||||
let slaveDestinations = deviceLinks.map { Destination(hexEncodedPublicKey: $0.slave.hexEncodedPublicKey, kind: .slave) }
|
||||
destinations.append(contentsOf: slaveDestinations)
|
||||
let query = YapDatabaseQuery(string: "WHERE \(DeviceLinkIndex.slaveHexEncodedPublicKey) = ? OR \(DeviceLinkIndex.masterHexEncodedPublicKey) = ?", parameters: [ hexEncodedPublicKey, hexEncodedPublicKey ])
|
||||
let deviceLinks = Set(DeviceLinkIndex.getDeviceLinks(for: query, in: transaction))
|
||||
var destinations = Set(deviceLinks.flatMap {
|
||||
return [ Destination(hexEncodedPublicKey: $0.master.hexEncodedPublicKey, kind: .master), Destination(hexEncodedPublicKey: $0.slave.hexEncodedPublicKey, kind: .slave) ]
|
||||
})
|
||||
if destinations.isEmpty {
|
||||
destinations.insert(Destination(hexEncodedPublicKey: hexEncodedPublicKey, kind: .master))
|
||||
}
|
||||
seal.fulfill(destinations)
|
||||
}
|
||||
if let transaction = transaction, transaction.connection.pendingTransactionCount != 0 {
|
||||
|
|
|
@ -29,8 +29,13 @@ public extension OWSPrimaryStorage {
|
|||
}
|
||||
|
||||
public func getDeviceLinks(for masterHexEncodedPublicKey: String, in transaction: YapDatabaseReadTransaction) -> Set<DeviceLink> {
|
||||
let query = YapDatabaseQuery(string: "WHERE \(DeviceLinkIndex.masterHexEncodedPublicKey) = ?", parameters: [ masterHexEncodedPublicKey ])
|
||||
return Set(DeviceLinkIndex.getDeviceLinks(for: query, in: transaction))
|
||||
let collection = getDeviceLinkCollection(for: masterHexEncodedPublicKey)
|
||||
var result: Set<DeviceLink> = []
|
||||
transaction.enumerateRows(inCollection: collection) { _, object, _, _ in
|
||||
guard let deviceLink = object as? DeviceLink else { return }
|
||||
result.insert(deviceLink)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
public func getDeviceLink(for slaveHexEncodedPublicKey: String, in transaction: YapDatabaseReadTransaction) -> DeviceLink? {
|
||||
|
|
|
@ -995,11 +995,11 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException";
|
|||
BOOL isFriendRequestMessage = [message isKindOfClass:LKFriendRequestMessage.class];
|
||||
BOOL isSessionRequestMessage = [message isKindOfClass:LKSessionRequestMessage.class];
|
||||
[[LKAPI getDestinationsFor:contactID]
|
||||
.thenOn(OWSDispatch.sendingQueue, ^(NSArray<LKDestination *> *destinations) {
|
||||
.thenOn(OWSDispatch.sendingQueue, ^(NSSet<LKDestination *> *destinations) {
|
||||
// Get master destination
|
||||
LKDestination *masterDestination = [destinations filtered:^BOOL(LKDestination *destination) {
|
||||
return [destination.kind isEqual:@"master"];
|
||||
}].firstObject;
|
||||
}].allObjects.firstObject;
|
||||
// Send to master destination
|
||||
if (masterDestination != nil) {
|
||||
TSContactThread *thread = [TSContactThread getOrCreateThreadWithContactId:masterDestination.hexEncodedPublicKey];
|
||||
|
@ -1012,7 +1012,7 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException";
|
|||
}
|
||||
}
|
||||
// Get slave destinations
|
||||
NSArray *slaveDestinations = [destinations filtered:^BOOL(LKDestination *destination) {
|
||||
NSSet *slaveDestinations = [destinations filtered:^BOOL(LKDestination *destination) {
|
||||
return [destination.kind isEqual:@"slave"];
|
||||
}];
|
||||
// Send to slave destinations (using a best attempt approach (i.e. ignoring the message send result) for now)
|
||||
|
|
Loading…
Reference in New Issue