3

Iam running this procedure to download some data and insert them into the database. The total procedure takes around 5 minutes. I noticed that while downloading, when the phone locks the screen and open it after 5 minutes, it will still downloading. It seems when locked download procedure slows down. Is there any explanation?

The execution time also slows down when pressing home button and becomes a background process, not only when sreen locks.

Thank you

public abstract class AppDatabase extends RoomDatabase {

private static AppDatabase sInstance;

@VisibleForTesting
public static final String DATABASE_NAME = "Database_db";

public abstract CustomerDao repoCustomer();


public static AppDatabase getInstance(Context context) {

    if (sInstance == null) {
        synchronized (AppDatabase.class) {
            if (sInstance == null) {
                sInstance = Room.databaseBuilder(context.getApplicationContext(), AppDatabase.class, DATABASE_NAME).build();

            }
        }
    }
    return sInstance;
}


public void downloadCustomers(final String table){

        executors.diskIO().execute(new Runnable() {
            @Override
            public void run() {
               //download data and insert into database.
             });
        }  
}
Nick
  • 2,818
  • 5
  • 42
  • 60

1 Answers1

1

I believe it is something related with power management. Have you tried using a wake lock?

To test if that is your problem, simply add android:keepScreenOn="true" to the layout of the activity where the thread is started.

If it solves the problem and you don´t need the screen on, consider reading this thread:

https://developer.android.com/training/scheduling/wakelock

To aquire a wakelock you must add this to your manifest:

<uses-permission android:name="android.permission.WAKE_LOCK" />

And set manually the wake lock:

val wakeLock: PowerManager.WakeLock =
        (getSystemService(Context.POWER_SERVICE) as PowerManager).run {
            newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MyApp::MyWakelockTag").apply {
            acquire()
        }
    }

To manually release it, you can do it with:

wakelock.release()

Also, from the same source and it seems to me that this can be applied to your problem, check this out:

Before adding wakelock support to your app, consider whether your app's use cases support one of the following alternative solutions:

"If your app is performing long-running HTTP downloads, consider using DownloadManager. If your app is synchronizing data from an external server, consider creating a sync adapter. If your app relies on background services, consider using JobScheduler or Firebase Cloud Messaging to trigger these services at specific intervals."

Hope it helps.

Curious Mind
  • 659
  • 10
  • 26
  • The execution time also slows down when pressing home button and becomes a background process, not only when sreen locks. – Nick Sep 13 '18 at 14:27
  • Not yet.. But what about when going to background? – Nick Sep 13 '18 at 15:45
  • 2
    If the cpu goes to sleep the execution takes longer than you expect... I believe both situations (locking the phone and pressing home button) have the same root cause, which is powermanager. Just try it. – Curious Mind Sep 13 '18 at 15:49
  • Tried the wake lock. Still slow when the app goes in the background – Nick Sep 13 '18 at 16:58
  • 1
    This helped me with the issue !! Definitely working :) – gunjot singh Aug 09 '19 at 20:01