architecture-components-samples
architecture-components-samples copied to clipboard
RemoteMediator initialize() Cannot access database on the main thread since it may potentially lock the UI for a long period of time
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