Search code examples
iosobjective-ccore-datanspredicatensfetchrequest

Is there a function similar to the sql "NOT IN" in Core Data?


SQL Command

SELECT GAMELOCALTEAM FROM GAME WHERE GAMELOCALTEAM NOT IN ‘TBC’ ORDER BY GAMELOCALTEAM

I would like to write sql command in objective c. My worte code below

NSManagedObjectContext * context = ((AFLAppDelegate *)[[UIApplication sharedApplication] delegate] ).managedObjectContext;
NSFetchRequest* fetch = [NSFetchRequest fetchRequestWithEntityName:@“game”];
NSEntityDescription *entity = [NSEntityDescription entityForName:@“game” inManagedObjectContext:context];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"NOT(gamelocalteam IN 'tbc')"];
[fetch setPredicate:predicate];
NSSortDescriptor *sortDescreptor =[[NSSortDescriptor alloc]initWithKey:@"gamelocalteam" ascending:YES];
[fetch setSortDescriptors:[NSArray arrayWithObject:sortDescreptor]];
NSAttributeDescription* attributeDescription = [entity.attributesByName objectForKey:@"gamelocalteam"];
[fetch setPropertiesToFetch:[NSArray arrayWithObjects:attributeDescription, nil]];
[fetch setPropertiesToGroupBy:[NSArray arrayWithObject:attributeDescription]];
[fetch setResultType:NSDictionaryResultType];
NSError *error = nil;
_team = [context executeFetchRequest:fetch error:&error];

Predicate Format is @"NOT(gamelocalteam IN 'tbc') if this call app is crashing.

Is possible do NOT IN?


Solution

  • Try it

    @"NOT gamelocalteam contains[cd] 'tbc'"
    

    Code Below

    NSManagedObjectContext * context = ((AFLAppDelegate *)[[UIApplication sharedApplication] delegate] ).managedObjectContext;
    NSFetchRequest* fetch = [NSFetchRequest fetchRequestWithEntityName:@“game”];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@“game” inManagedObjectContext:context];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:>@"NOT gamelocalteam contains[cd] 'tbc'"];
    [fetch setPredicate:predicate];
    NSSortDescriptor *sortDescreptor =[[NSSortDescriptor alloc]initWithKey:@"gamelocalteam" ascending:YES];
    [fetch setSortDescriptors:[NSArray arrayWithObject:sortDescreptor]];
    NSAttributeDescription* attributeDescription = [entity.attributesByName objectForKey:@"gamelocalteam"];
    [fetch setPropertiesToFetch:[NSArray arrayWithObjects:attributeDescription, nil]];
    [fetch setPropertiesToGroupBy:[NSArray arrayWithObject:attributeDescription]];
    [fetch setResultType:NSDictionaryResultType];
    NSError *error = nil;
    _team = [context executeFetchRequest:fetch error:&error];