Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit df2fc40

Browse files
author
Tianhua Ran
committedAug 31, 2019
fix: maintain database may cause anr
Closes lingochamp#1152
1 parent 77c787c commit df2fc40

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed
 

‎library/src/main/java/com/liulishuo/filedownloader/download/CustomComponentHolder.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,17 @@ public FileDownloadDatabase getDatabaseInstance() {
8888
synchronized (this) {
8989
if (database == null) {
9090
database = getDownloadMgrInitialParams().createDatabase();
91-
maintainDatabase(database.maintainer());
91+
// There is no reusable thread for this action and this action has
92+
// a very low frequency, so, a new thread is simplest way to make this action
93+
// run on background thread.
94+
final String maintainThreadName =
95+
FileDownloadUtils.getThreadPoolName("MaintainDatabase");
96+
new Thread(new Runnable() {
97+
@Override
98+
public void run() {
99+
maintainDatabase(database.maintainer());
100+
}
101+
}, maintainThreadName).start();
92102
}
93103
}
94104

@@ -186,7 +196,7 @@ private static void maintainDatabase(FileDownloadDatabase.Maintainer maintainer)
186196
|| model.getStatus() == FileDownloadStatus.error
187197
|| (model.getStatus() == FileDownloadStatus.pending && model
188198
.getSoFar() > 0)
189-
) {
199+
) {
190200
// Ensure can be covered by RESUME FROM BREAKPOINT.
191201
model.setStatus(FileDownloadStatus.paused);
192202
}
@@ -275,6 +285,9 @@ private static void maintainDatabase(FileDownloadDatabase.Maintainer maintainer)
275285
FileDownloadUtils.markConverted(FileDownloadHelper.getAppContext());
276286
maintainer.onFinishMaintain();
277287
// 566 data consumes about 140ms
288+
// update by rth: different devices have very large disparity, such as,
289+
// in my HuaWei Android 8.0, 67 data consumes about 355ms.
290+
// so, it's better do this action in background thread.
278291
if (FileDownloadLog.NEED_LOG) {
279292
FileDownloadLog.d(FileDownloadDatabase.class,
280293
"refreshed data count: %d , delete data count: %d, reset id count:"

0 commit comments

Comments
 (0)
Failed to load comments.