简体   繁体   中英

Android: Retrofit: How to do lots of async requests correctly

I'm using retrofit 2.x in my Android client. In my special situation, I want to upload >500 picture objects (actually Files) asynchronically.

If the pictureList size is <50, then uploading all these files is no problem.

   for (Picture pic : pictureList) {
        uploadPictureAsync(pic)

But when the size goes over 100, my test device is aborting with the following error message:

Large object allocation failed: ashmem_create_region failed for 'large object space allocation': Too many open files

This seems to be caused by too many async requests.

The question is now: How can I handle such a huge amount of requests?

First of all, make sure that you have a single instance of your Retrofit service, if you use it, (instead doing lazy instantiation every time you need to do a call), either you will get "Too many open files" error.

Later, if you need to upload this quantity of images, I recommend you follow Android documentation about ThreadPoolExecutors to manage Async calls within the use of BlockingQueue.

https://developer.android.com/training/multiple-threads/create-threadpool.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM