ab1428x
ab1428x

Reputation: 804

Swift CoreData child to-many entities not accessible through parent

I have an event parent entity which contains many fights. All the fight entries have the proper event inverse property id. I can access the event through the fight, but when I try to get the fights by accessing event.fights I get the following error

expression produced error: error: Execution was interrupted, reason: internal ObjC exception breakpoint(-5)..
The process has been left at the point where it was interrupted, use "thread return -x" to return to the state before expression evaluation.

Warning: hit breakpoint while running function, skipping commands and conditions to prevent recursion.Warning: hit breakpoint while running function, skipping commands and conditions to prevent recursion.Warning: hit breakpoint while running function, skipping commands and conditions to prevent recursion.

Why can't I access the many child properties through the parent? Is there any way to fix this?

The type shows up as an _ArrayBuffer<Schedule.Fight>

Edit 1:

After searching more on the subject, it turns out this is the result of something called relationship faults, which is a performance enhancement meaning that the child entities are lazily loaded.

I still couldn't find any solution on how to solve this and make the child entities accessible. Most answers seem to rely on the an NSSet however, my child entites are defined as optional arrays as so:

extension Event {
...
@NSManaged public var arena: String
@NSManaged public var baseTitle: String
@NSManaged public var eventFights: [EventFight]?
...
}

Can this be fixed by adding something to the fetch request or maybe forcing data loading on child entities? Any suggestion would be helpful.

Upvotes: 0

Views: 515

Answers (1)

vadian
vadian

Reputation: 285069

A Core Data to-many relationship must be an (unordered) set, it cannot be an array, and I recommended to declare to-many relationships always as non-optional:

@NSManaged public var eventFights: Set<Event>

Upvotes: 1

Related Questions