核心数据NSPredicate的关系

时间:2009-05-29 15:15:52

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

我的对象图很简单。

我有一个费用对象,用于存储有关RSS源的信息以及一个名为Tag的关系,该关系链接到“TagValues”对象。关系(to和inverse)都是to-many。即,Feed可以有多个标签,标签可以与多个Feed相关联。

我提到了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; 

-

2 个答案:

答案 0 :(得分:29)

这里的关键是“ANY”

来自apple的例子:

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

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

答案 1 :(得分:3)

你需要SQLite吗?我正在努力解决类似的问题,并发现一切都按照预期的方式运行二进制存储 使用SQLite作为商店时存在一些限制,但我还没有找到列出限制的文档,只有它们存在。

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