Skip to content

Commit 67e8c5c

Browse files
Attempt to workaround crash #1034
1 parent 7593e05 commit 67e8c5c

File tree

2 files changed

+13
-9
lines changed

2 files changed

+13
-9
lines changed

ChatSecure/Classes/Controllers/FileTransferManager.swift

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -155,14 +155,16 @@ public class FileTransferManager: NSObject, OTRServerCapabilitiesDelegate {
155155

156156
// Resume downloads, i.e. look for media items that are partially downloaded and retry getting them. TODO - use ranges
157157
@objc public func resumeDownloads() {
158-
connection.asyncRead { (transaction) in
159-
transaction.enumerateUnfinishedDownloads({ (mediaItem, stop) in
160-
if let downloadMessage = mediaItem.parentObject(with: transaction) as? OTRDownloadMessage, downloadMessage.messageError == nil {
161-
self.internalQueue.async {
162-
self.downloadMedia(downloadMessage)
158+
connection.asyncRead { [weak self] (transaction) in
159+
let unfinished = transaction.unfinishedDownloads()
160+
self?.internalQueue.async {
161+
for mediaItem in unfinished {
162+
if let downloadMessage = mediaItem.parentObject(with: transaction) as? OTRDownloadMessage,
163+
downloadMessage.messageError == nil {
164+
self?.downloadMedia(downloadMessage)
163165
}
164166
}
165-
})
167+
}
166168
}
167169
}
168170

ChatSecure/Classes/Controllers/YapDatabaseTransaction+ChatSecure.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,18 +148,20 @@ public extension YapDatabaseReadTransaction {
148148

149149
public extension YapDatabaseReadTransaction {
150150

151-
@objc public func enumerateUnfinishedDownloads(_ block:@escaping (_ mediaItem:OTRMediaItem,_ stop:UnsafeMutablePointer<ObjCBool>) -> Void) {
151+
public func unfinishedDownloads() -> [OTRMediaItem] {
152152
guard let secondaryIndexTransaction = self.ext(SecondaryIndexName.mediaItems) as? YapDatabaseSecondaryIndexTransaction else {
153-
return
153+
return []
154154
}
155+
var unfinished: [OTRMediaItem] = []
155156
let queryString = "Where \(MediaItemIndexColumnName.transferProgress) < 1 AND \(MediaItemIndexColumnName.isIncoming) == 1"
156157
let query = YapDatabaseQuery(string: queryString, parameters: [])
157158
secondaryIndexTransaction.enumerateKeysAndObjects(matching: query) { (key, collection, object, stop) in
158159
if let download = object as? OTRMediaItem {
159-
block(download, stop)
160+
unfinished.append(download)
160161
} else {
161162
DDLogError("Non-media item object in downloads index \(object)")
162163
}
163164
}
165+
return unfinished
164166
}
165167
}

0 commit comments

Comments
 (0)