amitshekhariitbhu / Fast-Android-Networking

🚀 A Complete Fast Android Networking Library that also supports HTTP/2 🚀
https://outcomeschool.com
Apache License 2.0
5.69k stars 958 forks source link

ANRequest unregister `requestListener` #597

Closed larabear closed 1 year ago

larabear commented 1 year ago

Hi @amitshekhariitbhu! Thank you for building this great library.

I am running into memory leak issue with ANRequest#getAsJSONObject when the listener I supplied contains references to destroyed lifecycle objects. I am hoping to convert our callsite to suspend functions using [suspendCancellableCoroutine](https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/suspend-cancellable-coroutine.html) and use something continuation.invokeOnCancellation { request.unregister(callback) } in order to prevent memory leak and unnecessary callback execution when the lifecycle object is dead. However, I don't see an api to unregister requestListener in ANRequest. Do you think this would be something that is safe to add? I'm happy to help if you don't have bandwidth. Thanks!

larabear commented 1 year ago

I realized I could do request.destroy()