NSPredicate使用Fetch请求返回无结果,与数组过滤一起使用 [英] NSPredicate Returns No Results with Fetch Request, Works with Array Filtering

查看:167
本文介绍了NSPredicate使用Fetch请求返回无结果,与数组过滤一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的情况很简单:我的核心数据存储中有一些记录。它们的一个属性是一个名为localId的字符串。有一个点,我想找到具有特定localId值的记录。明显的做法是使用NSFetchRequest和NSPredicate。但是,当我设置这个,请求返回零记录。



然而,如果我使用fetch请求没有谓词,返回所有记录,他们正在寻找目标localId值,我找到我正在寻找的记录。换句话说,记录在那里,但是获取请求找不到它。



我使用获取请求和谓词的其他方法都按预期工作。我不知道为什么这是失败。



我想这样做:

   - (void)deleteResultWithLocalID:(NSString *)localId {
NSFetchRequest * request = [[NSFetchRequest alloc] init];
[request setEntity:[NSEntityDescription entityForName:@WCAAssessmentResultinManagedObjectContext:context]];
[request setPredicate:[NSPredicate predicateWithFormat:@localId ==%@,localId]];
NSError * error = nil;
NSArray * results = [context executeFetchRequest:request error:& error];
NSAssert(error == nil,([NSString stringWithFormat:@Error:%@,error]));
if([results count])[context deleteObject:[results objectAtIndex:0]];
else NSLog(@找不到与localID%@记录,localId);
[self saveContext];
}

但我最终必须这样做:

   - (void)deleteResultWithLocalID:(NSString *)localId {
NSFetchRequest * request = [[NSFetchRequest alloc] init];
[请求setEntity:[NSEntityDescription entityForName:@WCAAssessmentResultinManagedObjectContext:context]];
NSError * error = nil;
NSArray * results = [context executeFetchRequest:request error:& error];
NSAssert(error == nil,([NSString stringWithFormat:@Error:%@,error]));
for(WCAAssessmentResult * result in results){
if([result.localId isEqualToString:localId]){
NSLog(@result found,deleted);
[context deleteObject:result];
}
}
[self saveContext];
}

有什么可能会出错的线索?



编辑



我发现我可以使用我创建的谓词我期望在获取请求被执行后。所以,以下也适用:

   - (WCAChecklistItem *)checklistItemWithId:(NSNumber *)itemId {
NSFetchRequest * request = [[NSFetchRequest alloc] init];
[request setEntity:[NSEntityDescription entityForName:@WCAChecklistIteminManagedObjectContext:context]];
NSArray * foundRecords = [context executeFetchRequest:request error:nil];
foundRecords = [foundRecords filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@serverId ==%@,itemId]];
if([foundRecords count]){
return [foundRecords objectAtIndex:0];
} else {
NSLog(@无法找到ID为%@的项目ID);
return nil;
}
}

UPDATE >

我碰到过遇到这个问题的人:



http://markmail.org/message/7zbcxlaqcgtttqo4



他还没有找到解决方案。



Blimey!我很抱歉。



谢谢!

解决方案

但是这些类型的问题的最常见的原因是简单的拼写错误。确保您的属性名称和谓词是相同的。确保您的属性名称和属性名称相同。如果对属性的引用工作,但对属性名称的引用不存在,则可能是不匹配。



您可以通过比较以下内容的结果来测试后者:

  [result valueForKey:@localID] 

...返回:

  result.localID 


My situation is simple: I have some records in my core data store. One of their attributes is a string called "localId". There's a point where I'd like to find the record with a particular localId value. The obvious way to do this is with an NSFetchRequest and an NSPredicate. However, when I set this up, the request returns zero records.

If, however, I use the fetch request without the predicate, returning all records, and just loop over them looking for the target localId value, I do find the record I'm looking for. In other words, the record is there, but the fetch request can't find it.

My other methods in which I use fetch requests and predicates are all working as expected. I don't know why this one is failing.

I want to do this:

- (void)deleteResultWithLocalID:(NSString *)localId {
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    [request setEntity:[NSEntityDescription entityForName:@"WCAAssessmentResult" inManagedObjectContext:context]];
    [request setPredicate:[NSPredicate predicateWithFormat:@"localId == %@", localId]];
    NSError *error = nil;
    NSArray *results = [context executeFetchRequest:request error:&error];
    NSAssert(error == nil, ([NSString stringWithFormat:@"Error: %@", error]));
    if ([results count]) [context deleteObject:[results objectAtIndex:0]];
    else NSLog(@"could not find record with localID %@", localId);
    [self saveContext];
}

But I end up having to do this:

- (void)deleteResultWithLocalID:(NSString *)localId {
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    [request setEntity:[NSEntityDescription entityForName:@"WCAAssessmentResult" inManagedObjectContext:context]];
    NSError *error = nil;
    NSArray *results = [context executeFetchRequest:request error:&error];
    NSAssert(error == nil, ([NSString stringWithFormat:@"Error: %@", error]));
    for (WCAAssessmentResult *result in results) {
        if ([result.localId isEqualToString:localId]) {
            NSLog(@"result found, deleted");
            [context deleteObject:result];
        }
    }
    [self saveContext];
}

Any clues as to what could be going wrong?

edit

I've found that I can use the predicate I'm creating to get the results I expect after the fetch request has been executed. So, the following also works:

- (WCAChecklistItem *)checklistItemWithId:(NSNumber *)itemId {
     NSFetchRequest *request = [[NSFetchRequest alloc] init];
     [request setEntity:[NSEntityDescription entityForName:@"WCAChecklistItem" inManagedObjectContext:context]];
     NSArray *foundRecords = [context executeFetchRequest:request error:nil];
     foundRecords = [foundRecords filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"serverId == %@", itemId]];
     if ([foundRecords count]) {
         return [foundRecords objectAtIndex:0];
     } else {
         NSLog(@"can't find checklist item with id %@", itemId);
         return nil;
    }
}

UPDATE

I've come across someone else experiencing this very issue:

http://markmail.org/message/7zbcxlaqcgtttqo4

He hasn't found a solution either.

Blimey! I'm stumped.

Thanks!

解决方案

Hate to say it but the most common cause of these types of problems is simple typos. Make sure that your attribute names and the predicate are the same. Make sure that your property names and the attributes names are the same. If a reference to a property works but a reference to attribute name doesn't there is probably a mismatch.

You could test for the latter by comparing the return of:

[result valueForKey:@"localID"]

... with the return of:

result.localID

这篇关于NSPredicate使用Fetch请求返回无结果,与数组过滤一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆