Heena Arora Heena Arora - 5 months ago 28
Android Question

Database operations in IntentService results into application halt,become unresponsive and giving ANR

I am using an IntentService in a alarm manager to trigger it after every 15 seconds.
I have to continuously send large amount data to server and receiving large amount of data in response in background.
I have to follow beneath process :


  1. I am reading data from Database through queries.

  2. Then converting it in Json through POJO architecture.

  3. Sending this JSON in request to server using Retrofit Library.

  4. Receiving data in response.

  5. Inserting this data to my database through certain queries if any updation in the database.



Is there any alternate approach? As i am facing ANR.
If data is less, its working fine. But as data size is becoming large, UI halts and Application becomes unresponsive.

Answer

After a lot of struggle finally i got a solution :

I simply used separate process to work for the intent-service.

using this in "Manifest" file

 <service android:name=".BackgroundSyncDataService"
        android:process=":my_process">
  </service>

Description :

The colon prefix in front of the name tells Android that the Service is private to its declaring application. If the colon is not used, the Service would be a global process and can be used by other Android applications.

Running a service in its own process gives it its own memory address space and a garbage collector of the virtual machine in this process does not affect the application process.

Running a service in its own process will not block the application in case the service performs long running operations in its main thread.