-
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
Realm Notifications issue #1083
Comments
In order for notifications to work properly the receiving thread must be a Looper/Handler thread, such as the UI thread. In our repo we provide several examples showing how to use this feature. Does this help? |
Also be aware that if you are using the official guide from Google on SyncAdapters, they run them in a separate process' which Realm doesn't current support. |
I thought the add change listener works across multiple threads. Is that
|
It does work across multiple threads, but not across processes. I made an example here https://github.com/realm/realm-java/tree/cm-example-syncadapter that can hopefully provide some inspiration. See the EntryListFragment and SyncAdapter. |
So i see that you are adding and removing the ChangeListener in onAttach() and onDetach() but what exactly is triggering those to get called? It looks like you are doing the realm transaction inside of the SyncAdapter which is what I am doing, except the addChangeListener does not get called in my Fragment. |
I also noticed in the SyncAdapter much like the documentation (http://realm.io/docs/java/latest/#transaction-blocks), your transaction block does not have a copyToRealm statement, is that not required in a transaction block or is that necessary at all times with or without a transaction block? |
The mSyncStatusObserver looks like it's the only listener that received a callback from the ContentProvider update. But the Realm ChangeListener does not look like it will ever get triggered. |
Without seeing all your code it is hard to tell why it doesn't behave as expected, but the example I provided does work and the the change handler is called as expected. |
The documentation for transaction blocks So the example that you provided in the experimental directory has a On Mon, May 4, 2015 at 12:47 PM, Christian Melchior <
-Etienne Lawlor (Software Developer) |
I'm sorry if the ContentProvider confuses you. It has nothing to do with the example but was just part of the code in Google's example. My main point was showing that changes to a Realm from inside a SyncAdapter was detected by the fragment. Regarding The difference is that standalone objects are created using |
Okay so I'm all clear on the copyToRealm() and createObject() methods. I figured that the ContentProvider was not critical to showcasing a SyncSdapter working with Realm. However, I am doing something nearly identical as shown by the code snippet at the top of this issue but the changes to Realm are not being detected inside the Fragment. |
Are you able to share you source code? You can do so privately through help@realm.io if you want. Bcause without seeing what your entire application is doing it is hard to tell what could be wrong |
Hi @lawloretienne
to
While looking into this I also noticed that committing a transaction on a Thread (eg. UI thread) will currently not result in a OnRealmChanged event on the same thread. We are going to change this so the changelistener on the same thread is also going to be called as it will make updating the UI easier. |
Nice. So it now works since the SyncService is not in a separate process. What are the downsides of not having the SyncService in a separate process? Also, I am able to commit a transaction on the UI thread and the ChangeListener's onChange method gets called. I am not sure what you are referring to? |
The downside is that if you SyncService crashes it will take your application with it. My last statement was referring to that RealmChangeListeners are currently not called on the same thread that does the commit, eg. doing the below will not trigger the listener, but if you are not running into issues with this then just ignore it :)
|
Thanks for all the help!! Much appreciated. |
Hi, I'm getting a 404 when trying to access the example sync adapter code: https://github.com/realm/realm-java/tree/cm-example-syncadapter Please could you tell me its new location? |
According to the docs https://realm.io/docs/java/latest/#notifications
If you have a background thread adding data to a Realm, your UI or other threads can get notified of changes in a realm by adding a listener, which is executed when the Realm is changed (by another thread or process)
However, I have a SyncAdapter which runs on a different thread then a Fragment which sets the SyncAdapter schedule. But if I write to the realm instance in the SyncAdapter, then the ChangeListener's onChange() method which is set up in the fragment does not get called.
How can I fix this?
AccountFragment.java
AccountSyncAdapter.java
The text was updated successfully, but these errors were encountered: