0

This is my definition of Model for SwiftData:

import SwiftData

@Model
class TerritoryGroup {
    var name: String?
    var territories: [Territory]?
    init(name: String) {
        self.name = name
    }
}

@Model
class Territory {
    var name: String?
    var territoryGroup: TerritoryGroup?
    init(name: String) {
        self.name = name
    }
}

Simple and nothing complicated. In the CloudKit I have 20 models, and Territory and TerritoryGroup have a few more properties saved in CloudKit. I don't know if it matters, but I just mapped two of them to check how it works. I am pretty sure on the CloudKit store there is one TerritoryGroup object and 9 Territory objects.

This is how I defined the store:

import SwiftData
import SwiftUI

@main
struct FieldService: App {
    var sharedModelContainer: ModelContainer = {
        let schema = Schema([
            TerritoryGroup.self,
            Territory.self,
        ])
        let modelConfiguration = ModelConfiguration(schema: schema, isStoredInMemoryOnly: false)

        do {
            return try ModelContainer(for: schema, configurations: [modelConfiguration])
        } catch {
            fatalError("Could not create ModelContainer: \(error)")
        }
    }()
    @UIApplicationDelegateAdaptor(ApplicationDelegate.self) var viewappDelegate
    
    var body: some Scene {
        WindowGroup {
            StartView()
        }
        .modelContainer(sharedModelContainer)
    }
}

How do I display that in my App?

import CoreLocation
import SwiftUI

struct StartView: View {
    @Environment(\.modelContext) private var modelContext
    @Query private var territoryGroups: [TerritoryGroup]
    @Query private var territories: [Territory]
    var body: some View {
        List{
            ForEach (territoryGroups) { item in
                Text(item.name ?? "abc")
            }
            ForEach (territories) { item in
                Text(item.name ?? "def")
            }
        }
    }
}

And the result is:

enter image description here

So simply it displays default values, not the real ones. Why?

Of course I have my .xcdatamodeld in project. I am not sure if it is needed any more, but it is. Of course with all 20 Entities

5
  • did you change anything in the models properties since you setup your project. That is, without proper migration. Commented Apr 8 at 23:32
  • In the model? What do you mean by Model? Of course I have created only 2 SwiftData Models, but in CloudKit there is a lot more, and in xcdatamodeld file, too. Is it the reason? Commented Apr 9 at 6:22
  • 1
    So this is an old Core Data project that is now using SwiftData? Unrelated (maybe not?) but if you have a relationship then it's best to use the @Relationship property wrapper. Commented Apr 9 at 6:54
  • Yes, it is old core data project, that is now using SwiftData. Will it work if I define only 2 of 20 entities and not with all properties defined before? Commented Apr 9 at 12:04
  • What I mean from my comment did you change anything in the models properties since you setup your project is clearer now. You want to use a new @Model class with fewer properties than your original stored data. AFAIK, you cannot declare a new @Model class that only maps to a subset of the stored properties. Use full original @Model class and only read properties you need, ignore the rest. Commented Apr 11 at 23:55

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.