ios – SwiftData working with model before saving it


I have this function that fetches TVShow from the TMDB API with all of its seasons.

func fetchShow(withId id: Int, fromSeason start: Int) async throws -> Show {
    let seasonStep = 14
    let end = start + seasonStep
    let fetchedShow = try await TMDB.TVShow(id: id, start: start, end: end)
    if fetchedShow.numberOfSeasons! > end {
        let seasonLayer = try await fetchShow(withId: id, fromSeason: end)
        fetchedShow.seasons.append(contentsOf: seasonLayer.seasons)
    } else {
        return fetchedShow
    }
        return fetchedShow
}

Some shows have many seasons, and I fetch them separately, and just the new seasons go to the TV show. However, I have found that when using SwiftData with Decodable @Model, I get this error: Thread 1: EXC_BREAKPOINT (code=1, subcode=0x1c3c6203c) which I found out is just a bad way to say that I can’t modify the model object before saving it to the database. The problem is this line of code.

fetchedShow.seasons.append(contentsOf: seasonLayer.seasons)

Is there any other solution to this other than saving it before doing anything? I have a search feature, and it seems like a pretty weird idea to store every result with SwiftData.

Latest articles

spot_imgspot_img

Related articles

Leave a reply

Please enter your comment!
Please enter your name here

spot_imgspot_img