supergeorg / Grocy-SwiftUI

A client for Grocy for macOS and iOS, written with SwiftUI
GNU General Public License v3.0
177 stars 17 forks source link

Fix bug where HTTP requests were incorrectly cancelled #168

Open FelixSFD opened 1 year ago

FelixSFD commented 1 year ago

hopefully fixes #167


First, I changed the HTTP call from .data() to .dataTask() with a completion handler. I did this, because I couldn't find out, which exact line was throwing the exception, when the request was cancelled. Because I didn't want to change the rest of the class, I wrapped this new call inside withCheckedThrowingContinuation() to turn this into an async call. This didn't fix the issue completely, because there was a new warning in the log:

SWIFT TASK CONTINUATION MISUSE [...] leaked its continuation!

This error told me, what happened to the running tasks when navigating quickly: Something is wrong with the memory management of this class. So I switched the property grocyVM in the views from @SteteObject to @ObservedObject, which is recommended for properties that are not created inside the view, but injected instead: https://www.avanderlee.com/swiftui/stateobject-observedobject-differences/#should-i-use-stateobject-for-all-views-using-the-same-instance

FelixSFD commented 1 year ago

@supergeorg tested it in the simulator and on my iPhone 13 mini and the bug seems to be fixed without breaking anything. But I think it's a good idea, if an experienced user would test as well, because I'm quite new to Grocy and might have missed something.

supergeorg commented 1 year ago

Well, I am unsure if this is the best solution. This works because the cancellation is not implemented. This seems to work, but I have found a different way (wrapping task into task, 5b2c45b114f3565267d05636b492c56228dec201) which seems to work too.

I will check if ObservedObject is better than StateObject and will adapt the code accordingly. If my solution doesn't work, I will be happy to merge yours.