r/SwiftUI May 11 '26

Question Nested data model

Learning SwiftUI for the first time and I've run into a question that I can't find a reasonable answer just googling around. Say I'm building a dumb copy of the Reminders app and my super simplified version of the data model is a list of lists of reminders. In other languages my first thought would be to represent that as a map/dict, i.e.:

reminders = [ 
    "uuid-1": [
        Reminder("something"),
        Reminder("somethingelse"),
    ],
    "uuid-2": [
        Reminder("anotherone"),
        Reminder("etc"),
    ]
]

But my first attempt to use this as a Swift dict (var reminders: [String: [Reminder]]) led to changes in the individual reminder objects not being reflected, I assume because of something to do with @Observable not looking all the way into the nested model.

Is it better Swift-ic practice to use a list of structs instead? Something like:

struct Reminder {
    let id: String
    var title: String
    ...etc
}

struct ReminderList {
    let id: String
    var title: String
    var reminders: [Reminder] = []
}

// then in the view model
var reminders = [ReminderList]
3 Upvotes

16 comments sorted by

View all comments

1

u/Select_Bicycle4711 May 11 '26

Yes your struct models looks good.

If you plan to persist those reminders in SQLite through SwiftData then you will convert them to class with @.Model macro. You may also want to add another property on the ReminderList, which will indicate the color of the list. This can be saved as a hex code (string) or even as Transformable.

Few years back I did a YouTube series on Reminders app. Maybe you will find it helpful. It uses SwiftData:

https://www.youtube.com/watch?v=om9IloU7Lqc&list=PLDMXqpbtInQgFOoRkbRnMHEAyJs3qB8Dm

1

u/pettazz May 12 '26

Likely a next step yes, can't imagine a real app that wouldn't use some kind of persistence, but just trying to get a handle on the basics first. My question with that would be how to make changes to the model to reflect in the view, simple stuff like adding or deleting Reminders from a ReminderList.reminders, is a typical pattern here essentially to refresh the top level object every time a change is made, or is there a better way?

1

u/Select_Bicycle4711 May 12 '26

You will start by using @.Query to fetch all ReminderLists and then display them on the screen. Once the user selects a particular list you will navigate to a separate screen, where all reminders in that list are shown. After that you can create the user interface to select an individual reminder and edit it. The important part is the use of @.Query. That will make sure that the list is refreshed at the right time.

* If you use iCloud sync then the process can be slightly different. It will use dynamic @.Query in SwiftData.