Just a simple question, Can a garbage collection happen while a method is being executed? As I believe a piece of code is worth a thousand words so here we go:
public void start() {
// view is an instance of WeakReference<View> and is not null
if (view.get() != null) {
fetchFeaturedProducts(view.get().getCategory());
// Some work
// More work
// And more work
//
// Garbage collections happens, now view.get() is null
//
// Is it possible?
// If yes, then I think there is now way to get around it other then checking right before dereferencing?
// Or am I wrong and being paranoid?
// Or is there a sophisticated way to resolve this issue other than using Kotlin?
fetchProducts(view.get().getCategory(), Manufacturer.All_COMPANIES, Tarteeb.TARTEEB_NONE, true);
}
}
Really need your expert advices. Thank you :)
Edited:
In case that this is possible, I am thinking of creating a dummy class that extends WeakReference
and overriding get
method to check for null
there. And if it is null
, return a dummy object of View
that does nothing..
But I get a feeling that this is more of a hack than a solution. Any better ideas?