iOS 18.1 and SeiftData

After installing iOS 18.1 and iPados 18.1 we get a consuste failure when trying to add to our one to many data model. This was working well until we installed 18.1

when trying to add a entry to the many relationship we get this error

Illegal attempt to map a relationship containing temporary objects to its identifiers.

Answered by doodahdayz2 in 801400022

The solution to this was that prior to iOS 18 if you had a one to many relationship in your SwiftData models that adding to the array in the "one" data model entity would automatically update the "many" data item so that its optional attribute that pointed back to the data item containing the array of "many" items when you added items to the array.

In iOS 18 now this is no longer true and if you don't set that optional attribute to explicitly point to the data model that will contain the array of "many" items then you will see the aforementioned error when you try to append the new item to that array. Hopefully this makes sense and will help someone down the road.

Accepted Answer

The solution to this was that prior to iOS 18 if you had a one to many relationship in your SwiftData models that adding to the array in the "one" data model entity would automatically update the "many" data item so that its optional attribute that pointed back to the data item containing the array of "many" items when you added items to the array.

In iOS 18 now this is no longer true and if you don't set that optional attribute to explicitly point to the data model that will contain the array of "many" items then you will see the aforementioned error when you try to append the new item to that array. Hopefully this makes sense and will help someone down the road.

I would appreciate any hint to solve this problem. Could you please give a simple example of what you mean by "set that optional attribute to explicitly point to the data model that will contain the area you 'many' items"?

Hi,

thanks a lot. I will try to make my code run again. peter

Success! Many thanks to all of you. Peter

This provided the clue to fix this error for me. For me, this appeared when I moved from iOS 17.7 to iOS18.0.

To be more specific, I had a main class with something like

@Model
final public class MainThing {
    @Attribute(.unique) public var id : UUID
    var name: String
	
    @Relationship(deleteRule: .cascade) var things = [Thing]()
...

and Thing was simply

@Model
public class Thing {
    var when: Date
    var note: String
...
}

Changing these to the following:

@Model
final public class MainThing {
    @Attribute(.unique) public var id : UUID
    var name: String
	
    @Relationship(deleteRule: .cascade, inverse: \Thing.mainThing) var things = [Thing]()
...

and Thing was simply

@Model
public class Thing {
    var mainThing: MainThing?
    var when: Date
    var note: String
...
}

and setting the mainThing to the "one" being added to before adding the Thing fixed the problem.

Thank you all for posting this and the solution. This is literally the only resource about this on the internet. It helped me with a resolution.

My app was working fine for iOS 17, but for iOS 18 would silently fail with the mentioned error b/c I didn't explicitly define the relationships and set the corresponding objects before insertion.

I really can't believe Apple just made breaking changes to SwiftData, a supposedly stable framework, without any communication or documentation thereof. Very unprofessional. Never again will I be an early adopter of any of Apple's developer technologies.

iOS 18.1 and SeiftData
 
 
Q