BLACK FRIDAY: Save 50% on all my Swift books and bundles! >>

SOLVED: Can I write a query for two SwiftData models?

Forums > SwiftUI

I have two models that aren't related, but they both have a common object of startDate. I want to have a view that dislays data from both together and sorts chronologically by startDate. Is that even possible?

MODEL

@Model
class Event1: Identifiable {
    var id = UUID()
    var name: String = ""
    var dateStart: Date = Date()
    var dateEnd: Date = Date()

    init(name: String, dateStart: Date, dateEnd: Date) {
        self.name = name
        self.dateStart = dateStart
        self.dateEnd = dateEnd
    }
}

@Model
class Event2: Identifiable {
    var id = UUID()
    var name: String = ""
    var dateStart: Date = Date()
    var dateEnd: Date = Date()

    init(name: String, dateStart: Date, dateEnd: Date) {
        self.name = name
        self.dateStart = dateStart
        self.dateEnd = dateEnd
    }
}

I have a query for one Model but don't know how to add the other one.

                ForEach(event1!.sorted(by: {$0.dateStart.compare($1.dateStart) == .orderedAscending})){ event in }

I tried a basic &&:

                ForEach(event1 && event2.sorted(by: {$0.dateStart.compare($1.dateStart) == .orderedAscending})){ event in

But that generated nothing but errors and I am not sure what other options to try.

Thanks for any ideas.

   

@hsavi  

Is this your exact code for your models or are you leaving code out to simplify things for us?

I think the problem is how you defined two models instead of just one. Both models are 100% the same with the exception of the name. I would have designed it like so:

@Model
class Event: Identifiable {
    var id = UUID()
    var name: String = ""
    var dateStart: Date = Date()
    var dateEnd: Date = Date()

    var eventType: EventType      // EventType would be something you define to hold all types of events

  // ... initialisation and other stuff follow here 
}

This way you can add as many event types as you want and you don't have the problem of needing to merge different sets of data. If you want to show one ore more event you just use a query.

If you want to continue with your models you could create another class that reads both models and merges them in to a third data set which you can then show in your list. This will only exist in memory so something like an array of event will do. This will work for displaying merged data but will be a pain if you want more than that, like adding or changing items.

   

Excellent question. I did leave things out to simply. Each model has other parameters that are the reason I created two models instead of one. For the majority of the app they are completely independent and information is displayed on different screens.

It's just this one chronological view where I want to blend them if I can.

   

I got the answer I needed and sorted this out.

   

Save 50% in my WWDC sale.

SAVE 50% All our books and bundles are half price for Black Friday, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

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.