Creating a "wrapper" AppEntity worked for me. In OP's example, this would be something like:
struct HabitatAppEntity: AppEntity, Identifiable {
let id = UUID()
let habitat: Habit
static var defaultQuery = HabitEntityQuery()
static var typeDisplayRepresentation: TypeDisplayRepresentation = "Hábito"
var displayRepresentation: DisplayRepresentation {
DisplayRepresentation(title: "\(habitat.nombre)")
}
}
Xcode 16 Beta 3 seems to work as intended, so hopefully this workaround isn't required for too long :)
Post
Replies
Boosts
Views
Activity
I decided to disable the App Sandbox here - my app was built off a legacy system that depends on being able to access relative paths.
A workaround for this is to specify a ModelConfiguration that uses a different database file for each type. My solution is based off the example code shown in wwdc2023-10196. It should be noted that the example code fails with the error "Cannot use instance member '' within property initializer; property initializers run before 'self' is available", so I had to move the ModelContainer initialisation into .modelContainer.
import SwiftUI
import SwiftData
@main
struct TestApp: App {
let fullSchema = Schema([
Item.self,
DuplicateItem.self
])
let itemModelConfiguration = ModelConfiguration(
schema: Schema([
Item.self
]),
url: URL.applicationSupportDirectory.appendingPathComponent("item.store")
)
let duplicateItemModelConfiguration = ModelConfiguration(
schema: Schema([
DuplicateItem.self
]),
url: URL.applicationSupportDirectory.appendingPathComponent("duplicateItem.store")
)
var body: some Scene {
WindowGroup {
ContentView()
}
.modelContainer(try! ModelContainer(for:
fullSchema,
itemModelConfiguration,
duplicateItemModelConfiguration)
)
}
}
I've got a minimal reproduction working.
@Model
class Item {
var test: [Int]
init(test: [Int]) {
self.test = test
}
}
@Model
class DuplicateItem {
var test: [Int]
init(test: [Int]) {
self.test = test
}
}
Internally, SwiftData makes an SQL database and adds a table called Z<MEMBER>. It tries to make this twice, and fails.
SwiftData doesn't like enums at the moment, this video shows a workaround: https://www.youtube.com/watch?v=yOqGva62nzg
Basically, store the enum as a string, and compute the enum at runtime.
Is this bug reported? I'd consider it a showstopper.