-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Change listener not being triggered on realm updates #1676
Comments
Hi @Wattos |
Hi @Wattos |
@nhachicha Do you think that the code is really necessary? I did a quick search in the realm-java repository and the notifyChangeListener method is only called 3 times. (See https://github.com/realm/realm-java/search?utf8=%E2%9C%93&q=notifyChangeListeners) None of these method calls would indicate that the change listeners are called on updates in realm |
I have encountered similar issue with when at times change listener is not being triggered even once for the same query, but is triggered when I query it again.
|
From this discussion seems like anonymous listeners should be fine for |
@Wattos I tried to reproduce your case, and it looks like it is working as intended. If you can provide some sample code where this is not happening it would be really helpfull. |
I have the similar problem and I'm pretty sure it happens only with queries on related models, i.e. .equalsTo("related.id", id). RealmResults is never updated. Listener actually fires twice (first immediately after findAllAsync I guess, second time after data has been written to Realm from network thread), but no data is present in RealmResults! After something else updates Realm, listener fires again and data appears. But, if I remove condition on related model, everything works as it should. |
@Wattos I discovered that you are right. It seems that our change listeners doesn't get triggered for our synchronous queries and objects. I have updated #1696 with the unit tests to catch this. /cc @nhachicha |
@cmelchior I've created test app to showcase my problems: https://dl.dropboxusercontent.com/u/11334864/master.zip |
fixed in |
This issue still exists in Version ( I have this method which is called by background thread by this lib: public void saveImageDownloaded(boolean isDownloaded){
RealmManager realmManager = null;
try{
realmManager=new RealmManager();
realmManager.updateImageDownloaded(isDownloaded,imageDatabaseType, imageName);
}finally {
if(realmManager!=null)realmManager.close();
}
} RealmManager.java: ...
realm=Realm.getDefaultInstance();
...
public void updateImageDownloaded(...) {
realm.executeTransactionAsync(new Realm.Transaction() {
@Override
public void execute(Realm bgRealm) {
ModelRealm_ImageDestination model = bgRealm.where(ModelRealm_ImageDestination.class)
.equalTo("imageName", imageName)
.findFirst();
if (model != null)
model.setDownloaded(isDownloaded);
}
});
}
...
public void close() {
if (realm != null) {
realm.close();
}
} Problem occurs in exact above calling order, which is not quite right, because i run async call in Problem does not occur in this case: public void saveImageDownloaded(boolean isDownloaded){
RealmManager realmManager = null;
try{
realmManager=new RealmManager();
realmManager.updateImageDownloaded(isDownloaded,imageDatabaseType, imageName);
}finally {
///if(realmManager!=null)realmManager.close();
}
} Problem does not occur in this case public void saveImageDownloaded(boolean isDownloaded){
RealmManager realmManager = null;
try{
realmManager=new RealmManager();
//realmManager.updateImageDownloaded(isDownloaded,imageDatabaseType, imageName);
}finally {
if(realmManager!=null)realmManager.close();
}
} |
You should consider closing the Realm instance only AFTER the transaction is completed |
I am aware of close() is called too early, so it should give error like "realm cannot be used after realm instance is closed.." if async op tries to do sth after .close(). But this above code breaks my RealmManager in every screen, even if i create new realm instance and try to use it. I corrected my method like this: public void saveImageDownloaded(boolean isDownloaded){
new Thread(new Runnable() {
@Override
public void run() {
android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_BACKGROUND);
RealmManager realmManager = null;
try{
realmManager=new RealmManager();
realmManager.updateImageDownloadedSynch(isDownloaded,imageDatabaseType, imageName);
}finally {
if(realmManager!=null)realmManager.close();
}
}
}).start();
}
My point is above buggy code should break only one screen, but it breaks whole app until i restart |
Define "breaks whole app until restart" |
realm.getDefaultInstance() does not work in any screen, for .findAllAsync(), changeListener does not get notified |
@jemshit I can imagine that occurring if you access the Realm from multiple processes. By the way, you should consider updating to 1.1.1, it's much more stable than 1.1.0 |
classpath "io.realm:realm-gradle-plugin:3.5.0"
Listener isn't called after saveCarDetails
|
@GitHubMurt Please create a new issue with all details. |
@GitHubMurt you did not include |
I've created new issue as cmelchior suggested |
According to the documentation:
"If you want to be notified when the query completes and the RealmResults is updated, you can register a RealmChangeListener. This listener will be called every time the RealmResults is updated to reflect the latest changes in the Realm (usually after a commit)"
I add a change listener to either a RealmObject or RealmResult, but that only gets triggered once (after the data is loaded). The documentation states that this listener should also be triggered when the real result is updated. That does not seem to be the case.
Is the documentation incorrect or do I need to do something special besides commiting a transaction so that the listener gets triggered?
The text was updated successfully, but these errors were encountered: