Why is the order of my array random when i use the @Model macro.
class TestModel {
var name: String?
var array: [TestModel2]
init(name: String = "") {
self.name = name
array = []
}
}
class TestModel2 {
var name: String?
init(name: String = "") {
self.name = name
}
}
This works fine, and all the items in array are in the order I add them.
But if I declare both of them as @Model, like this:
@Model
class TestModel {
var name: String?
var array: [TestModel2]
init(name: String = "") {
self.name = name
array = []
}
}
@Model
class TestModel2 {
var name: String?
init(name: String = "") {
self.name = name
}
}
The array items are always in a random order. When I reload the view where they are displayed or when I add items to the array the order is randomised.
Is this a beta bug? Or is this intended?
Post
Replies
Boosts
Views
Activity
I want to sync an object between iPhone and Apple Watch during a workout session, with the startMirroringToCompanionDevice function. It works fine if the class is no model, but as soon as I declare them as @Model, I get the problem that the JSON decoder creates a new Object. Now, SwiftData tries to insert these objects again.
Is there another way to sync SwiftData objects?
Or is there a way to create an object, which consists of multiple @Model objects, without the need to insert it instantly into the model context?