Tom Tallak Solbu
Tom Tallak Solbu

Reputation: 559

CoreData. Fetchrequest all entities with relations

My main Entity is called Series. Series has a one to many relationship to an entity called Rounds, connected by one-to-many (ordered), and Rounds has again an NSOrderedSet with Shots. The Shots entity has an attribute called score, which is an Integer.

xcdatamodeld

What I want, is to get all the scores from the Shots entity, belonging to a specific Series.

let shots = currentSeries.rounds.shots as [Shots]

does not give me all the Shots to iterate through, due to the error messeage: "Value of type 'NSOrderedSet' has no member: 'shots'". Somehow, I need to set a predicate to the "Shots" entity which filters all the Shots that belong to a specific "Series" entity.The Series entity does not have a unique identifier, but I guess it could be possible to use the timestamp attribute to isolate a specific "Series". But again, I want all the "Shots" entities, connected to that specifi "Series".

I could seriously need som help about CoreData mining, or at least som suggestions about how to accomplish my efforts.

Upvotes: 1

Views: 256

Answers (1)

pbasdf
pbasdf

Reputation: 21536

One way to get all the Shots for a given Series would be to fetch with a predicate:

let fetch = NSFetchRequest(entityName:"Shots")
let fetch.predicate = NSPredicate(format:"rounds.series == %@", currentSeries)
let currentSeriesShots = try! context.executeFetchRequest(fetch) as! [Shots]

(You should add proper error handling). No need to use a unique identifier - CoreData will use its own (internal) identifier for the currentSeries object to determine which Shots to return.

Upvotes: 1

Related Questions