Finding source for SwiftData array behaviour

Hello Apple Developer Forum,

I got the following statement from the AI model. It seems it is also reflecting my real-world experience.

  • Where do I find an official source that fully describes the array on swiftData model behaviour?
When a SwiftData model contains 
an array of value types, such as [String] or [Int], 
the array's order is preserved
Answered by DTS Engineer in 862517022

I am not completely clear what your question is, but based on the description, it seems to be related to the order of SwiftData array, and so I'd start with that.

In a SwiftData model that contains an array, the array element can be a Codable type or a SwiftData model, as shown below:

@Model
class Department {
    @Relationship(deleteRule: .cascade, inverse: \Employee.department)
    var employees: [Employee] = [Employee]() // Relationship
    
    var aliasNames: [String] = [] // `String` is Codable
    ...
}

@Model
class Employee {
    var department: Department?
    ...
}

Here employees is a SwiftData relationship, and aliasNames is an attribute of a string array.

Every time you fetch Department and access the attribute and relationship, the elemet order of department.aliasNames is the same, but that of department.employees is not. The reason is that SwiftData uses Core Data as its default store, and in Core Data, a too-many relationship is expressed as a set (NSSet), which doesn't have order. Core Data supports ordered relationships, but that is currently not available in SwiftData.

If it's the order of SwiftData to-many relationships bothering you, consider sorting the data with your own code. Also, consider filing a feedback report to voice you need.

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

I am not completely clear what your question is, but based on the description, it seems to be related to the order of SwiftData array, and so I'd start with that.

In a SwiftData model that contains an array, the array element can be a Codable type or a SwiftData model, as shown below:

@Model
class Department {
    @Relationship(deleteRule: .cascade, inverse: \Employee.department)
    var employees: [Employee] = [Employee]() // Relationship
    
    var aliasNames: [String] = [] // `String` is Codable
    ...
}

@Model
class Employee {
    var department: Department?
    ...
}

Here employees is a SwiftData relationship, and aliasNames is an attribute of a string array.

Every time you fetch Department and access the attribute and relationship, the elemet order of department.aliasNames is the same, but that of department.employees is not. The reason is that SwiftData uses Core Data as its default store, and in Core Data, a too-many relationship is expressed as a set (NSSet), which doesn't have order. Core Data supports ordered relationships, but that is currently not available in SwiftData.

If it's the order of SwiftData to-many relationships bothering you, consider sorting the data with your own code. Also, consider filing a feedback report to voice you need.

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

The question is about the content ordering of @Model array.

  • The employee case, [Employee] will behave like a set. i.e. random ordering
  • The [String] case, it will behave like a normal standard array.

Where do I find this in any documentation? Do you have a link to this?

Thanks in advance!

Finding source for SwiftData array behaviour
 
 
Q