Setup Fetch To Search For SuperClass?
I have a NSFetch that searches for objects in the Superclass which works fine. There are 4 subclasses. I want to have it search for 3 out of the 4 subclasses instead. One of the 4 subcl开发者_运维问答asses can't be displayed in the app without an in app purchase so I don't want it visible via a search.
Any ideas?
Sounds like you're setting the entity in an NSFetchRequest
and you want to prevent a certain sub-entity from showing up. I think your best bet is simply to filter the fetch results yourself, since NSPredicate can't test the entity type. Once you've executed your NSFetchRequest you can construct a new array consisting only of elements of the first array that aren't a member of that particular subclass. Granted, if you're limiting the fetch request to a set number of results (e.g. "paging" your array), it's going to get a bit more complicated. In that case, you can fetch more objects than you need on the assumption that some will be filtered out, and if you filter out so many that you're left with less than a page size, you can execute a second fetch and continue. Yes, it's complicated.
An alternative solution is to store a boolean on your superclass which indicates whether the object requires IAP, and then always set this to YES
for one subclass and NO
for all others. That way you can test the boolean in your NSPredicate
.
精彩评论