Realm with RxAndroid not picking up latest data changes - android

I am using Realm with RxAndroid. i am having this strange issue where realm is not picking up the latest modification done on DB.
There are 2 methods that i am using.
Observable<Integer> save(Bitmap bitmap).
Observable<Integer> getImageList(Context applicationContext).
Like this
Activity 1
getImageList(applicationContext)
button click -> Activity 2
save(bitmap)
finish()
getImageList(applicationContext)
This method "save" basically adds a newly created model into RealmList.
private Observable<Integer> save(Bitmap bitmap) {
return Observable.create((Observable.OnSubscribe<Integer>) subscriber -> {
--------------------------------------
-----Various file creation stuff------
--------------------------------------
UserImagesModel model = realm
.where(UserImagesModel.class)
.findFirst();
//ImageModel class extends RealmObject
ImageModel imageModel = new ImageModel();
realm.beginTransaction();
//realm object must be Edited inside transaction
model.getResponse().add(0, imageModel);
realm.commitTransaction();
realm.close();
subscriber.onNext(1);
subscriber.onCompleted();
}
}
Ans this method fetches saved list.
public Observable<Integer> getImageList(Context applicationContext) {
return Observable.create((Observable.OnSubscribe<Integer>) subscriber -> {
AppUtils.logD("User image observable instance " + this);
UserImagesModel model;
Realm realm = Realm.getInstance(applicationContext);
model = realm.where(UserImagesModel.class).findFirst();
^
This model doesn't replicate data added in save call
------------------------------------------------
----Various validation and service calls.-------
------------------------------------------------
subscriber.onCompleted();
realm.close();
});
}
}
As i mentioned in code, UserImageModel that i get from Realm doesn't replicate changes i made in save method.
the problem occurs when i call getImageList method second time. also when i print this.toString inside Observable.create it prints same object that was returned first time.
So i believe this issue seems to be with the way i am using RxAndroid. can anyone tell me what i am missing? and how can i resolve it?
UPDATE :
After few tests i realized that this.toString inside Observable.create is actually points to parent object as i have used lamda expression so that is not seems to be the issue and now i am back to square one ;(

Turns out, this is expected behavior of Realm. as i was subscribing those observables on IO threads which doesn't have Looper.
Op here has similar issue. answer explains the case.

Related

Android and RxJava: insert into Room database a Retrofit Response

I have two slightly different Question classes. One is an retrofit call results object, and the other is a Room #Entity in my Android App.
And now I want from my Interactor class (Use-case) class do the following:
Make a call to the API and result (List where question is
the Retrofit response class)
On success, make a new Game object in my Room database. This operation have long (#Entity id which is autogenerated) as return
type.
for each Question from retrofit response (from (1)), question -> Converter which converts from retrofit.Question to
database.Question. Converter method takes 2 parameters, the
retrofit.Question object and the ID which was returned in step (2).
After conversion, add to database.
Observe on AndroidSchedulers.mainthread. (subscribeOn is called from repository)
Now the problem I am having is creating this stream with RxJava from my Interactor class.
Here is all the classes and calls. First is my Interactor.class method which should do the stream described above:
public Single<List<Question>> getQuestionByCategoryMultiple(String parameter);
The API CALL from MyAPI.class:
//this Question is of database.Question.
Single<List<Question>> getQuestionByCategory(String parameter);
The Room database repository.class:
Single<Long> addGameReturnId(Game game);
Completable addQuestions(List<Question> questions);
Converter.class:
public static List<database.Question> toDatabase(List<retrofit.Question> toConvert, int id);
I am having trouble creating the stream described above with these methods. I tried a mix of .flatmap, .zip, .doOnSuccess, etc without successfully creating the stream.
If there is anything else you need me to explain, or explain the problem better, please comment below.
public Single> getQuestionByCategoryMultiple(String parameters){
return openTDBType
.getQuestionByCategory(paramters) //step 1
// step 2
// step 3
.observeOn(AndroidSchedulers.mainThread()); //step 4
}
EDIT:
I tried something like this:
return openTDBType
.getQuestionByCategory(parameters)
.map(QuestionConverter::toDatabase)
.flatMap(questions -> {
int id = gameRepositoryType.addGameReturnId(new Game(parameters).blockingGet().intValue();
questions.forEach(question -> question.setqId(id));
gameRepositoryType.addQuestions(questions);
return gameRepositoryType.getAllQuestions(); })
.observeOn(AndroidSchedulers.mainThread());
^^ I don't know if this is the best way to go about this one? Can anyone confirm if this is a good way to design what I want to do here, or if there are better ways or any suggestions?
Try not use blockingGet especially when it is avoidable. Also, addQuestions won't be executed at all because it is not subscribed. You can add both addGameReturnId and addQuestions into the chain like this:
return openTDBType
.getQuestionByCategory(parameters)
.map(QuestionConverter::toDatabase)
.flatMap(questions -> {
return gameRepositoryType.addGameReturnId(new Game(parameters)) // returns Single<Long>
.map(id -> {
questions.forEach(question -> question.setqId(id));
return questions;
})
}) // returns Single<List<Question>> with the GameId attached
.flatMapCompletable(questions -> gameRepositoryType.addQuestions(questions)) // returns Completable
.andThen(gameRepositoryType.getAllQuestions()) // returns Single<>
.observeOn(AndroidSchedulers.mainThread());

Realm instance has already been closed - with RxJava2

I used Realm in conjunction with RxJava it this way:
public Flowable<List<EventEntity>> getAll() {
try (final Realm realm = Realm.getInstance(mRealmConfiguration)) {
RealmQuery<RealmEvent> query = realm.where(RealmEvent.class);
Flowable<RealmResults<RealmEvent>> result;
if (realm.isAutoRefresh()) {
result = query
.findAllAsync()
.asFlowable()
.filter(RealmResults::isLoaded);
} else {
result = Flowable.just(query.findAll());
}
return result
.unsubscribeOn(AndroidSchedulers.mainThread());
}
}
I use this chain on multiple places in app. For example:
return Observable.merge(
mEventRepository.getAll()
.toObservable(),
subjectNotificationChange
.flatMapMaybe(notification ->
mEventRepository.getAll()
.firstElement()
)
)
Problem is that I obtain exception: java.lang.IllegalStateException: This Realm instance has already been closed, making it unusable.
I looked at implementation method from of RealmObservableFactory and each call of subscribe method should create new instance of Realm. Entire situation looks as problem with references counting.
Do you know where is problem?
Java's try-with-resource closes the resource as soon as you leave the code block, but RxJava being lazy and all, only begins working when you actually subscribe, which happens after your code exits the getAll() function.
Edit: since you build a special Realm instance each time, passing configuration to it, the instance is not shared and therefore definitively closed each time.
Instead, initialize your Realm earlier using Realm.setDefaultConfiguration(config). Then, use Realm.getDefaultInstance() in your function so you access the default shared instance instead of creating a new one each time.
Edit2: the easiest solution is to keep a reference to the Realm instance:
class MyRepository {
private final Realm realm;
public MyRepository(Realm realm) {
this.realm = realm;
}
public Flowable<List<EventEntity>> getAll() {
RealmQuery<RealmEvent> query = realm.where(RealmEvent.class);
// ...
}
}
Realm realm = Realm.getDefaultInstance();
MyRepository repository = MyRepository(realm);
repository.getAll()
// ...
I find solution. It is bug in official example. When you call mentioned chain than must exist other open Realm instance for same thread. In other cases RealmResult is invalidated. Can be used solution mentioned by ESala.

RealmRecylerAdapter with RxJava

With the following code. I expect:
1. Get all the data from the server side.
2. Insert all the data to Realm
3. Refresh RecycleView with Realm Adapter.
The first time, the data is always empty. Maybe the data is still not ready but subscribe is still invoked? Is there any way to update the view when the data is ready?
ApiCall().doOnNext(response -> {
Realm realm = Realm.getDefaultInstance();
try {
realm.beginTransaction();
realm.insertOrUpdate(response);
realm.commitTransaction();
} finally {
realm.close();
}
}).subscribeOn(Schedulers.io())
.unsubscribeOn(Schedulers.computation())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new exObserver<List<T>>() {
#Override
public void onResponse(List<T> response) {
updateView()
}
#Override
public void onFailure(exException exception) {
adapter.notifyChanged();
}
});
As EpicPandaForce mentioned, you don't need to notify RealmRecyclerViewAdapter when there is a change.
My answer will specifically target your use of RxJava.
doOnNext() is a side-effect method.
This means it will be called parallel to your stream, without affecting it.
When your api call returns, the Action1 in doOnNext() and the Observer in subscribe() will both be triggered at the same time.
This means updateView() is called before the Realm transaction finishes.
If you want to update your view after inserting into the DB, your transaction must happen in your stream.
You could use flatMap() for this purpose.
RealmRecyclerViewAdapter already observes the provided data set with a RealmChangeListener, and calls adapter.notifyDataSetChanged() when a change happens in the underlying Realm.
Why are you trying to manually call adapter.notifyDataSetChanged()? The RealmRecyclerViewAdapter was specifically created so that you don't need to do that.
What is your apiCall() method? try using Observable.defer(()-> apicall()) if you're not using Retrofit. This will delay code evaluation until actual value arrive.

Accessing RealmDB from multiple Threads

I would like to know if there is any recommended practice of using RealmDB across multiple threads.
My scenario: I am looping through the records in RealmDB using one thread and doing some action. Based on the response from the previous action I would like to remove the records from another thread.
What would be best way to achieve this?
You can pass RealmObject field values (e.g. id, primaryKey) across the threads, which means that when you are done with your "action1" on the other thread, you can transfer the id(s) to the thread that is responsible for handling Realm operations, query the Object(s) which needs to be removed and delete them from Realm, you can executeTransactionAsync to further takeaway delete operation(s) from the thread where Realm is operating.
EDIT
In Realm Write operations don't block Read operations.
RealmResults & RealmObjects are LIVE objects until we close Realm instance
If you use Read operations as Observable, all further modification will be notified, If you don't want to use Observable you can also use addChange listener.
Lets have a look at some code:
Lets say in one of your class you have a Realm instance, and you are doing read operation on ThreadA (mainThread in this example)
realm.where(GitHubUser.class).findAll().asObservable()
.filter(RealmResults::isLoaded)
.filter(RealmResults::isValid)
.subscribeOn(AndroidSchedulers.mainThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(gitHubUsers -> {
for (GitHubUser gitHubUser : gitHubUsers) {
Log.e("TAG", "data = " + gitHubUser.getLogin());
}
});
And a corresponding addChangeListener version
RealmResults realmResults = realm.where(GitHubUser.class).findAll();
realmResults.addChangeListener(new RealmChangeListener<RealmResults>() {
#Override
public void onChange(RealmResults element) {
for (GitHubUser gitHubUser : gitHubUsers) {
Log.e("TAG", "data = " + gitHubUser.getLogin());
}
}
});
for (GitHubUser gitHubUser : gitHubUsers) {
Log.e("TAG", "data = " + gitHubUser.getLogin());
}
and let's say you get the trigger and want to delete one of the entry on a separate thread, what you should do is, get a new Realm instance, delete the entry as shown below and close the Realm instance.
This way you will not face any thread issue and your Read query gets a notification after you delete the entry and you can update your view with updated data.
new Thread(() -> {
Realm realm1 = Realm.getDefaultInstance();
GitHubUser gitHubUser = realm1.where(GitHubUser.class)
.equalTo("login", "loginString")
.findFirst();
if (gitHubUser != null) {
realm1.executeTransaction(realm2 -> gitHubUser.deleteFromRealm());
}
realm1.close();
}).run();

Android realm.io: Row/Object is no longer valid

This is my delete function and it does find the workday1 object:
public static void delete(Context context, Workday workday) {
Realm realm = getRealm(context);
realm.beginTransaction();
Workday workday1 = realm.where(Workday.class)
.equalTo("date", workday.getDate())
.equalTo("hours", workday.getHours())
.equalTo("minutes", workday.getMinutes())
.findFirst();
workday1.removeFromRealm();
realm.commitTransaction();
}
When it executes the removeFromRealm method it crashes:
java.lang.IllegalStateException: Illegal State: Row/Object is no longer valid to operate on. Was it deleted?
How can I fix this? Any help would be greatly appreciated.
UPDATE (I can print the content returned by the following method):
Workday workday1 = realm.where(Workday.class)
.equalTo("date", workday.getDate())
.equalTo("hours", workday.getHours())
.equalTo("minutes", workday.getMinutes())
.findFirst();
System.out.println("--------------------------------");
System.out.println(workday1.getHours());
You are trying to remove an object you have not committed to the Realm yet.
In this particular case, if for some reason you don't want to commit the object anymore, you can simply cancel the transaction.
In my case, the problem was that the same Object I was deleting was in an Adapter. After I made the Adapter extend RealmBaseAdapter the problem stopped.

Categories

Resources