在核心数据中搜索 id

发布于 2024-11-17 02:10:43 字数 1364 浏览 3 评论 0原文

我在 CoreData 和 NSPredicate 中遇到了一些意外的行为。在一个大型数据库群体中,我有不同的相互关联的托管对象。但是,我有以下问题。当给出一个 id(NSNumber,作为 NSString 给这个函数)时,除非我先保存整个上下文,否则我不会得到结果。我不想这样做,因为它需要太多时间(因为它是一大组数据)。代码是:

- (DOSite *) findSite:(NSString *) siteId {
    NSPredicate* predicate = [NSPredicate predicateWithFormat:@"(id = %@)", siteId];

    [NSFetchedResultsController deleteCacheWithName:nil];
    [[self fetchedResultsController].fetchRequest setPredicate:predicate];

    NSError *fetchError;

    if (![[self fetchedResultsController] performFetch:&fetchError]) {
        // Handle the error.
        // This is a serious error and should advise the user to restart the application
        NSLog(@"Fetching data error: %@", [fetchError localizedDescription]);
    }

    if([[[self fetchedResultsController] fetchedObjects] count] == 0){
        return NULL;
    }
    return (DOSite *)[[[self fetchedResultsController] fetchedObjects] objectAtIndex:0];
}

因此,当我添加 x 个项目(使用 +[NSEntityDescription insertNewObjectForEntityForName:inManagedObjectContext:])时,对所有项目进行搜索会返回正确数量的项目。

当搜索字符串时(例如predicateWithFormat:@"(name LIKE %@)"),我得到了肯定的结果,但是当使用上面的代码时predicateWithFormat:@"(id = %@) 我得到零结果。

我得到结果的唯一方法是保存整个上下文,然后执行 fetchRequest,然后突然它起作用了,

所以我在搜索 id 时一定犯了一些小错误 。似乎是盲目地发现它现在花两天时间来缩小范围,有没有人可以给我一些建议?

I am getting some unexpected behavior with CoreData and NSPredicate. In a large database population I have different Managed Objects relating to one-another. However, I have a problem with the following. When giving an id (NSNumber, given as NSString to this function) I don't get a result unless I save the whole context first. I don;t want to do that as it takes too much time (as it is a large set of data). The code is:

- (DOSite *) findSite:(NSString *) siteId {
    NSPredicate* predicate = [NSPredicate predicateWithFormat:@"(id = %@)", siteId];

    [NSFetchedResultsController deleteCacheWithName:nil];
    [[self fetchedResultsController].fetchRequest setPredicate:predicate];

    NSError *fetchError;

    if (![[self fetchedResultsController] performFetch:&fetchError]) {
        // Handle the error.
        // This is a serious error and should advise the user to restart the application
        NSLog(@"Fetching data error: %@", [fetchError localizedDescription]);
    }

    if([[[self fetchedResultsController] fetchedObjects] count] == 0){
        return NULL;
    }
    return (DOSite *)[[[self fetchedResultsController] fetchedObjects] objectAtIndex:0];
}

So when I add an x number of items (using +[NSEntityDescription insertNewObjectForEntityForName:inManagedObjectContext:]) doing a search on all items return the right amount of items.

When searching for a string (e.g.predicateWithFormat:@"(name LIKE %@)") I get positive results, but when using the above code predicateWithFormat:@"(id = %@) I get zero results.

The only way I can get results is to save the whole context and then perform the fetchRequest, then suddenly it works.

So there must be something small I do wrong in searching for the id, I just seem to be blind to find it and spend two days at it now to narrow it down to this point. Is there anybody who can give me some advice on this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

摇划花蜜的午后 2024-11-24 02:10:43

这可能不起作用,但是您是否尝试过在实体中使用比“id”更复杂的名称(例如“SiteID”)?有时,非常短的名称会与其他系统属性重叠,从而导致奇怪的问题。

This may not work, but have you tried using a name more complex than "id" in your entity (like "SiteID")? Sometimes very short names overlap with other system properties and it causes odd issues.

酸甜透明夹心 2024-11-24 02:10:43

问题是我给了谓词一个 NSString ,如上所述。当将其更改为 int (即 predicateWithFormat:@"(id == %i)")时,由于某种原因它可以正常工作。

The problem was that I gave a NSString to the predicate as outlined above. When changing that to an int (ie predicateWithFormat:@"(id == %i)") it works fine for some reason.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文