Skip to content

Commit

Permalink
Merge pull request #1266 from lingochamp/fix/1152
Browse files Browse the repository at this point in the history
fix: maintain database may cause anr
  • Loading branch information
Jacksgong authored Aug 31, 2019
2 parents 2df38ac + df2fc40 commit 4601b76
Showing 1 changed file with 15 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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:"
Expand Down

0 comments on commit 4601b76

Please sign in to comment.