iOS Development

ios – SwiftData Filtering on a many to many relationship

Spread the love


I’m attempting to filter on a many to many relationship. So I’ve 2 fashions one is named Film and the opposite is named Style. They seem like this.

@Mannequin class Film {
    @Attribute(.distinctive) var identify: String
    @Relationship(inverse: Style.films) var genres: [Genre]?
    
    init(identify: String) {
        self.identify = identify
    }
}

@Mannequin class Style {
    @Attribute(.distinctive) var identify: String
    var films: [Movie]?
    
    init(identify: String) {
        self.identify = identify
    }
}

Now I created an inventory that has a dynamic filtering choice that may mean you can filter on the film based mostly on the style. The view code seems to be like this.

struct MovieList: View {
    @Question var films: [Movie]
    
    init() {
        _movies = Question(
            filter: #Predicate<Film> { film in
//                return film.genres?.comprises { $0.identify == "horror" } ?? false
                return true
            }
        )
    }
    var physique: some View {
        if !films.isEmpty {
            Listing {
                ForEach(films) { film in
                    Textual content(film.identify)
                }
            }
        }
        else {
            ContentUnavailableView("No Films", systemImage: "coronary heart", description: Textual content("Add some pattern films by tapping on the button."))
        }
        
    }
}

I got here throughout this text by Paul Hudson for options for frequent errors. The road commented out use to work however is now crashing the app with an error saying this.

error: SQLCore dispatchRequest: exception dealing with request: <NSSQLFetchRequestContext: 0x600003b10380> , to-many key not allowed right here with userInfo of (null)

CoreData: error: SQLCore dispatchRequest: exception dealing with request: <NSSQLFetchRequestContext: 0x600003b10380> , to-many key not allowed right here with userInfo of (null)

*** Terminating app on account of uncaught exception 'NSInvalidArgumentException', purpose: 'to-many key not allowed right here'

That is how I’m including pattern information.

struct ContentView: View {
    @Surroundings(.modelContext) var context
    
    var physique: some View {
        NavigationStack {
            VStack {
                Button {
                    let movie1 = Film(identify: "Scream (Int.random(in: 0...1000))")
                    context.insert(movie1)
                    let genre1 = Style(identify: "Horror")
                    movie1.genres = [genre1]
                } label: {
                    Textual content("Create Dummy Information")
                }
                
                MovieList()
            }
            .navigationTitle("Films")
        }
    }
}

That is additionally how I create my container for my App.

import SwiftUI
import SwiftData

@primary
struct SwiftDataFilteringApp: App {
    var physique: some Scene {
        WindowGroup {
            ContentView()
        }
        .modelContainer(for: Film.self)
    }
}

Undecided if this was wanted however attempting to be as detailed as potential. Additionally could be straightforward to duplicate by simply copying and pasting the code. Now certain if the code was working earlier than I up to date Xcode however the factor is it was working earlier than.

Leave a Reply

Your email address will not be published. Required fields are marked *