working with RealmObject properly
Question: How to properly handle the case of an activity working with a RealmObject and a background thread writing to the same RealmObject including, potentially, deleting the object while the activity is running?
Hi @ImGauravC Currently you have two solutions:
-
Add a RealmChangeListener and reload the object whenever something changes.
-
Encapsulate access to that object with try/catch for RealmException. If it has been closed or deleted an exception will be thrown and you can act accordingly.
Do any of these two cases work in your use case?
Ok, so I have an activity with a ListView adapter which extends the RealmBaseAdapter and uses the autorefresh feature. My issue is that the background update probably causes issues with the position that I receive in the getView() of the adapter.
Usually the position index is just used by ListView to render the appropriate items that are visible on the screen, and the whole list should be redrawn when notifyDatasetChanged() is called by the RealmBaseAdapter. Do you save the position index and use it for something? If yes, you should probably override notifyDatasetChanged() so you can modify/reset as appropriate.
I don't save the position index at all. When I do Log.d(TAG, "position is: " + position) in my getView(), below is a sample output I get position is: 8 position is: 8 position is: 6 position is: 5 position is: 4 position is: 3 position is: 2 ...
Have you tried looking at our AdapterExample: https://github.com/realm/realm-java/tree/master/examples/adapterExample/src/main/java/io/realm/examples/realmadapters
I tried to reproduce your behaviour there, and it iterates the positions correctly. Can you try to compare our implementation there with yours. Perhaps you can spot something that makes a difference?
I don't see any difference in the implementation.
I've posted the relevant code in my Adapter here: http://stackoverflow.com/questions/27951923/not-getting-the-last-position-in-the-getview-on-android
@ImGauravC I am closing this as an Realm issue as we cannot reproduce it. The most likely cause is the adapter getView() code making the listview render the same position twice, which hopefully will get answered on Stack Overflow.
Feel free to reopen if you have any new information.