Optimization of the query query master data

I am developing an application and an application that needs to check whether a row has been saved in the database or not. This may seem like a simple operation, but it takes half a second to get an answer, which, in my opinion, is quite a lot. My question is is there a way to reduce this time. Thank you for your interest.

This is my current code:

- (BOOL) isDeleted:(int)i {

    NSString *value = [NSString stringWithFormat:@"deleted.*.%i", i];

    MyAppDelegate *appDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
    NSManagedObjectContext *context = [appDelegate managedObjectContext];

    NSString *entityName = @"Deleted";
    NSEntityDescription *entityDesc = [NSEntityDescription entityForName:entityName inManagedObjectContext:context];
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    [request setEntity:entityDesc];

    NSPredicate *pred = [NSPredicate predicateWithFormat:@"(deletedpics like %@)", value];
    [request setPredicate:pred];

    NSError *error;
    NSArray *objects = [context executeFetchRequest:request error:&error];

    BOOL returnBool = [objects count] >= 1 ? YES : NO;
    return returnBool;

}
+5
source share
1 answer

One optimization is replacing

NSArray *objects = [context executeFetchRequest:request error:&error];

by

[request setFetchLimit:1];
NSUInteger count = [context countForFetchRequest:request error:&error];

if you want to check for objects.

But I guess the main performance issue is finding a lookup with LIKE, which is slower than finding with ==.

deleted.<status>.<picturenumber> ( ), , status picturenumber .

[NSPredicate predicateWithFormat:@"picturenumber == %d", i];

. , ( @flexaddicted, ).

+5

All Articles