【问题标题】:Filter in CoreData?过滤CoreData?
【发布时间】:2013-10-07 04:25:57
【问题描述】:

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

-(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中Array的个数是

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

显示不止一个元素,随着我添加的更多,它会适当增加。

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

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

它什么也不返回,即使是 OBJECT 在 CoreData 中也是可用的。

那么通过 match clubid 获取的正确查询是什么,即 NSString?

【问题讨论】:

    标签: ios objective-c core-data


    【解决方案1】:

    试试这个,

    -(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;
    }
    

    【讨论】:

    • [fetchRequest setPredicate:[NSPredicate predicateWithFormat:@"(dealid contains[cd] %@)", clubID]];
    • 谢谢,它有效,让我知道亲爱的,因为你有时间,我已经接受了答案:)
    • 看看这个,亲爱的,这也是我的查询。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-24
    • 1970-01-01
    • 1970-01-01
    • 2016-12-13
    相关资源
    最近更新 更多