From df2fc4068f4aa9333127a7deb38f8039067d37df Mon Sep 17 00:00:00 2001 From: Tianhua Ran Date: Tue, 27 Aug 2019 16:06:14 +0800 Subject: [PATCH] fix: maintain database may cause anr Closes #1152 --- .../download/CustomComponentHolder.java | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/library/src/main/java/com/liulishuo/filedownloader/download/CustomComponentHolder.java b/library/src/main/java/com/liulishuo/filedownloader/download/CustomComponentHolder.java index c03d2f43..0bab9b37 100644 --- a/library/src/main/java/com/liulishuo/filedownloader/download/CustomComponentHolder.java +++ b/library/src/main/java/com/liulishuo/filedownloader/download/CustomComponentHolder.java @@ -88,7 +88,17 @@ public FileDownloadDatabase getDatabaseInstance() { synchronized (this) { if (database == null) { database = getDownloadMgrInitialParams().createDatabase(); - maintainDatabase(database.maintainer()); + // There is no reusable thread for this action and this action has + // a very low frequency, so, a new thread is simplest way to make this action + // run on background thread. + final String maintainThreadName = + FileDownloadUtils.getThreadPoolName("MaintainDatabase"); + new Thread(new Runnable() { + @Override + public void run() { + maintainDatabase(database.maintainer()); + } + }, maintainThreadName).start(); } } @@ -186,7 +196,7 @@ private static void maintainDatabase(FileDownloadDatabase.Maintainer maintainer) || model.getStatus() == FileDownloadStatus.error || (model.getStatus() == FileDownloadStatus.pending && model .getSoFar() > 0) - ) { + ) { // Ensure can be covered by RESUME FROM BREAKPOINT. model.setStatus(FileDownloadStatus.paused); } @@ -275,6 +285,9 @@ private static void maintainDatabase(FileDownloadDatabase.Maintainer maintainer) FileDownloadUtils.markConverted(FileDownloadHelper.getAppContext()); maintainer.onFinishMaintain(); // 566 data consumes about 140ms + // update by rth: different devices have very large disparity, such as, + // in my HuaWei Android 8.0, 67 data consumes about 355ms. + // so, it's better do this action in background thread. if (FileDownloadLog.NEED_LOG) { FileDownloadLog.d(FileDownloadDatabase.class, "refreshed data count: %d , delete data count: %d, reset id count:"