I have two entities:
- Entity A
- Entity B
Entity A has an ordered to-many relationship to Entity B.
Entity B has an ordered to-many relationship to Entity A.
Entities A and B are unique (no duplicates) and a single Entity B can be added to various Entity A via relationship.
How can I fetch Entities B based on the order of insertion on Entity A via relationship using NSFetchedResultsController? I can't use createdAt property as it wouldn't work and I can't add a sortOrder property because the order will be different on various Entity A.
To make it more clearer with code example.
let entityA1 = EntityA(context: context)
let entityA2 = EntityA(context: context)
let entityB1 = EntityB(context: context)
let entityB2 = EntityB(context: context)
let entityB3 = EntityB(context: context)
entityA1.relationshipToEntityB = NSOrderedSet(array: [entityB1, entityB2, entityB3])
entityA2.relationshipToEntityB = NSOrderedSet(array: [entityB2, entityB1, entityB3])
Note the order of the array, it is different for each Entity A. I want NSFetchedResultsController to display the order of the array. This is why a dedicated property on Entity B can't work otherwise I would have to make a property for each Entity A which doesn't scale well.
I tried doing this based on this answer.
NSSortDescriptor(key: #keyPath(EntityB.relationshipToA), ascending: true)
But it crashed for me as I use NSTableViewDiffableDataSource. It make sense to me because which Entity A will it base its sort order on? I also noticed the answer is based on to-one and to-many rather than to-many and to-many which is what I want.
Fatal: supplied identifiers are not unique.