bolerovt
bolerovt

Reputation: 623

core data array of string filter

background:

I have a set of data, each of them has an attribute called "type". "type" is an array of String values, like:

data1 = {"type": ["car", "truck", "plane"]}

in core data, I store "type" as Transformable


need:

Now, I need to filter these data by checking if there "type" contains ANY value in an given array, like:

["pickup", "plane"]

so, data1 in the previous section should be qualified.


question:

Could anyone help me to figure out an working NSPredicate to get what I want?

I tried "CONTAINS", "SUBQUERY", but none of them works.

Many Thanks!!

Upvotes: 0

Views: 434

Answers (1)

Tom Harrington
Tom Harrington

Reputation: 70936

There is no such predicate. You cannot use transformable values as part of a predicate, except (probably) if you're checking for equality. That is, if you had an array and transformed it, you should be able to filter for objects with that exact value.

The transformed value is a series of bytes, represented in iOS apps by an instance of NSData. This byte blob has no indication of set membership, or any other higher level meaning. It's just bits. Even if you transformed your filter array, there's no guarantee that it would match a sub-section of those bytes.

If you need to do this kind of filtering, you need to reconsider how you're saving your data. Ideally you'd have a separate entity called Type where you could save the type names, and have a to-many relationship from your current entity to the Type entity.

Upvotes: 2

Related Questions