I have two entities 'Times
' <*-> 'FileList
'. 'FileList
' - has relation 'whenDownload
', reverse relation from 'Times
' is 'wichFile
'.
Every file in 'FileList
' could be download several times and myApp stores several versions of files in file system. 'Times
' helps me to find exact version and other information about file (comments, etc).
So I have filesArray with several objects from 'FileList' and try to find last download version each FileList objects from array to understand is it necessary to download new versions or not. Server-side with files is not mine.
My code:
NSFetchRequest *cutRequest = [[NSFetchRequest alloc] init];
cutRequest.entity = [NSEntityDescription entityForName:@"Times" inManagedObjectContext:localContext];
(NSArray *) listFilesToDownload = [self getListFilesToDownload]; // array with 30-90 files from 10k
NSPredicate * filePredicate = [NSPredicate predicateWithFormat:@"whichFile IN %@", listFilesToDownload];
// doesn't work:
//NSPredicate * timePredicate = [NSPredicate predicateWithFormat : @ "SUBQUERY(Times, $s, max($s.timeDownload))"];
//NSPredicate * timePredicate = [NSPredicate predicateWithFormat : @ "max (Times, $s, $s.timeDownload)"];
//NSPredicate * timePredicate = [NSPredicate predicateWithFormat : @ "(SUBQUERY (Times, $s, $s.timeDownload).max != 0)"];
//NSPredicate * timePredicate = [NSPredicate predicateWithFormat : @ "max (timeDownload)"];
//NSPredicate * timePredicate = [NSPredicate predicateWithFormat : @ "max (Times.timeDownload)"];
//NSString *name1 = @"timeDownload";
//NSPredicate * timePredicate = [NSPredicate predicateWithFormat : @ "max (%K)", name1];
NSPredicate * timePredicate = [NSPredicate predicateWithFormat : @ "SUBQUERY (Times, $time, max($time.timeDownload)).@count > 0"];
NSArray *allPredicates = [NSArray arrayWithObjects: filePredicate, timePredicate, nil];
cutRequest.predicate = [NSCompoundPredicate andPredicateWithSubpredicates:allPredicates];
cutRequest.fetchBatchSize = 300;
NSArray *arrayRequest = [localContext executeFetchRequest:cutRequest error:&error];
[cutRequest release];
also I tried single predicate:
NSPredicate * timePredicate = [NSPredicate predicateWithFormat : @ "(whichFile IN %@) AND (SUBQUERY (Times, $s, max($s.timeDownload))"];
but still have "Unable to parse the format string..."
I'm not familiar with Core Data. Some posts (one, two and three) were interesting, but not enough for me. I couldn't find enough details about SUBQUERY syntax with functions.
1. Could you explane how to get correct predicate with subquery in my case ?
2. What are you recommend to read about subquery (except Predicate programming guide :)
Hope, the question would be interesting for everybody.
Looking forward for your advices.
Nik