I'm using RxJava and Realm database in an android project. But sometimes pressing a button is unresponsive and you have to do that many times for it to work sometime, and android log is saying xxx frame skipped. I know it has something to do with misusing UI thread. Here's some of my request, can someone tell me what's wrong with them? Realm wants me to perform IO request on the same thread I'm using the response(not too sure though).
public Flowable<List<ClothingItem>> getClothingItemsLocal() {
return Flowable.just(dbProvider.getClothingItems(mSortType));
}
public Flowable<List<ClothingItem>> getClothingItemsRemote() {
return clothingService.getAll("Bearer " + preferencesManager.getToken())
.map(response -> response.items)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.doOnSuccess(clothingItems -> {
dbProvider.clearClothingItems();
dbProvider.saveOrUpdateClothingItems(clothingItems);
})
.toFlowable()
.map(remoteItems -> dbProvider.getClothingItems(mSortType));
}
public Flowable<ClothingItem> getClothingItem(@NonNull final String id) {
return getClothingItemRemote(id)
.startWith(dbProvider.getClothingItem(id))
.onErrorReturn(throwable -> dbProvider.getClothingItem(id));
}
getAll method with retrofit.
@GET(BuildConfig.BASE_API_PATH + "clothing_items")
Single<GetClothingItemsResponseModel> getAll(@Header("Authorization") String token);
Realm provider methods:
public void saveOrUpdateEvents(List<Event> data) {
realmInstance.executeTransaction(realm -> {
for (Event event : data) {
if (!TextUtils.isEmpty(event.date)) {
Date date = DateUtils.getFullDate(event.date);
Timber.d("date %s", date.toString());
event.timestamp = date;
}
Event cashedEvent = getEvent(event.id);
if (cashedEvent.id != null) {
event.eventClothingItems = cashedEvent.eventClothingItems;
event.tags = cashedEvent.tags;
event.location = cashedEvent.location;
}
}
realm.delete(Event.class);
realm.insertOrUpdate(data);
});
}
public void clearClothingItems() {
realmInstance.executeTransaction(realm -> {
realm.delete(ClothingItem.class);
});
}