Let me be more specific on it,interviewer asked me this question,if you have to download many images like 1000 thousands.how will you do that?i said, i would use Universal Image Loader,Picasso third party library then he further asked No,we do not want that.we want you to create your own Parallel or Queue thread whatever you are thinking,It should be efficient.
Now, Lets understand first what is Lazy Loading?
A little introduction of lazy loading is, it is a design pattern to defer the initialization of an object until the point at which it is needed. In simple words create objects when it is needed. Further,object on demand.
So what is the challenges in loading lots of images in ListView or GridView or RecyclerView.
1) Scrolling Blocked or Interrupted – Downloading images from server OR loading from device local storage is heavy task and it may take some time. If you load images directly in getView() method of Adapter then it blocks the UI thread and your ListView scroll will be interrupted and not smooth. You need a mechanism to load images in a worker (separate) thread and show a place holder image until the image in not downloaded and placed in memory for fast access. Remember accessing images from Hard disk may also takes some time.
2)App Heap Memory can Overflow– If we load many images in memory for faster access by ListView / Adapter, then memory (heap memory) allocated to the application might overflow and app will crash with Out of Memory (OOM) error.
3)Have to work on Recycling View can be tricky:
When image is downloaded we need to decide when to set a particular image in its ImageView of that row it is meant for. It may be possible that the ImageView object will recycle and it is given to another image and we end up showing wrong image.
So, downloading lots image lead lots of carefulness but i would suggest create Parallel Worker Thread which will perform this task.Let me give more clarity on it. If you ask me how will you do that in Android regards ,i would say use intent service and create worker threads from thread pool and let thread executor handle the worker thread.Now the whole task will happen in queue. i mean to say lets assume you have started 9 worker threads from Thread Pool,so paralleled 9 threads are executing their task and all of threads are in Thread Pool in queue .
And yes there are many other ways also to do that but if you see third party Library you will understand they are also performing this kind of task in Parallel Worker Threads.
Now, Lets understand first what is Lazy Loading?
A little introduction of lazy loading is, it is a design pattern to defer the initialization of an object until the point at which it is needed. In simple words create objects when it is needed. Further,object on demand.
So what is the challenges in loading lots of images in ListView or GridView or RecyclerView.
1) Scrolling Blocked or Interrupted – Downloading images from server OR loading from device local storage is heavy task and it may take some time. If you load images directly in getView() method of Adapter then it blocks the UI thread and your ListView scroll will be interrupted and not smooth. You need a mechanism to load images in a worker (separate) thread and show a place holder image until the image in not downloaded and placed in memory for fast access. Remember accessing images from Hard disk may also takes some time.
2)App Heap Memory can Overflow– If we load many images in memory for faster access by ListView / Adapter, then memory (heap memory) allocated to the application might overflow and app will crash with Out of Memory (OOM) error.
3)Have to work on Recycling View can be tricky:
When image is downloaded we need to decide when to set a particular image in its ImageView of that row it is meant for. It may be possible that the ImageView object will recycle and it is given to another image and we end up showing wrong image.
So, downloading lots image lead lots of carefulness but i would suggest create Parallel Worker Thread which will perform this task.Let me give more clarity on it. If you ask me how will you do that in Android regards ,i would say use intent service and create worker threads from thread pool and let thread executor handle the worker thread.Now the whole task will happen in queue. i mean to say lets assume you have started 9 worker threads from Thread Pool,so paralleled 9 threads are executing their task and all of threads are in Thread Pool in queue .
And yes there are many other ways also to do that but if you see third party Library you will understand they are also performing this kind of task in Parallel Worker Threads.
No comments:
Post a Comment