Is there anything bad about reusing NSFetchRequest for several different sets using Core Data?

My question is: Is there anything bad to reuse NSFetchRequestfor several different sets using Core Data?

Code example:

NSFetchRequest *request = [[NSFetchRequest alloc] init];

NSEntityDescription *logEntity = [NSEntityDescription entityForName:@"LogEntry" inManagedObjectContext:context];
[request setEntity:logEntity];

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"dateTimeAction" ascending:NO]; // ascending NO = start with latest date
[request setSortDescriptors:[NSArray arrayWithObject:sortDescriptor]];

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"status == %@",@"op tijd"];
[request setPredicate:predicate];
[request setFetchLimit:50];

NSError *error = nil;
NSInteger onTimeCount = [context countForFetchRequest:request error:&error];

NSPredicate *predicate1 = [NSPredicate predicateWithFormat:@"status == %@",@"uitgesteld"];
[request setPredicate:predicate1];
[request setFetchLimit:50];

NSInteger postponedCount = [context countForFetchRequest:request error:&error];

NSPredicate *predicate2 = [NSPredicate predicateWithFormat:@"status == %@",@"gemist"];
[request setPredicate:predicate2];
[request setFetchLimit:50];

NSInteger missedCount = [context countForFetchRequest:request error:&error];
+4
source share
3 answers

This is not a problem, but in the above example it does not gain you much (just brevity of the code.) The most expensive part of creating a query is to parse a predicate format string.

If the code you provided is called frequently and you want to speed it up, here are some ideas:

  • : , dispatch_once() ;
  • , , count
  • , , predicateWithSubstitutionVariables: .
  • fetchRequestFromTemplateWithName:substitutionVariables: .

, .

+5

, , NSFetchedRequest - , , :

NSPredicate *predicates = [NSCompoundPredicate andPredicateWithSubpredicates:NSArray_of_predicates];
+1

, , , . (, NSKnownKeysDictionary1) , . , , , . . , , , ; ( ) /. , , , .

However, you can easily use the same predicate in multiple select queries.

0
source

All Articles