I receive the following warning:
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)
}
})
}
func saveAndCloseWithCompletionHandler(_ onCompletion :((Bool) -> Void)?)
{
FTCLSLog("Doc: Save and Close");
self.prepareForClosing();
self.saveDocument { (saveSuccess) in
if(saveSuccess) {
self.closeDocument(completionHandler: { (_) in
onCompletion?(saveSuccess);
});
}
else {
onCompletion?(saveSuccess);
}
}
}
func saveDocument(completionHandler : ((Bool) -> Void)?)
{
if(self.openPurpose == .read) {
FTLogError("Doc Saved in Readonly");
completionHandler?(true);
return;
}
if(self.hasAnyUnsavedChanges) {
(self.delegate as? FTNoteshelfDocumentDelegate)?.documentWillStartSaving(self);
}
FTCLSLog("Doc: Save");
#if !NS2_SIRI_APP
self.recognitionCache?.saveRecognitionInfoToDisk(forcibly: true);
if(self.hasAnyUnsavedChanges) {
if let cache = self.recognitionCache, let cachePlist = cache.recognitionCachePlist() {
let mutableDict = NSMutableDictionary.init(dictionary: cachePlist.contentDictionary);
self.recognitionInfoPlist()?.updateContent(mutableDict);
}
if let cache = self.recognitionCache, let cachePlist = cache.visionRecognitionCachePlist() {
let mutableDict = NSMutableDictionary.init(dictionary: cachePlist.contentDictionary);
self.visionRecognitionInfoPlist()?.updateContent(mutableDict);
}
//This was added in version 6.2, when we removed the bounding rect from the Segment level storage.
updateDocumentVersionToLatest()
}
#endif
super.save { (success) in
if(success) {
self.previousFileModeificationDate = self.fileModificationDate;
let pages = self.pages();
for eachPage in pages {
eachPage.isDirty = false;
}
}
completionHandler?(success);
}
}
func closeDocument(completionHandler: ((Bool) -> Void)?)
{
FTCLSLog("Doc: Close");
#if !NS2_SIRI_APP
self.recognitionCache?.saveRecognitionInfoToDisk(forcibly: true)
#endif
super.close { (success) in
self.removeObservers();
completionHandler?(success);
}
}
Need fix to avoid -
SWIFT TASK CONTINUATION MISUSE: saveAndClose() leaked its continuation!
and to return result properly after executing all items of for loop.