realm-java icon indicating copy to clipboard operation
realm-java copied to clipboard

working with RealmObject properly

Open dotgc opened this issue 11 years ago • 7 comments

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?

dotgc avatar Jan 09 '15 18:01 dotgc

Hi @ImGauravC Currently you have two solutions:

  1. Add a RealmChangeListener and reload the object whenever something changes.

  2. 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?

cmelchior avatar Jan 12 '15 12:01 cmelchior

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.

dotgc avatar Jan 13 '15 11:01 dotgc

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.

cmelchior avatar Jan 13 '15 11:01 cmelchior

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 ...

dotgc avatar Jan 14 '15 16:01 dotgc

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?

cmelchior avatar Jan 14 '15 20:01 cmelchior

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

dotgc avatar Jan 14 '15 21:01 dotgc

@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.

cmelchior avatar Jan 19 '15 05:01 cmelchior