9

I have an array of ids. I want to fetch core data entity for the items in the ids array. How can we implement that using NSFetchRequest?

Sheik_101
  • 770
  • 1
  • 8
  • 24

2 Answers2

13

Use NSPredicate

fetchRequest.predicate = NSPredicate(format: "itemId IN %@", itemIds)

Instead of itemId set your actual id property name. Now perform a fetch with this fetch request

Andrey Chernukha
  • 21,488
  • 17
  • 97
  • 161
0

let hrData = self.cstProjectDetails as NSArray

jojo
  • 51
  • 5
  • The result of calling fetch is an array. You had an array to start with. Then you did a very odd thing: you cast hrData to an NSArray. Well, an NSArray has no element type. – jojo Jul 10 '18 at 12:54
  • Swift doesn't have any information about what a logDetails even is. It just types it as an AnyObject. If you want to extract its species property you will need to cast your //logDetails back down to whatever it really is, i.e. a CSTProjectDetails. Alternatively, don't cast to an NSArray, since this erases your element type information. Cast down to a Swift array of CSTProjectDetails. Now Swift will have type information from the get-go. – jojo Jul 10 '18 at 12:55