1
votes

I need to make a list of Products that is present on my Cart. So I have this data model: image

My idea is get all products that have a cartItem by the SKU Product to make this "Product List".

My question is what is the best way to do it?

I tried to make a fetchRequest to the Product entity and apply this NSPredicate:

NSPredicate *predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"sku.cartItem IN %@", cartItemArray]];

Bug I get this error:

2013-10-29 15:08:10.860 Prototipo[85766:70b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unable to parse the format string "sku.cartItem IN ( " (entity: CartItem; id: 0x8aa2820 ; data: )", " (entity: CartItem; id: 0x8aa2830 ; data: )", " (entity: CartItem; id: 0x8aa2840 ; data: )", " (entity: CartItem; id: 0x8aa2850 ; data: )", " (entity: CartItem; id: 0x8aa2860 ; data: )", " (entity: CartItem; id: 0x8aa2870 ; data: )", " (entity: CartItem; id: 0x8aa2880 ; data: )", " (entity: CartItem; id: 0x8aa2890 ; data: )", " (entity: CartItem; id: 0x8aa28a0 ; data: )", " (entity: CartItem; id: 0x8aa28b0 ; data: )", " (entity: CartItem; id: 0x8aa28c0 ; data: )", " (entity: CartItem; id: 0x8aa28d0 ; data: )", " (entity: CartItem; id: 0x8aa28e0 ; data: )" )"'

Thank you so much. Appreciate your attention.

––––– X –––––

EDIT (Solved)

In order to help someone else and to summarize the solution, it cannot be used the [NSString stringWithFormat:@...], the right way is to use directly [NSPredicate predicateWithFormat:@"...", args].

And just another tip! If you have the same problem and need to get some data filtered for a n-th level, use the ANY + IN on Predicate:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ANY sku.cartItem IN %@", cartItemArray];
1

1 Answers

1
votes

A predicate use its own formating replacement.
Use:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ANY sku.cartItem IN %@", cartItemArray];