It's not an issue, but rather just a general question on how realm collection change notifications are dispatched if changes happen in background thread.
So there is a scenario (in Realm Cocoa):
RLMResultsof objects of classFoo: RLMObjectis fetched from default realm. The list is then transformed into array[Foo]and saved as variable in view controller (does not matter)- Table displays that list of foo objects
- Some objects are deleted on background thread
- User scrolls the table view, cell is reused, object from array at index x is accessed, but it was deleted on background thread and crash happens, because
object was deleted or invalidated. As expected.
To solve that we could use collection notifications and refresh the list when changes occur. As I tried everything works as expected, but isn't it possible that dispatch of cell reuse will occur before notification is dispatched on main thread so that cell setup method will be using invalidated object?
Just tried to explain the question as detailed as possible.
Mainly the question is about situation(s) when data sync with server (not using Realm's mobile platform) is happening on background thread and views, whether they're table cells or any other views, are holding references to could-be-deleted objects. Is it a good practice to check if object was invalidated when trying to do something with the object because it could be deleted on background thread.
I see a couple of solutions:
- each time accessing reference of
RLMObjectsubclass object check if it is not invalidated - wrap the object into view model (leaving all the good parts of self-updating model features) which then leaves with another two solutions when trying to change the model:
- save object id in view model so that when trying to change it would be possible to fetch object again
- have a reference to the object itself having the same problems as the first point
What are the suggestions for this?
EDIT:
Using Results and List sometimes not really possible if the object Foo is complicated. For example when opening details screen of Foo table view with lots of different cells accessing properties of Foo class' object. So on notification about deleted object screen could be dismissed, but as user scrolls isn't it possible that cell could be accessing invalidated object?
Maybe the question is just stupid, over engineered about raise conditions, but I'm curious if it's possible that dispatches on main thread will occur something like: object becomes invalidated (I don't know how that happens in realm internally> then some code which access that object then notification about invalidated object