在CoreData中过滤?

时间:2013-10-07 04:25:57

标签: ios objective-c core-data

我写了一个类似下面的方法

-(NSArray*)getClubDetailedDeals:(NSString *)clubID{

    NSError *error;
    NSManagedObjectContext *context = [self managedObjectContext];

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

    NSEntityDescription *entity = [NSEntityDescription entityForName:@"ClubDetailsDeals"
                                              inManagedObjectContext:context];

    [fetchRequest setEntity:entity];

    NSArray *fetchedArray1 = [[context executeFetchRequest:fetchRequest error:&error] mutableCopy];

    ClubDetailsDealsDAO *obb = [fetchedArray1 objectAtIndex:0];

    NSLog(@"MY ID IS %@ and count is %d", obb.dealname, [fetchedArray1 count]);




    NSLog(@"deal is %@", clubID);

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(%K CONTAINS %@)", @"clubid", clubID];

    //NSPredicate *predicate = [NSPredicate predicateWithFormat:@"dealid contains[cd] %@", dealID];

    [fetchRequest setPredicate:predicate];

    NSArray *fetchedArray = [[context executeFetchRequest:fetchRequest error:&error] mutableCopy];

    NSLog(@"COUNT of arary is ~~~~ %d", [fetchedArray count]);

    return fetchedArray;
}

此处,此LOG中的数组计数为

* NSLog(@“MY ID IS%@ and count is%d”,obb.dealname,[fetchedArray1 count]); *

显示了多个元素,正如我添加的更多,它正常增加。

但是当我使用此查询获取它时

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(%K CONTAINS %@)", @"clubid", clubID];

它没有返回任何内容,甚至在CoreData中也可以使用OBJECT。

那么匹配clubid获取的正确查询是什么,即NSString?

1 个答案:

答案 0 :(得分:0)

试试这个,

-(NSArray*)getClubDetailedDeals:(NSString *)clubID{

    NSError *error;
    NSManagedObjectContext *context = [self managedObjectContext];

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

    NSEntityDescription *entity = [NSEntityDescription entityForName:@"ClubDetailsDeals"
                                              inManagedObjectContext:context];

    [fetchRequest setEntity:entity];

[fetchRequest setPredicate:[NSPredicate predicateWithFormat:@"(dealid
 contains[cd] %@)", clubID]];

    NSArray *fetchedArray = [[context executeFetchRequest:fetchRequest error:&error] mutableCopy];

    NSLog(@"COUNT of arary is ~~~~ %d", [fetchedArray count]);

    return fetchedArray;
}