i want to use the cached data in realm then update the data from server using retrofit. i managed that by the following:
public void getNotifications() {
Observable.concat(getCashedNotifications(), downloadNotification())
.subscribe(new Action1<List<Notification>>() {
#Override
public void call(List<Notification> notifications) {
setSize(notifications.size() + "");
}
});
}
private Observable<List<Notification>> getCashedNotifications() {
return Observable.just(mRealm.copyFromRealm(mRealm.where(Notification.class).findAll()));
}
private Observable<List<Notification>> downloadNotification() {
return mApiHandler.createRetrofitService(NotificationServices.class)
.getNotificationByUser(10)
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.doOnNext(new Action1<NotificationResponse>() {
#Override
public void call(final NotificationResponse notificationResponse) {
setLoading(false);
mRealm.executeTransactionAsync(new Realm.Transaction() {
#Override
public void execute(Realm realm) {
realm.copyToRealmOrUpdate(notificationResponse.getResult().getData().getNotifications());
}
});
}
})
.map(new Func1<NotificationResponse, List<Notification>>() {
#Override
public List<Notification> call(NotificationResponse notificationResponse) {
if (notificationResponse.getResult() != null) {
return notificationResponse.getResult().getData().getNotifications();
} else {
return new ArrayList<>();
}
}
});
}
my problem is to get the current status like :
1- if there is no data in realm show progress
2- if there is no data and no network show error dialog
3- if there is data in realm and no network show the data from realm only
4- if there is no data in realm and no data from retrofit show no data state
any idea how to know the resuslts from concat are from ? (retrofit or realm)
what i ended up with is to edit the getNotifications method to the following
public void getNotifications() {
setNoData(false);
setLoading(false);
if (ConectivityUtils.isDeviceConnectedToNetwork(mContext)) {
if (mRealm.where(Notification.class).count() > 0) {
Observable.concat(getCashedNotifications(), downloadNotification())
.subscribe(new Action1<List<Notification>>() {
#Override
public void call(List<Notification> notifications) {
setSize(notifications.size() + "");
}
});
} else {
// show progress
setLoading(true);
downloadNotification().subscribe(new Action1<List<Notification>>() {
#Override
public void call(List<Notification> notifications) {
setLoading(false);
if (notifications.size() > 0) {
setSize(notifications.size() + "");
} else {
// no data in realm and retrofit
setNoData(true);
setErrorMessage("No data");
}
}
});
}
} else {
if (mRealm.where(Notification.class).count() > 0) {
getCashedNotifications().subscribe(new Action1<List<Notification>>() {
#Override
public void call(List<Notification> notifications) {
setSize(notifications.size() + "");
}
});
} else {
//show no network
setNoData(true);
setErrorMessage("No Network");
}
}
}
but i believe that there is better and cleaner solution than this
Related
I need when refresh page make request to API and insert getting data to my room database. But when I try to insert data I get io.reactivex.exceptions.OnErrorNotImplementedException: UNIQUE constraint failed: data.id (code 1555). So I decided to check is my table empty and if isn't make update request, but so my recyclerview doesn't work normally, and data doesn't update properly on db. Here is my code:
private void getData() {
progressBar.setVisibility(View.VISIBLE);
APIInterface service = RetrofitInstance.getRetrofitInstance().create(APIInterface.class);
Call<DataAll> call = service.getData();
call.enqueue(new Callback<DataAll>() {
#Override
public void onResponse(Call<DataAll> call, Response<DataAll> response) {
if(response.body() != null) {
List<Data> allData = response.body().getData();
Disposable disposable = db.dataDao().dataSize().subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Consumer<Integer>() {
#Override
public void accept(Integer dbSize) throws Exception {
if(dbSize > 0)
updateData(allData);
else
insertData(allData);
fillListFromDb();
}
});
}
}
#Override
public void onFailure(Call<DataAll> call, Throwable t) {
tvErrorMessage.setVisibility(View.VISIBLE);
recyclerDataList.setVisibility(View.GONE);
progressBar.setVisibility(View.GONE);
swipeRefreshToUpdateList.setRefreshing(false);
Toast.makeText(getApplicationContext(), R.string.errorMessage, Toast.LENGTH_LONG).show();
t.printStackTrace();
}
});
}
private void fillListFromDb() {
Disposable disposable = db.dataDao().getAllData().subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Consumer<List<Data>>() {
#Override
public void accept(List<Data> data) throws Exception {
listData.clear();
listData.addAll(data);
adapter = new MyAdapter(listData);
adapter.notifyDataSetChanged();
recyclerDataList.setAdapter(adapter);
progressBar.setVisibility(View.GONE);
swipeRefreshToUpdateList.setRefreshing(false);
}
});
}
private void updateData(List<Data> data) {
Completable.fromAction( () ->
db.dataDao().updateData(data))
.subscribeOn(Schedulers.io())
.subscribe();
}
private void insertData(List<Data> data) {
Completable.fromAction(() ->
db.dataDao().addData(data)).
subscribeOn(Schedulers.io())
.subscribe();
}
And onCreate method:
#Override
protected void onCreate(Bundle savedInstanceState) {
// ……
swipeRefreshToUpdateList.setOnRefreshListener(() -> {
tvErrorMessage.setVisibility(View.GONE);
recyclerDataList.setVisibility(View.VISIBLE);
getData();
});
}
Please help me
If you want an insert operation to overwrite existing object in DB, then you should use OnConflictStrategy.REPLACE.
See example:
#Dao
interface CatDao {
#Insert(onConflict = OnConflictStrategy.REPLACE)
fun insertCats(cats: List<Cat>)
You have to check you List<Data> one by one in your updateData
For exemple :
#Override
public void onResponse(Call<DataAll> call, Response<DataAll> response) {
if(response.body() != null) {
List<Data> allData = response.body().getData();
for (Data data:allData){
if(isStored(data.getId())){
updateData(data);
else {
insertData(data);
}
}
}
}
}
}
you can create a new method in your DataDao findDataById(string id) and use it the boolean isStored(String id) method and update it directly
Good luck ! hope that will help you.
What is the correct way to implement the tests below using rxjava2?
Given a list of ntp servers, test each one until you succeed.
Example:
time.nist.gov -> timeout
pool.ntp.org -> timeout
time.google.com -> success, get date
time.apple.com -> ignore
I do not want to test all in parallels but one by one. And if all fail, it restarts the test again.
Using only one server, the code I'm using is this:
public void getTime() {
timeObservable = Observable
.fromCallable(new Callable<Date>() {
#Override
public Date call() throws IOException {
return connectAndGetTime(HOST);
}
})
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.doOnError(new Consumer<Throwable>() {
#Override
public void accept(Throwable error) {
Timber.tag(TAG).e(error);
}
})
.retry(5);
timeObservable.subscribe(new Consumer<Date>() {
#Override
public void accept(Date date) {
mDate = date;
}
}, new Consumer<Throwable>() {
#Override
public void accept(Throwable throwable) {
Timber.tag(TAG).e(throwable);
}
});
}
Thanks!
Thanks Alexei, you're right.
Why complicate things?
The end result looks like this:
public void getTime() {
timeObservable = Observable
.fromCallable(new Callable<Date>() {
#Override
public Date call() {
for (String host : Arrays.asList("time.google.com", "time.apple.com", "time.nist.gov")) {
try {
return connectAndGetTime(host);
} catch (Exception e) {
Timber.tag(TAG).d("Sync (%s) fail!", host);
}
}
return null;
}
})
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.doOnError(new Consumer<Throwable>() {
#Override
public void accept(Throwable error) {
Timber.tag(TAG).e(error);
}
})
.retry(5);
timeObservable.subscribe(new Consumer<Date>() {
#Override
public void accept(Date date) {
mDate = date;
}
}, new Consumer<Throwable>() {
#Override
public void accept(Throwable throwable) {
Timber.tag(TAG).e(throwable);
}
});
}
I am trying to make webservie call using retrofit and rxjava 2. i was exploring two different approach to use RxJava2. problem is i am getting response whene i use Observable but it is not working with Flowable. Logs are not getting printed when using Flowable i tried to debug it but its not going inside onNext or onComplete or onError. only onSubscribe gets executed.
1) using observable as return type
new WebRequestManager().getContactObservable(userRequest)
.subscribe(new Observer<ResponseData>() {
#Override
public void onSubscribe(Disposable d) {
}
#Override
public void onNext(ResponseData responseData) {
Log.e(TAG , "data "+responseData.getStatus());
}
#Override
public void onError(Throwable e) {
}
#Override
public void onComplete() {
Log.e(TAG , "data complete");
}
}
);
2) Using flowable as return type
new WebRequestManager().getContactFlowable(userRequest)
.subscribe(new Subscriber<ResponseData>() {
#Override
public void onSubscribe(Subscription s) {
Log.e(TAG , "contact subscription ");
}
#Override
public void onNext(ResponseData responses) {
Log.e(TAG , "contact onNext ");
}
#Override
public void onError(Throwable t) {
}
#Override
public void onComplete() {
Log.e(TAG , "contact onComplete ");
}
});
Rest contact retrofit api
public interface ContactApi {
#POST(WebRequest.GET_CONTACTS)
Flowable<ResponseData> getContactFlowable(#Body UserRequest userRequest);
#POST(WebRequest.GET_CONTACTS)
Observable<ResponseData> getContactObservable(#Body UserRequest userRequest);
}
call to webservice
public Flowable<ResponseData> getContactsData(UserRequest userRequest){
return webRequest.getWebClient().create(ContactApi.class).getContacts(userRequest);
}
public Observable<ResponseData> getContact(UserRequest userRequest){
return webRequest.getWebClient().create(ContactApi.class).getContact(userRequest);
}
getting retrofit instance
public static Retrofit getWebClient(){
//if(okHttpClient == null)
okHttpClient = new OkHttpClient.Builder()
.connectTimeout(120,TimeUnit.SECONDS)
.readTimeout(120,TimeUnit.SECONDS)
.writeTimeout(120,TimeUnit.SECONDS)
.addInterceptor(new WebRequestInterceptor("\"application/json\""))
.build();
// if(client == null)
client = new Retrofit.Builder()
.baseUrl(BASE_URL)
.client(okHttpClient)
.addConverterFactory(LoganSquareConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build();
return client;
}
With Subscribers, you have to call request to get items:
new WebRequestManager().getContactFlowable(userRequest)
.subscribe(new Subscriber<ResponseData>() {
#Override
public void onSubscribe(Subscription s) {
Log.e(TAG , "contact subscription ");
s.request(Long.MAX_VALUE); // <---------------------------------
}
#Override
public void onNext(ResponseData responses) {
Log.e(TAG , "contact onNext ");
}
#Override
public void onError(Throwable t) {
}
#Override
public void onComplete() {
Log.e(TAG , "contact onComplete ");
}
});
See also DisposableSubscriber with its example.
I'm using rx libraries im my app to call some REST api on my server and to show the results on screen.
I'm also following the MVP design pattern. So I have a Presenter and an Interactor classes.
In MainInteractor.java I have the following method:
public Observable<Card> fetchCard(final String clientId, final CardFetchedListener listener) {
Log.i(TAG, "FetchCard method");
// Manipulate the observer
return CARDS
.doOnCompleted(new Action0() {
#Override
public void call() {
Log.d(TAG, "CARDS Completed");
}
})
.flatMap(new Func1<Card, Observable<Card>>() {
#Override
public Observable<Card> call(final Card card) {
return ResourceClient.getInstance(card)
.getIDCard()
.observeOn(AndroidSchedulers.mainThread())
.doOnError(new Action1<Throwable>() {
#Override
public void call(Throwable throwable) {
Log.w(TAG, "interactor -> fetchCard 2", throwable);
}
}
})
.flatMap(new Func1<CardMeta, Observable<Card>>() {
#Override
public Observable<Card> call(CardMeta cardMeta) {
card.setCardMeta(cardMeta);
saveOrUpdateCardToTheDb(card);
return Observable.just(card);
}
})
.doOnCompleted(new Action0() {
#Override
public void call() {
Log.d(TAG, "Completed body");
}
});
}
});
}
In the logs I can see the "Completed Body" string.
The above method is being called by MainPresenter.java class as follows:
interactor.fetchCard(clientId, this)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<Card>() {
#Override
public void onCompleted() {
Log.i(TAG, "fetchCard onCompleted");
view.hideProgressDialog();
view.updateCardsAdapter(cards);
}
#Override
public void onError(Throwable e) {
Log.e(TAG, "Fetch Card error ", e);
onFailure(parseThrowable(e));
}
#Override
public void onNext(Card card) {
if (card != null) {
Log.i(TAG, card.getTenant() + " was fetched and will be displayed");
}
}
});
The problem is that the onCompleted method in the Presenter class is never bein called. I have tried to call onCompleted myself and it worked, but the problem is I don't know actually when the observable has finished emitting cards.
What am I doing wrong here?
UPDATE
CARDS is also an observable that contains meta info. It is initialized using
Observable.from(tenants)
.filter(...).flatMap(// I'm using create operator here and it is calling its onCompleted method successflly);
Hi i used realm to save data into relam database but its not saving data to database
In my application class
Realm.init(this);
RealmConfiguration config = new RealmConfiguration.Builder().build();
Realm.deleteRealm(config);
Realm.setDefaultConfiguration(config);
In my saving activity
public void SaveUserData(final UserData userData, Realm realm)
{
final String userdata = new Gson().toJson(userData);
realm.executeTransaction(new Realm.Transaction() {
#Override
public void execute(Realm realm) {
// realm.createObjectFromJson(com.mediquick.databaseModel.UserData.class, userdata);
com.mediquick.databaseModel.UserData user = realm.createObject(com.mediquick.databaseModel.UserData.class);
user.setLname(userData.getLname());
user.setEmail(userData.getEmail());
user.setToken(userData.getToken());
user.setAddress(userData.getAddress());
user.setMobile_no(userData.getMobile_no());
user.setDob(userData.getDob());
user.setName(userData.getName());
user.setProfile_pic(userData.getProfile_pic());
user.setUser_type(userData.getUser_type());
}
});
And when i retrieve a value
public String getUserToken(Realm realm)
{
final com.mediquick.databaseModel.UserData user = realm.where(com.mediquick.databaseModel.UserData.class).findFirst();
return (user!=null)?user.getToken():null;
}
It gives null back when i retrive token from the database
Any ideas..
Declare Realm Object before onCreate function:
Realm realmDB;
Now write these line in onCreate Method:
Realm.init(this);
realmDB=Realm.getDefaultInstance();
On save button click method:
try{
realmDB.beginTransaction();
UserSchedule us = realmDB.createObject(UserSchedule.class);
us.setId(userData.get_ID());
us.setDate(userData.getDate());
realmDB.commitTransaction();
}
catch (Exception ex){
Log.d("RError",ex.toString());
Toast.makeText(this, "Error in realm", Toast.LENGTH_SHORT).show();
}
To get records:
RealmResults<UserSchedule>userSchedules=realmDB.where(UserSchedule.class).findAll();
for (UserSchedule userSchedule:userSchedules)
{
Toast.makeText(this,userSchedule.getId(), Toast.LENGTH_SHORT).show();
Toast.makeText(this, userSchedule.getId(), Toast.LENGTH_SHORT).show();
}
To delete record:
RealmResults<UserSchedule> result = realmDB.where(UserSchedule.class).findAll();
UserSchedule userSchedule = result.where().equalTo("Id", getItem(position).getS_ID()).equalTo("Date",getItem(position).getDate()).findFirst();
if(userSchedule!=null)
{
if (!realmDB.isInTransaction())
{
realmDB.beginTransaction();
}
userSchedule.deleteFromRealm();
realmDB.commitTransaction();
}
else
{
Toast.makeText(this,"No Record.", Toast.LENGTH_SHORT).show();
}
Could you try this save function:
// SAY YES TO THIS
Realm realm = null;
try { // I could use try-with-resources here
realm = Realm.getDefaultInstance();
realm.executeTransaction(new Realm.Transaction() {
#Override
public void execute(Realm realm) {
realm.insertOrUpdate(dog);
}
});
} finally {
if(realm != null) {
realm.close();
}
}
In your code you are missing call
realm.insertOrUpdate(user)
inside executeTransaction function.
public void saveData(){
realm.executeTransactionAsync(new Realm.Transaction() {
#Override
public void execute(Realm bgRealm) {
Students students = bgRealm.createObject(Students.class);
students.setName(name.getText().toString().trim());
students.setCity(city.getText().toString().trim());
students.setDegree(degree.getText().toString().trim());
students.setGender(gender.getSelectedItem().toString().trim());
}
}, new Realm.Transaction.OnSuccess() {
#Override
public void onSuccess() {
// Transaction was a success.
}
}, new Realm.Transaction.OnError() {
#Override
public void onError(Throwable error) {
// Transaction failed and was automatically canceled.
}
});
}