NSFetchedResultsController - KVO, UITableView, and "Tree"

I am using NSFetchedResultsController to implement KVO for my UITableView (which is obvious). I cannot understand how to use several Entities - a kind of tree structure - to be present simultaneously (ed).

Here is my setup:

  • Entity1
    • display_name
    • RelationToEntity2
  • entity2
    • display_name

Now I can get the data that should be submitted - so far so good. I want to have a single-section TableView (e.g., a flattened view) with the following structure:

  • Entity1 (entry 1)
  • Entity2 (entry 1)
  • Entity2 (Record 2)
  • ...
  • Entity1 (entry 2)
  • ...

Although this may look like something that needs to be done through sections, it is not. Both objects must be UITableViewCells. Can someone point me in the right direction to smooth without losing the actual hierarchy.

+8
ios objective-c uitableview tree key-value-observing
source share
2 answers

It looks like you need to maintain your own flattened data source. Perhaps the following will be done:

When the NSFetchedResultController tells you that a new Entity1 been added, you insert Entity1 and its associated Entity2 into say, _flattenedArray so that it looks like this:

[<Entity1>, <related Entity2>, <related Entity2>...]

Where you insert them is up to you - it pretty much comes down to:

  • build a subarray containing the new Entity1 and its associated Entity2 objects
  • decide where to insert a new subarray in _flattenedArray .
  • calling reloadData or some other means to inform tableView of new data

When deleting an Entity1 object Entity1 delete it and all subsequent Entity2 objects until you encounter the end of _flattenedArray or move to another Entity1 object. This assumes that Entity2 never a top-level object. If you need to delete only those Entity2 objects in the relation.

When Entity1 gets or loses Entity2 , you can first remove Entity1 from _flattenedArray and then reinsert it. If this is too effective, do a merge instead.

+1
source share

This is exactly what you need to use object inheritance. When retrieving a parent abstract object with a selected result controller, all child objects can be displayed in the table. Use the section ordering property to display the sections in the order you prefer. And use the second sorting property to order within sections. The Notes application does this by selecting the abstract entity of the Container and displays the "Accounts and Folders", which are child objects. These are sections on the account and the first folder β€œAll iCloud” - this is actually an account. Folders are related to the account, even if they are equal in the object inheritance tree.

0
source share

Source: https://habr.com/ru/post/651295/


All Articles