开发者

traverse Core Data object graph with added predicate?

I want to load a client object and then pull their related purchase orders based on whether they have been placed or not, purchase orders have an IsPlaced BOOL property.

So I have my client object and I can get all purchase orders like this, which is working great:

purchaseordersList =[[myclient.purchaseorders allObjects] mutableCopy];

But ideally I would actually like 2 array's - one开发者_StackOverflow社区 for each order type: IsPlaced=YES and IsPlaced=NO

How do I do that here? Or do I need to do another fetch?


First, there is no reason to be turning the set into an array unless you are sorting it and there is no reason to be turning that array into a mutable array. Did you get that from some example code?

Second, you can filter an array or a set by using a predicate so you can create two sets (or arrays) easily via:

NSSet *placed = [[myclient purchaseorders] filteredSetUsingPredicate:[NSPredicate predicateWithFormat:@"isPlaced == YES"]];
NSSet *notPlaced = [[myclient purchaseorders] filteredSetUsingPredicate:[NSPredicate predicateWithFormat:@"isPlaced == YES"]];

If you are wanting to use this for a UITableView then look into a NSFetchedResultsController instead. It will save you a LOT of boiler-plate code.

Do you remember what example code you got that from? Been seeing that -mutableCopy a lot lately and would love to quash it. :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜