android / architecture-components-samples

Samples for Android Architecture Components.
https://d.android.com/arch
Apache License 2.0
23.37k stars 8.28k forks source link

RemoteMediator initialize() Cannot access database on the main thread since it may potentially lock the UI for a long period of time #971

Open shivamsriva31093 opened 3 years ago

shivamsriva31093 commented 3 years ago

In the example shown on Paging3 on Paging 3 Library, database is directly accessed in initialize method to start with SKIP or INITIAL refresh. However, if I try accessing the database like that in my code, it throws "Cannot access database on the main thread since it may potentially lock the UI for a long period of time."

I had to modify the code like below to do the same thing:

` override suspend fun initialize(): InitializeAction { return coroutineScope { withContext(Dispatchers.IO) { val launchRefresh = database.news().newsItems().isNullOrEmpty()

            if (launchRefresh) InitializeAction.LAUNCH_INITIAL_REFRESH
            else InitializeAction.SKIP_INITIAL_REFRESH
        }
    }

} `

Paging Library: 3.0.0-alpha11 compileSdkVersion: 30 kotlinVersion: 1.3.61 Android Studio: 4.1.2

Tanya-jain99 commented 1 year ago

@shivamsriva31093 I am facing same issue Did you found the answer?