Skip to content
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

crash when replacing items using RealmStorage.setSection, if amt items in new list don't match... #62

Closed
skydivedan opened this issue Jun 14, 2018 · 4 comments

Comments

@skydivedan
Copy link

Here's the scenario.

I have a UITableView. (so, no cells). I call RealmStorage.setSection and pass in a List. At the moment that I do this, the list has 5 items in it.
My table-view updates correctly, and draws all my cells. Great! Loving it.

At some point down the road, I need to completely replace the entire contents of my table-view. So, I pass in a new List by calling RealmStorage.setSection again. This time the list is empty. (I'll be making a call to a webservice to get some data back later, but initially, the list is empty).

This causes UITableView to crash. I get a bad index exception (trying to draw the first cell, but there shoud be no cells). So, it asks for data for index-0, but there is no data, so crash.

Thankfully, I found a workaround:
before calling setSection call realmStorage.deleteSections(IndexSet(integer: 0)) (I only have one section).

The reason this works, is because (for reasons I don't understand) calling setSection does not convince UITableView that the amount of items in the datasource has changed, but deleteSections does. I can tell this, because numberOfRowsInSection is never called when I call setSection the second time. But it does get called when I delete the section, and call setSection again.

If there's any better solution to this, I'd be happy to hear it.

@DenTelezhkin
Copy link
Owner

DenTelezhkin commented Jun 15, 2018

Hey!
Don't really know what is going on there. I suggest you adding breakpoints(or prints) in DTTableViewDataSource in func numberOfSections(in tableView: UITableView) and tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) methods and inspect what is being returned to UITableView in both cases.

@skydivedan
Copy link
Author

OK, I'll try that. But I do have a bit more information for you.

My app uses a UITabBarController. When the crash happens, the table-view isn't visible, because it's on another tab. Another VC is frontmost and visible. I just leave the observation on for the lifetime of the view-controller, and don't check whether the view is shown or hidden.

@DenTelezhkin
Copy link
Owner

Sounds similar to UICollectionView stuff i've seen couple of times, sometimes crash happens when reload of UICollectionView happens when it's not visible, but i don't know the underlying specifics of how and when UIKit handles that. Maybe it's not even related to Realm, but some specific methods of UITableView/UICollectionView.

@DenTelezhkin
Copy link
Owner

Hi!

It seems that multi-section RealmStorage currently has an unfixable design flaw with no clear way to fix it on a framework level.

However, with introduction of Diffable Datasources in iOS 13, which I think should become a de-facto standard in updating UITableView, this problem has a solution. It seems that with diffable datasources we don't need RealmStorage for Realm nor CoreDataStorage for CoreData in current form, because sections and items are built from snapshot update rather than from Storage.

At this point, i think we can consider RealmStorage and CoreDataStorage to be soft-deprecated, since there's much better solution going forward.

I encourage you to try new beta releases with support for diffable datasources in iOS 13, and hope this solution works for you.

https://github.com/DenTelezhkin/DTTableViewManager/blob/master/Guides/7.0%20Migration%20Guide.md
https://github.com/DenTelezhkin/DTCollectionViewManager/blob/master/Guides/7.0%20Migration%20Guide.md
https://github.com/DenTelezhkin/DTModelStorage/blob/master/Guides/8.0%20Migration%20Guide.md

I'm going to close this issue for now, but in any case, looking forward to any feedback.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants