1. Don't fetch more than you need. Only 10 or so rows are visible so set a batch size on your NSFetchRequests.
Example:
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Contact"];
request.fetchBatchSize = 20;
2. Optimize your data model. Put binary data in separate entities and use external storage. Duplication isn't always a bad thing, if it speeds up your data query.
3. Prefetch relationships if you know you need them.
4. Consider returning dictionaries instead of managed objects.
Example:
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Quake"];
[request setResultType:NSDictionaryResultType];
[request setPropertiesToFetch:@[@"magnitude"]];
5. Use SQLite to perform your calculations.
Example:
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Quake"];
NSExpressionDescription *ed = [[NSExpressionDescription alloc] init];
ed.name = @"minimum";
ed.expression = [NSExpression expressionForFunction:@"min:"
arguments:@[ [NSExpression expressionForKeyPath:@"magnitude"]]];
[request setPropertiesToFetch:@[ ed ]];
6. Use SQLite to group your results automatically.Example:
NSExpressionDescription *ed = [[NSExpressionDescription alloc] init];
ed.name = @"count";
ed.expression = [NSExpression expressionForFunction:@"count:"
arguments:@[ [NSExpression expressionForKeyPath:@"magnitude"]]];
[request setPropertiesToFetch:@[ @"magnitude", ed ]];
[request setPropertiesToGroupBy:@[ @"magnitude" ]];
7. Use SQL Logging.Pass argument flags = {1,2,3} on launch:
-com.apple.CoreData.SQLDebug 1
8. Optimize predicates. Text comparisons are expensive. Put numeric comparisons first.
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Contact"];
Bad:
request.predicate = [NSPredicate predicateWithFormat:@"firstName == %@ AND age > %i", @"John", 40];
Good:
request.predicate = [NSPredicate predicateWithFormat:@"age > %i && firstName == %@", 40, @"John"];
9. Predicate Costs (Low to High):
Beginswith/Endswith < Equality < Contains < Matches
No comments :
Post a Comment