5

When I'm fetching vast amount of data (in form of videos) from cache the app responds after a long delay.

How do I resolve or fix it?

Logcat: The application may be doing too much work on its main thread

Ola Ström
  • 4,136
  • 5
  • 22
  • 41
Mohammad Zeeshan
  • 4,525
  • 4
  • 12
  • 15

1 Answers1

7

While performing long running or computation heavy tasks, you should prefer doing it on a background thread (i.e, separate from the UI thread).

You will see ANRs on the app if the UI thread is kept busy for more than 5 seconds, but even for less than 5 seconds there will be a noticeable lag and delay of user actions (click, scroll) and a similar message in logs as you are seeing (Choreographer : Skipped XXX frames! The application may be doing too much work on its main thread.).

Some of the ways to such perform tasks on background thread are :

Also, be careful while updating the UI from a background thread as Android UI toolkit is not thread-safe.


Edit :

For off-loading tasks from main thread, you can also use :

Shrey Garg
  • 1,317
  • 1
  • 7
  • 17
  • thanks sir but one thing is that my app is working fine when using this background task but data will show little bet time now fastly show. – Mohammad Zeeshan Feb 11 '19 at 09:15
  • is it possible that app showup data little bet and when im scrolling down data fetch. – Mohammad Zeeshan Feb 11 '19 at 09:17
  • for such a behaviour you will have to implement something like **Pagination, Lazy loading, Android's Paging library etc** – Shrey Garg Feb 11 '19 at 09:40
  • how to use pagination or lazy loading android any idea sir? – Mohammad Zeeshan Feb 11 '19 at 14:27
  • for implementing pagination or lazy loading you can do something like this - suppose on start up, your screen shows 20 items, then you should first fetch about 30 items and then keep fetching more items subsequently. you can set the number of records to be fetched on start up and subsequent fetches depending on your usage. – Shrey Garg Feb 12 '19 at 04:13
  • ok sir but how to fetch startup screen 30 item fetch in recycler view then user scroll down and fetch 30 30 items. if it is possible then how to do it? – Mohammad Zeeshan Feb 12 '19 at 08:28
  • you can keep a service running for fetching items, along with that try using Android's LiveData (https://developer.android.com/topic/libraries/architecture/livedata) and Paging (https://developer.android.com/topic/libraries/architecture/paging) – Shrey Garg Feb 12 '19 at 11:33