Thread safety of Firebase's API in Android

358 views
Skip to first unread message

Hadi Lashkari

unread,
Mar 13, 2020, 11:00:48 AM3/13/20
to Firebase Google Group

Hadi Lashkari

Mar 13, 13:09 IST

Hi

We check the calls to the `android.content.SharedPreferences#apply` method in our app and we suspicious this method is called in the main thread after we call Firebase's API in the main thread, where we believe causes some ANRs for us. We would like to know if there is any problem if we call API's methods from the background thread? What is your advice in general cases of using background thread?

Firebase version: 5.1.1@aar

Best regards,

Hadi

Hadi Lashkari

unread,
Mar 16, 2020, 11:11:24 AM3/16/20
to Firebase Google Group
I found https://groups.google.com/forum/#!searchin/firebase-talk/thread$20safe|sort:date/firebase-talk/WKVZ-vOyss0/Ie5aPTt2CQAJ thread which seems Firebase's API is thread safe. But the problem is deeper. In our app, the call to `android.content.SharedPreferences#apply` method is happening in main thread as the blow stacktrace shows, and we cannot intercept this call via the API to post it to a background thread.

apply:454, SharedPreferencesImpl$EditorImpl (android.app)
zzi
:200, zzx (com.google.android.gms.internal.measurement)
zzb
:207, zzx (com.google.android.gms.internal.measurement)
getScionFrontendApiImplementation
:98, FirebaseAnalytics (com.google.firebase.analytics)
invoke
:-1, Method (java.lang.reflect)
zzb
:30, AppMeasurement (com.google.android.gms.measurement)
zza
:17, AppMeasurement (com.google.android.gms.measurement)
getInstance
:20, AnalyticsConnectorImpl (com.google.firebase.analytics.connector)
create
:6, zzb (com.google.firebase.analytics.connector.internal)
lambda$new$0:66, ComponentRuntime (com.google.firebase.components)
get:-1, ComponentRuntime$$Lambda$1 (com.google.firebase.components)
get:55, Lazy (com.google.firebase.components)
initializeEagerComponents
:155, ComponentRuntime (com.google.firebase.components)
initializeAllApis
:553, FirebaseApp (com.google.firebase)
initializeApp
:300, FirebaseApp (com.google.firebase)
initializeApp
:264, FirebaseApp (com.google.firebase)
initializeApp
:249, FirebaseApp (com.google.firebase)
onCreate
:51, FirebaseInitProvider (com.google.firebase.provider)
attachInfo
:1917, ContentProvider (android.content)
attachInfo
:1892, ContentProvider (android.content)
attachInfo
:45, FirebaseInitProvider (com.google.firebase.provider)
installProvider
:6391, ActivityThread (android.app)
installContentProviders
:5938, ActivityThread (android.app)
handleBindApplication
:5853, ActivityThread (android.app)
access$1100
:199, ActivityThread (android.app)
handleMessage
:1650, ActivityThread$H (android.app)
dispatchMessage
:106, Handler (android.os)
loop
:193, Looper (android.os)
main
:6669, ActivityThread (android.app)
invoke
:-1, Method (java.lang.reflect)
run
:493, RuntimeInit$MethodAndArgsCaller (com.android.internal.os)
main
:858, ZygoteInit (com.android.internal.os)



Reply all
Reply to author
Forward
0 new messages