Could you please help me? I try to get some RSS feeds from the internet, save it to Roomdb, and show it in lazyColumn. For that, I use Paging 3 library and Mediator class. Data is coming from here: https://habr.com/rss/hub/webdev/ But the problem is that on my endpont there are no parameters, so I cannot specify the page that I want to Quary or page limit.
At this moment the method database.newsFeedsDao().insertFeed() is running as long as there is data (there are multiple items as you can see). Moreover, it falls with NetworkOnMainThreadException right now.
Could you please suggest how to properly manage RSS data saving to the database and showing to the user? I need let's say download 2 feeds to show, when the user scrolls to the end download and show 10 more.
class NextWebFeedsRemoteMediator(
private val nextWebFeedApiService: NextWebFeedApiService,
private val feedSourceAllocatorService: FeedSourceAllocatorService,
private val database: AppDatabase
) : RemoteMediator<Int, FeedsEntity>() {
override suspend fun load(
loadType: LoadType,
state: PagingState<Int, FeedsEntity>
): MediatorResult {
return try {
val response = CoroutineScope(Dispatchers.IO).async {
nextWebFeedApiService.fetchNextWebFeeds().execute()
}.await()
if (response.isSuccessful) {
database.withTransaction {
if (loadType == LoadType.REFRESH) {
database.newsFeedsDao().deleteAll()
}
response.body()?.channel?.items?.map {
database.newsFeedsDao()
.insertFeed(it.toFeedEntity(feedSourceAllocatorService))
}
}
}
MediatorResult.Success(
endOfPaginationReached = response.body()?.channel?.items.isNullOrEmpty()
)
} catch (e: IOException) {
MediatorResult.Error(e)
} catch (e: HttpException) {
MediatorResult.Error(e)
}
}
}
interface HabrFeedApiService {
@GET("rss/hub/webdev/")
fun fetchHabrFeeds(): Call<Rss>
}
override suspend fun getAllHabrFeeds(): Pager<Int, FeedsEntity> {
return Pager(
config = PagingConfig(pageSize = 10),
remoteMediator = HabrFeedsRemoteMediator(
habrFeedApiService = habrFeedApiService,
feedSourceAllocatorService = feedSourceAllocatorService,
database = database
),
pagingSourceFactory = {
feedDao.getHabrFeeds()
}
)
}