1

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.

Tom Tallak Solbu
  • 559
  • 6
  • 20

1 Answers1

1

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.

pbasdf
  • 21,386
  • 4
  • 43
  • 75
  • Great! It did not occur to me that I had do drill from the bottom and up. Also a life saver that an internal identifier aborts the need for a unique identifier for the top entity. Thanks :-) – Tom Tallak Solbu May 12 '16 at 14:31