• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Paul Clapham
  • Devaka Cooray
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Liutauras Vilda
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Piet Souris
Bartenders:
  • salvin francis
  • Carey Brown
  • Frits Walraven

Optimising ANR's and Threading in java

 
Ranch Hand
Posts: 207
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I have an app which is a chat based application. In which I inflate different layout's in a recyclerview to show different type of chat messages.

problem : There are multiple Anr's I am getting due to a an operation which happens on UI thread. I need to optimise this thread.

The purpose of this thread is to update recycler view at an interval of  1s.(it mainly updates if any new message came.)

Questions which I have

1. As it is a critical operation will it be fine if I shifts this thread operations to happen in background?
2. How to debug ANR's  and fix them end to end?




This thread is creating problem I have concluded it through trace of ANR'S as all of the ANR'S starting point is this thread only.

Thanks in advance. Small guidance will be a great help. I am new with threading concept + ANR's
 
Saloon Keeper
Posts: 6385
158
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The important thing is not to do anything time-consuming on the UI thread. We can't tell whether either setData or populateChat is time-consuming, but if either can take more than a second to complete, they should definitely run in the background. And possibly much less than s second. Anything that can lead to network access should never run on the UI thread. So the code may be have to be broken into two steps: 1) gather all the data you want to display (this should run in the background) and 2) display the data (this should run on the UI thread).
 
priyanshi bhardwaj
Ranch Hand
Posts: 207
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Moores wrote:The important thing is not to do anything time-consuming on the UI thread. We can't tell whether either setData or populateChat is time-consuming, but if either can take more than a second to complete, they should definitely run in the background. And possibly much less than s second. Anything that can lead to network access should never run on the UI thread. So the code may be have to be broken into two steps: 1) gather all the data you want to display (this should run in the background) and 2) display the data (this should run on the UI thread).




In setData()

1. From Db(Realm) I fetch my latest messages
2. Notifies the adapter that data has changed
3. Create  adapter object (if adapter is null)
4. Few if else statements which operates on data fetched from db

Populate Chat()

1. If recycler view layout manager is null then I assign it again
2. Assigns adapter(if adapter is null)


How can I break this code?
 
Tim Moores
Saloon Keeper
Posts: 6385
158
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In setData, steps 1 (in particular) and step 4 sound like they should run in the background. With step4 it depends on how much work it does; I can't really tell from what you said about it.

populateChat doesn't sound lengthy, and since it works with the UI, it can probably run entirely in the UI thread.
 
priyanshi bhardwaj
Ranch Hand
Posts: 207
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Moores wrote: step 4 sound like they should run in the background. With step4 it depends on how much work it does; I can't really tell from what you said about it.



Step 4 are normal UI changes , Some boolean variables get's set  to true.
 
priyanshi bhardwaj
Ranch Hand
Posts: 207
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Moores wrote:In setData, steps 1 (in particular) and step 4 sound like they should run in the background. With step4 it depends on how much work it does; I can't really tell from what you said about it.

populateChat doesn't sound lengthy, and since it works with the UI, it can probably run entirely in the UI thread.





And mainly I am getting this error If you can help me with stack trace:

 
Tim Moores
Saloon Keeper
Posts: 6385
158
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That has nothing to do with UI thread vs background thread. Apparently the fragment handling is incorrect. Not seeing the relevant code excerpts, there's nothing we can advise on that (except to check fragment handling, obviously).
 
priyanshi bhardwaj
Ranch Hand
Posts: 207
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Moores wrote:That has nothing to do with UI thread vs background thread. Apparently the fragment handling is incorrect. Not seeing the relevant code excerpts, there's nothing we can advise on that (except to check fragment handling, obviously).




I know it has nothing do with UI and background thread. But crash which is being logged on crashlytics is this only.
 
Tim Moores
Saloon Keeper
Posts: 6385
158
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Moores wrote:Not seeing the relevant code excerpts, there's nothing we can advise on that (except to check fragment handling, obviously).

 
I don't like that guy. The tiny ad agrees with me.
Two software engineers solve most of the world's problems in one K&R sized book
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic