YapDatabase
YapDatabase copied to clipboard
Using YapDatabaseViewMappings with complex groupSortBlock
Spinoff of issue #89 :
The YapDatabaseViewMappings.groupSortBlock isn't currently equipped to properly handle changes in the sort order of groups. That is, if the set of groups doesn't change, but their sort order does, this is not properly handled by YDB mappings/changes.
@ERussel commented in the original issue:
- Create mapping for UITableView using initWithGroupBlock:sortBlock:view.
- Implement sorting by comparing first objects of groups.
- Create fixed range for every group to show only first object.
- For example, we have 2 groups A and B. And on start we see first section for A group then for B.
- Insert object to the group B in such way to reorder sections in table view
- In result, generates 2 row changes: insert change for first section and delete for second section.
But it should be both changes for first section. It happens because insert change gets right final section and delete change wrong one(because gets it from original mapping).
Insert object to the group B in such way to reorder sections in table view
This is not currently supported. That is, the groupSortBlock of YapDatabaseViewMappings is only (currently) designed to support sorting new groups that get added to the View. For example, you have a view with groups @{ "A", "G", "X" }, and a new group "H" gets added to the view. Then mappings will use the groupSortBlock to find the correct position of "H".
However, it is not currently equipped to support inserting a new object into the existing "G" group, and then expecting the mappings to re-sort the groups as @{ "G", "A", "X" }.
I believe this may be possible to implement though.
@robbiehanson Ah I think currently I am experiencing a problem similar to this in #105. I would like to trigger a sorting "refresh" on significant distance changes, and the only way that I can find to do this is to unregister and then re-register the view with the new origin location.
Did anything happen with regards to this bug yet? I'm working on a song book, having a song database where the song metadata contains a timestamp for 'last recently played'. I can't find a good way to update the view after bumping the timestamp (metadata) for one song object. Unregistering and reregistering fails here with a tableview assertion. Can we have a refresh
API for YapDatabaseView
or is there a better way for this particular scenario? At the end of the day, I just need the 'sorting' block to rerun.
Or is using a full-fledged view probably overkill here? I might just directly query the database in viewWillAppear
…? But then I won‘t get the nice differential animations.
I'm using the above patch now. It's probably still heavier than it would have to be (since after all I only change the sorting group), but it's still better IMHO to use a view than directly querying the db.