Allan Wang10/14/2018, 12:13 AM
(T) -> Unit
, which strikes me as a bit odd as anyone can use the original variable and forget about trying to get it from the weak reference. With coroutines, I don’t think weak references are used correct? Would it stand that so long as we make the coroutine scope agree to our lifecycle, we don’t have to worry about leaking variables beyond what we would normally consider when writing synchronous code? More specifically to Android, is there a general practice for writing suspended functions dealing with
T.() -> Unit
in a utility class where context would be passed through as an argument? Is it fine that we keep a global hashmap of deferred results if the scope cancels the job when appropriate?
gildor10/14/2018, 2:32 AM
More specifically to Android, is there a general practice for writing suspended functions dealing withDepends how you use this context. If you don’t use any non-cancellable or blocking code in your suspend utility, than everything is finy. if you do, than you have not so many choices, one common practice for that, always get applicationContext instance from any passed context, so there is not possible to leak such global context, but this work not for all cases, sometimes you need component context.in a utility class where context would be passed through as an argument?
Allan Wang10/14/2018, 2:59 AM
gildor10/14/2018, 3:55 AM
given anko’s different structure.Could you show an example?
context into suspended functions, and creatingAs I said before it depends on context type (application context cannot leak) and on suspend function implementation, if all operations inside suspend function are cancelable than everything is fine even with activity context
something like RxJava’s PublishSubjectSimilar approach already available in Channels API
I can add an interface for the webview to get onclick events, then switch to the main thread to show the onclick events in the ui.But you don't need context for this task, switching of threads can be done with coroutines dispatchers
Allan Wang10/14/2018, 5:31 AM
method also checks if the weak reference still exists, so how does that differ with coroutines? My current logic all use view post runnables, Rx, and anko. The questions I have for coroutines are the same as I’d have for my current code, though as I make the transition I just want to make sure that I do everything properly.
gildor10/17/2018, 2:04 AM
and it’s not coroutine! This is just a wrapper from ExecutorService that runs on thread executor and you cannot cancel it (you cannot just stop thread, only finish it) and such task is always blocking, so, yeah you need weak reference there