GO FURTHER, FASTER: Try the Swift Career Accelerator today! >>

SOLVED: How do I create a List Section based on an empty related field in SwiftData?

Forums > SwiftUI

I have two related data models in SwiftData: Group and Task.

On the Task model, Group is optional so I want a list where all the tasks without a group are in a section and then a section with each group and the tasks associated with it.

I've got the second part with the groups/tasks figured out but every way I've tried to get the tasks without a group doesn't work.

Here is the latest iteration that seems like the closest:

MODELS

@Model
class Group: Identifiable {
    var id = UUID()
    var name: String
    var status: String
    @Relationship() var tasks = [Task]()

    init(name: String, status: String, tasks: [Task] = [Task]()) {
        self.name = name
        self.status = status
        self.tasks = tasks
    }
}

@Model
class Task: Identifiable {
    var id = UUID()
    var name: String
    var notes: String = ""
    var status: String
    @Relationship(inverse: \Group.tasks) var group: Group?

    init(name: String, notes: String = "", status: String, group: Group? = nil) {
        self.name = name
        self.notes = notes
        self.status = status
        self.group = group
    }
}

VIEW

                    ForEach(tasks){ task in
                        if task.group.isEmpty == true {
                            Text("I am a test")
                        }
                    }

That works fine if I use task.notes.isEmpty but as soon as I try to use the related field I get these errors:

Cannot convert value of type '[Task]' to expected argument type 'Binding<C>'
Generic parameter 'C' could not be inferred

I also tried using a query to get the tasks but even though I know I have some tasks without a group association, I don't get any results so clearly my query is flawed somehow.

      @Query(filter: #Predicate<Task> { task in
        task.group?.name == ""
    }) var freeTasks: [Task]

                      ForEach(freeTasks){ task in
                        Text(task.name)
                    }

Any suggestions? I don't care how I go about it, as long as it works :)

   

Can you explain me in full detail. Thank You. vdowni. vdowni.com

   

hi,

asking Task.notes.isEmpty makes sense: it asks if a String is an empty String.

but instead of asking

if task.group.isEmpty == true

you should be asking

if task.group == nil

task.group is neither a String nor an Array type. it's a simple reference. there's only (at most) one Group associated with a Task.

hope that helps,

DMG

1      

I had this exact same question and I tried the same options with isEmpty and a query :) Thanks for the solution! Is it possible to take it one layer deeper?

My model has 3 levels: Project, Group, Task. If I use just the suggested code I get all tasks without a group across all projects. I tried narrowing it with if task.project?.sections == nil but that causes an error in the sections above that line.

MODEL:

@Model
class Project: Identifiable {
    var id = UUID()
    var name: String = ""

    @Relationship(deleteRule: .cascade)
    var groups: [Group]?
    var tasks: [Task]?

    init(name: String) {
        self.name = name
    }
}

@Model
class Group: Identifiable {
    var id = UUID()
    var name: String = ""
    @Relationship(inverse: \Project.sections) var project: Project?
    @Relationship() var tasks: [Task]?

    init(name: String) {
        self.name = name
    }
}

@Model
class Task: Identifiable {
    var id = UUID()
    var name: String = ""
    @Relationship(inverse: \Project.tasks) var project: Project?
    @Relationship(inverse: \Group.tasks) var section: Group?

    init(name: String, section: Group? = nil) {
        self.name = name
        self.group = group
    }
}

Any ideas?

   

Go further, faster with the Swift Career Accelerator.

GO FURTHER, FASTER Unleash your full potential as a Swift developer with the all-new Swift Career Accelerator: the most comprehensive, career-transforming learning resource ever created for iOS development. Whether you’re just starting out, looking to land your first job, or aiming to become a lead developer, this program offers everything you need to level up – from mastering Swift’s latest features to conquering interview questions and building robust portfolios.

Learn more here

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

Reply to this topic…

You need to create an account or log in to reply.

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.