Skip to content

Commit 56dd55b

Browse files
committed
prevent ANRs on background app event
Move calls to methods that do disk I/O from the main thread to a new background thread. There are reports where this can cause a significant amount of time on main thread. Any disk I/O is a risk on the main thread and should not be done as there is no upper bond of how long it can take. ANRs trigger after 5 seconds of the main thread being busy. The stacktrace of reported ANRs confirm that appStopped then calls getSessionInfluences which can result in waiting on disk I/O. We have also seen stacktraces with scheduling a AndroidX worker so that was moved into this background thread as well. I isn't known if it does any disk I/O but it was significant enough to show up in some ANRs
1 parent 0a313f2 commit 56dd55b

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

OneSignalSDK/onesignal/src/main/java/com/onesignal/ActivityLifecycleHandler.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,14 @@ private void handleLostFocus() {
179179
if (focusHandler == null || focusHandler.hasBackgrounded() && !focusHandler.hasCompleted())
180180
return;
181181

182-
OneSignal.getFocusTimeController().appStopped();
183-
focusHandler.startOnLostFocusWorker(FOCUS_LOST_WORKER_TAG, SYNC_AFTER_BG_DELAY_MS, OneSignal.appContext);
182+
new Thread() {
183+
public void run() {
184+
// Run on it's own thread since both these calls do disk I/O
185+
// which could contribute a significant amount to ANRs.
186+
OneSignal.getFocusTimeController().appStopped();
187+
focusHandler.startOnLostFocusWorker(FOCUS_LOST_WORKER_TAG, SYNC_AFTER_BG_DELAY_MS, OneSignal.appContext);
188+
}
189+
}.start();
184190
}
185191

186192
private void handleFocus() {

0 commit comments

Comments
 (0)