【问题标题】:Core Data NSPredicate for relationships用于关系的核心数据 NSPredicate
【发布时间】:2010-10-29 22:02:21
【问题描述】:

我的对象图很简单。

我有一个 feedentry 对象,它存储有关 RSS 提要的信息和一个名为 Tag 的关系,它链接到“TagValues”对象。关系(到和逆)都是对多的。也就是说,一个提要可以有多个标签,一个标签可以关联到多个提要。

我参考了How to do Core Data queries through a relationship? 并创建了一个 NSFetchRequest。但是当获取数据时,我得到一个异常说明,

NSInvalidArgumentException 未实现的谓词 SQL 生成

我该怎么办?我是核心数据的新手 :( 我知道我做错了什么...请帮助...

谢谢

--

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
// Edit the entity name as appropriate.
NSEntityDescription *entity = [NSEntityDescription entityForName:@"FeedEntry" inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];
// Edit the sort key as appropriate.
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"authorname" ascending:NO];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];

[fetchRequest setSortDescriptors:sortDescriptors];

NSEntityDescription *tagEntity = [NSEntityDescription entityForName:@"TagValues" inManagedObjectContext:self.managedObjectContext];
NSPredicate *tagPredicate = [NSPredicate predicateWithFormat:@"tagName LIKE[c] 'nyt'"];          
NSFetchRequest *tagRequest = [[NSFetchRequest alloc] init];
[tagRequest setEntity:tagEntity];
[tagRequest setPredicate:tagPredicate];

NSError *error = nil;
NSArray* predicates = [self.managedObjectContext executeFetchRequest:tagRequest error:&error];


TagValues *tv = (TagValues*) [predicates objectAtIndex:0];
NSLog(tv.tagName); // it is nyt here...


NSPredicate *predicate = [NSPredicate predicateWithFormat:@"tag IN %@", predicates];
[fetchRequest setPredicate:predicate];


// Edit the section name key path and cache name if appropriate.
// nil for section name key path means "no sections".
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:nil cacheName:@"Root"];
aFetchedResultsController.delegate = self;
self.fetchedResultsController = aFetchedResultsController; 

--

【问题讨论】:

  • 贴出你的一些代码,让我们看得更清楚
  • Mungunth,在将谓词重新设置为“标记 IN 谓词”后,您可能忘记设置 fetchRequest 的实体。如果这不是问题,请同时发布对象模型的图片,我可以为您提供更多指导。
  • Mugunth> 您应该添加您的解决方案作为答案并接受它。
  • 我通过 devforums.apple.com 得到了答案。在 NDA 到位之前,我不能透露答案...... :(

标签: iphone cocoa cocoa-touch core-data osx-leopard


【解决方案1】:

这里的关键是“ANY”

来自苹果的例子:

NSPredicate *predicate = [NSPredicate predicateWithFormat:
    @"ANY employees.firstName like 'Matthew'"];

http://developer.apple.com/mac/library/documentation/cocoa/conceptual/CoreData/Articles/cdBindings.html

【讨论】:

    【解决方案2】:

    您需要 SQLite 吗?我正在努力解决类似的问题,并且发现二进制存储的一切都按预期工作。
    使用 SQLite 作为存储时有一些限制,虽然我还没有找到列出限制的文档,只是它们存在。

    很抱歉,我无法提供更多帮助。

    【讨论】:

    • 是的,它适用于二进制存储。我使用了 sql lite db,因为在我的情况下它似乎更快。我处理大小接近 5MB 的数据(在 iPhone 上)。 sqlite 的应用加载时间
    • 我找到了答案... NSPredicate *predicate = [NSPredicate predicateWithFormat:@"tag IN %@", predicates]; [fetchRequest setPredicate:predicate];应该改为 NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ANY tag IN %@", predicates]; [fetchRequest setPredicate:predicate];
    • 很高兴知道-很奇怪,您的消息与我的消息非常相似,但我的消息是由于使用 SQLite 的 Core Data 的限制。知道如何区分!
    猜你喜欢
    • 2013-05-03
    • 1970-01-01
    • 2011-02-18
    • 2012-03-08
    • 1970-01-01
    • 2011-04-10
    • 2011-02-01
    • 1970-01-01
    • 2014-01-16
    相关资源
    最近更新 更多