SWIFT TASK CONTINUATION MISUSE: saveAndClose() leaked its continuation!
Inside for loop, after executing few items, at one of the item, in save and close function, it is blocked, and says above error, and not moving to next item, so not able to return result.
What could be wrong here, and is there any way to optimize any of these snippets?
private func processTags(reqItems: [FTShelfItemProtocol], selectedTags: [String]) async throws -> FTShelfTagsResult { let items: [FTDocumentItemProtocol] = reqItems.filter({ ($0.URL.downloadStatus() == .downloaded) }).compactMap({ $0 as? FTDocumentItemProtocol })
var totalTagItems: [FTShelfTagsItem] = [FTShelfTagsItem]()
for case let item in items where item.documentUUID != nil {
guard let docUUID = item.documentUUID else { continue }//, item.URL.downloadStatus() == .downloaded else { continue }
let destinationURL = FTDocumentCache.shared.cachedLocation(for: docUUID)
print(destinationURL.path)
// move to post processing phace
do {
let document = await FTNoteshelfDocument(fileURL: destinationURL)
let isOpen = try await document.openDocument(purpose: FTDocumentOpenPurpose.read)
if isOpen {
let tags = await document.documentTags()
let considerForResult = selectedTags.allSatisfy(tags.contains(_:))
if considerForResult && !tags.isEmpty {
var tagsBook = FTShelfTagsItem(shelfItem: item, type: .book)
tagsBook.tags = tags
totalTagItems.append(tagsBook)
}
}
let tagsPage = await document.fetchSearchTagsPages(shelfItem: item, selectedTags: selectedTags)
totalTagItems.append(contentsOf: tagsPage)
_ = await document.saveAndClose()
} catch {
cacheLog(.error, error, destinationURL.lastPathComponent)
}
}
cacheLog(.success, totalTagItems.count)
let result = FTShelfTagsResult(tagsItems: totalTagItems)
return result
}
func saveAndClose() async -> Bool {
return await withCheckedContinuation({ continuation in
self.saveAndCloseWithCompletionHandler { isSuccess in
continuation.resume(returning: isSuccess)
}
})
}