I have a CoreData performance problem in my iOS App.
I need execute a query with a subquery clause.
Lets explain the case.
I have a Entity called quota that has groupCode and date attributes. (There are more attributes but it isn't import for now).
For execute my search, it's require two step:
- At first, I need select all quota items where groupCode is equals to quotaGroupProductParam (quotaGroupProductParam is a variable. For this example quotaGroupProductParam = 6816)
- At last, for each result I need execute a new search in the same entity use as clause date = result.date and groupCode = quotaGroupParam (quotaGroupParam is a variable. For this example quotaGroupParam = 58).
Example:
NSArray *quotasGroupProduct = [[DBContext shared] executeFetchRequest:@"Quota" WithFilter:[NSPredicate predicateWithFormat:@"groupCode=%@", quotaGroupProductParam]];
for (Quota *quota in quotasGroupProduct) {
NSArray *quotasGroup = [[DBContext shared] executeFetchRequest:@"Quota" WithFilter:[NSPredicate predicateWithFormat:@"groupCode = %@ AND date = %@", quotaGroupParam, quota.date]];
if([quotasGroup count] > 0 && [quotasRepresentative count] > 0) {
[quotaDatesArray addObject:quota.date];
}
}
I'd like change this code to execute just one fetchRequest.
Below a SQL query that do that:
select * from zquota q where zgroupCode = 58 and exists (select 1 from zquota where ZgroupCode = 6816 and zdate = q.zdate);
Anyone could help me?
Thanks!