Per NSConstraintConflict
documentation:
There are two situations in which a conflict may occur:
1. Between multiple objects being saved in a single managed object context. In this case, the conflict will have a nil database object/snapshot, and multiple conflicting objects/snapshots representing the state of the objects when they were first faulted or inserted into the context.
2. Between a single object being saved in a managed object context and the external store. In this case, the constraint conflict will have a database object, the current row snapshot for the database object, plus a a single conflicting object and its snapshot from when it was first faulted or inserted.
.
Snapshot dictionaries include values for all attributes and to-one relationships, but not to-many relationships. Relationship values are NSManagedObjectID references. to-many relationships must be pulled from the persistent store as needed.
I have a merge policy based on mergeByPropertyStoreTrumpMergePolicyType
that inserts only changed values into the database objects.
But I'm not really sure how to handle conflicts between new objects with conflicting ids (first situation).
for example, a corresponding json response for a Conversion
entity in my store:
{
"id": "Conversation-F59AAAB5-ECEE-4825-9724-5593952C5B72",
"readDate": 691410107.91709995,
"receiver": {
"gender": "male",
"firstName": "Kyle",
"id": "FE9B3A26-B71F-4371-8F11-D2B5187902CE", <--- conversation receiver ID
"creationDate": 639652907.91709399,
"profileImageUrl": "https://images.pexels.com/photos/1462980/pexels-photo-1462980.jpeg?h=75&w=75",
"lastName": "Edwards"
},
"initiator": {
"id": "d0b36216-aab5-40f9-b2de-7efd3bc6cc8d"
},
"lastMessage": {
"creationDate": 685910087.91710198,
"id": "Message-Conversation-F59AAAB5-ECEE-4825-9724-5593952C5B72-2F0CCB35-91EA-4023-9739-09C960D26858",
"isStarred": false,
"sender": {
"id": "FE9B3A26-B71F-4371-8F11-D2B5187902CE" <--- last message sender ID
},
"deletionState": 0,
"text": "Doloremque minima quidem atque doloremque neque consectetur ea laboriosam."
}
}
Receiver and message sender are both User
.
On this scenario, when using the predefined merge policies, core data sometimes insert the receiver with all of its data to the store, but sometimes it selects the sender part of the json containing only the ID.
I do see a conflict in resolve(constraintConflicts list: [NSConstraintConflict])
but not sure how to resolve it without a database object.
init() {
super.init(merge: .mergeByPropertyStoreTrumpMergePolicyType)
}
override func resolve(constraintConflicts list: [NSConstraintConflict]) throws {
for conflict in list {
guard let databaseObject = conflict.databaseObject else {
//--->Poblem is here <---
try NSMergeByPropertyObjectTrumpMergePolicy.resolve(constraintConflicts: [conflict])
return
}
for conflictingObject in conflict.conflictingObjects {
resolveConstraintConflicts(conflictingObject: conflictingObject, databaseObject: databaseObject)
}
}
try super.resolve(constraintConflicts: list)
}
My question is, what object should I set with the proper values, is it the first conflict? or should I insert one manually to the context?
I find the lack of custom merge policies subclasses examples quite frustrating.