UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Hierarchical list navigation with Core Data - How?

Forums > SwiftUI

Total beginner here. Warning: this might be a long post, sorry!

I've been following along with the excellent tutorials and articles on this site and I feel that I'm getting somewhat of a tentative grip on SwiftUI. I'm very much still learning and thought I would ask this excellent community for help with my next step.

Now to my issue:

I want to create an iOS app, where the user can create categories and then add items to those categories (Category 1, Category 2, etc.).

I want those items to be shown in a list that uses one or more pieces of data to dynamically group the items together (Category 1 : Group 1, Category 1 : Group 2, etc.).

I then want to show the groups content in a new list. (Category 1 : Group 1: Item 1...n)

Perhaps an image will explain it better: Link to image on Google Drive

How can I achieve this? I've tried modifying the Bookworm project as well as following along with the Core Data tutorials, but I can't quite understand how to achieve what I want.

Example of user flow:

  • Create a category (in sheet): "Deadlift"
  • Navigate to category and add new item (in sheet): "Weight: 140kg | Reps: 3, | Date() or some picked date"
  • Inside the category "Deadlift" there is now a list with a navigation item "3 reps (1 item) >"
  • Tapping on "5 reps (1 item) > shows a list with "100kg at Date"
  • Go back to the "Deadlift" category
  • Add new item: "90 kg | 5 reps | Date() or some picked date)
  • The list in category "Deadlift" now contains "3 reps (1 item), 5 reps (1 item) "

Does this make sense? Is this achievable with SwiftUI and something like Core Data?

3      

Yes, this is definitely possible with SwiftUI. Your image was really helpful and makes me think you've already got a good idea of the data schema you want to use.

If you haven't already I strongly recommend you watch the Data Essentials in SwiftUI video here: https://developer.apple.com/videos/play/wwdc2020/10040/

This has great advice about how to think about SwiftUI views that you create: What data does this view need? How will it use the data? Where does the data come from?

If you break down your problem along those lines things will become simpler to reason about. For example, from your image you require 4 views, something like CategoryListView, CategoryGroupListView, CategoryGroupItemListView, CategoryGroupItemDetailView.

4      

@lordmooch Thanks! I'll have a look at the link you provided. My view setup is pretty much what you described already, so I'm happy to see that I was on the right track. Now I just need to figure out the Core Data aspect of it all.

Would you say that I need to use relationships in Core Data for this to work out the way I want, or could I solve it some other way? With filtered data perhaps?

Thanks again for taking the time to respond! Super helpful.

3      

hi,

@lordmooch has the view setup right. the Core Data setup is similar:

Entity Category <-- one to many -->> Entity Group <-- one to many -->> Entity Item

to match the views with the data:

  • the list of categories view has a @FetchRequest for the Category objects;
  • a selected Category object is passed to the list of groups view and used to access the Group objects;
  • a selected Group object is passed to the list of items view and used to access the Item objects;
  • a selected Item object is passed to the item detail view.

be sure you know how to read the relationship objects from Core Data. for example, if Category has a one to many relationship to Group named groups, it will come across as

@NSManaged var groups: NSSet?

you can read how many groups there are for a category with category.groups?.count ?? 0, and you can read the groups themselves as an Array with perhaps a helper function on Category such as

extension Category {

func groupArray() -> [Group] {
  if let setOfGroups = groups as Set<Group> {
    return setOfGroups.sort(by: { /* however you sort groups */ } )
  }
  return []
}

}

one last thing: the names Category and Group already have some meaning to Swift on SwiftUI -- you may want to use different names.

hope that helps,

DMG

4      

@delawaremathguy That was super helpful! Thanks so much for taking the time to answer my question in such a detailed manner.

3      

I have no experience with Core Data as I have never used it, so I don't want to comment on that specifically. More generally, I am an advocate of letting the persistence layer protect you from data errors. So I would use relationships and so on to ensure that the data can't be (famous last words!) corrupted no matter what happens in the UI. I think @delawaremathguy has the right idea with the relationships he defined earlier.

4      

3      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.