CoreData NSPredicate on a Transformable causes EXC_BAD_ACCESS

1k views Asked by At

What is the correct way to search for existence of an array element in a Transformable in Core Data using NSPredicate?

I have an Entity with an Attribute operationalDays with a Type of Transformable which is set given a Custom Class [Int]. enter image description here I use it to store, well Arrays of Ints. It works to store but I get EXC_BAD_ACCESS when trying to fetch them using an NSPredicate

I Know

operationalDays is {1,2,3,4}

I am trying but they all fail with error.

//day is an Int (1) in this case

NSPredicate(format: "%d IN operationalDays",day) 
NSPredicate(format: "%i IN operationalDays",day) 
NSPredicate(format: "%@ IN operationalDays",day) 
NSPredicate(format: "operationalDays CONTAINS %d",day) 
NSPredicate(format: "operationalDays CONTAINS %i",day)
NSPredicate(format: "operationalDays CONTAINS %@",day)

when I inspect the predicate after crash I get

predicate: (1 IN operationalDays);

or

predicate: (operationalDays CONTAINS 1)

2

There are 2 answers

2
jarora On BEST ANSWER

The transformable attributes are stored as binary data in the Sqlite DB. You can't run a query on that data unless your predicate has binary data as well. Transformable attributes abide by NSCoding protocol, hence, Core Data knows how to serialize and deserialize the attribute.

P.S. To see the error that Sqlite throws, try adding

-com.apple.CoreData.SQLDebug 1

to the "Arguments Passed on Launch" in your Xcode scheme.

enter image description here

0
Nabin Bhusal On

you should use the predicate in this style

let predicate = NSPredicate(format: "%@ IN days", argumentArray: [valueOfArgumentHere])