I have an activity where I build an instance of fetch in onCreate() then use that instance through out the Activity.
Finally onDestroy() I call fetch.close(); This works fine the Activity destroyed and fetch instance is closed. But my phone freezes to about 9 seconds and usually with an ANR Application Not Responding after that then it works fine. But during the freeze NO button NOR input responds you know.
I struggled with it then I comment off //fetch.close(); and when I left onDestroy(); the activity everything works great and No any ANR's. I also could not leave the code that way since when a user reenter the activity onCreate(); fails with an exception
FetchException Namespace already exist for this instance of Fetch, did you forget to close...
So I am stuck with this... What can solve this?
@Xenolion What version of Fetch are you using? Just released RC10. Please try the latest version of Fetch and let me know if you are experiencing the same issues.
I have just upgraded to RC10 now, but the error keeps on, Screen freezes!
Hi @Xenolion I've implemented the same logic as you for my application (terminating my Fetch instances in the on destroy method). I have two POJOs that contain a fetch instance each. In my onDestroy I run the terminateDownloadObjects method that does the following:
try {
applicationUpdateObject.getFetch().removeAll();
if (!applicationUpdateObject.getFetch().isClosed()) {
applicationUpdateObject.getFetch().close();
}
}
catch (Exception e) {
e.printStackTrace();
}
try {
contentUpdateObject.getFetch().removeAll();
if (!contentUpdateObject.getFetch().isClosed()) {
contentUpdateObject.getFetch().close();
}
}
catch (Exception e) {
e.printStackTrace();
}
When the application starts again I face no issues.
I will try this looks good. But I also have another question can I retrieve Fetch instance if I know its Namespace? Or can get the list of all downloads within Fetch in my app? @Sergios21 @tonyofrancis
@Xenolion When you create a new instance of Fetch with a namespace (say "default") only one instance of Fetch with that namespace can be active. If you close the instance of Fetch with the namespace (say "default") and create a new instance of Fetch("default") again all the previous downloads and settings for that namespace are maintained.
As for closing Fetch and encountering errors. I am going to perform more test. There should not be a need to users of the library to add try catch causes to close Fetch.
Ooooh I have got you know I did not know that the same namespace can retrieve the available downloads! Thanks for this and Thanks for Fetch.
All the best in fixing the close-fetch bug.
@Xenolion @Sergios21 found the issue with closing Fetch and ANR. Working on a fix. Hope to release it soon.
Thanks. We are waiting, Fetch Forever @tonyofrancis
@Xenolion This is not a simple problem to fix. It will take some time. In the meantime use the solution @Sergios21 suggested. I do not have an estimated time on the fix. I will report back here once the fix is in place.
No worries! I did not mean I am waiting for my current project. I am waiting for future use. Actually I redesign my project. I now use a service that has a fetch object and so my activity just binds to it and access the downloads. This way the screen does not freeze in my activity. Though I am facing a challenge. How to stop a service when there is no actual downloading progressing or retry them when the device comes online! @tonyofrancis @Sergios21
You need to implement a connectivity change receiver to listen to connectivity changes. This way you can queue all the files that need to be downloaded in your Fetch instance. When the connectivity is lost you can pause Fetch and when it's back on you can resume your downloads. You don't need to stop your service while your app is executing just control what is happening in your service and terminate it when your app is terminated.
A connectivity change receiver is a broadcast receiver that is registered to listen to connectivity changes in the following manner. In you activity onCreate:
ConnectivityChangeReceiver receiver = new ConnectivityChangeReceiver();
registerReceiver(
receiver,
new IntentFilter(
ConnectivityManager.CONNECTIVITY_ACTION));
where ConnectivityChangeReceiver is a class that extend a BroadcastReceiver and all the changes are handled in the onReceive method that you override from it's superclass.
Actually I can make a BroadCast Receiver and and register it in Manifest. But I was just hoping hoping for a lazy short cut for that in Fetch @Sergios21 . But now will make a broadcast receiver. And I think this library is growing! And It may be a big deal in the Future. Is there a stackoverflow tag yet for fetch2?? @tonyofrancis This will save time and answering non- bug issues that are fetch2 specific. You can just do this by providing a link in the fetch2 description!! Or can I do that? @tonyofrancis
@Xenolion @Sergios21 Fetch RC9 and RC10 already have enableRetryOnNetworkGain(true). Please see the Fetch.Builder class. Fetch already monitors network connectivity, to start downloads once network connection is gained/reconnected. Just don't close the Fetch instance for it to work. @Xenolion I will create the stackOverflow tag soon :).
Thanks and best of luck too, Waiting for the tag! :) @tonyofrancis .
@Xenolion @Sergios21 RC11 is now live with the close fixes. Please check it out and let me know if you guys encounter any issues. @Xenolion I have added the stack-overflow link to the repository https://meta.stackoverflow.com/tags/fetch2
Yeah the ANR is solved. Now fetch.close(); works okay. Thanks to you! About the tag, I will activate it by asking a question soon! Also the link does not have to use meta because it is something else. Put www or nothing. @tonyofrancis
The bug is solved! Now I can close Fetch instance without any error! @tonyofrancis
Most helpful comment
@Xenolion @Sergios21 found the issue with closing Fetch and ANR. Working on a fix. Hope to release it soon.