iphone:NSPredicate 与 NSFetchedResultsController 的问题
目前,我的系统中定义了一个“主题”实体:
@interface Topic : NSManagedObject
{
}
@property (nonatomic, retain) NSString * path;
@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) NSNumber * topicID;
@property (nonatomic, retain) NSNumber * parent;
@end
我想使用 NSFetchedResultsController 获取具有特定编号(例如 4001)的主题。我将我的定义为:
- (NSFetchedResultsController *)fetchedResultsController {
if (_fetchedResultsController != nil) {
return _fetchedResultsController;
}
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Topic" inManagedObjectContext:_context];
[fetchRequest setEntity:entity];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"topicID == 4100"];
[fetchRequest setPredicate:predicate];
NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"path" ascending:NO];
[fetchRequest setSortDescriptors:[NSArray arrayWithObject:sort]];
[fetchRequest setFetchBatchSize:20];
NSFetchedResultsController *theFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:_context sectionNameKeyPath:nil cacheName:@"Root"];
self.fetchedResultsController = theFetchedResultsController;
_fetchedResultsController.delegate = self;
[fetchRequest release];
[theFetchedResultsController release];
return _fetchedResultsController;
}
..没有谓词,FetchedResultsController 工作正常..但是,当我添加它时,出现错误并且应用程序崩溃..有人可以告诉我我是否错误地定义了 NSPredicate ?
I currently have a 'Topic' entity defined in my system as:
@interface Topic : NSManagedObject
{
}
@property (nonatomic, retain) NSString * path;
@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) NSNumber * topicID;
@property (nonatomic, retain) NSNumber * parent;
@end
I want to fetch a topic with a specific number (e.g. 4001) using an NSFetchedResultsController. I've defined mine as:
- (NSFetchedResultsController *)fetchedResultsController {
if (_fetchedResultsController != nil) {
return _fetchedResultsController;
}
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Topic" inManagedObjectContext:_context];
[fetchRequest setEntity:entity];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"topicID == 4100"];
[fetchRequest setPredicate:predicate];
NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"path" ascending:NO];
[fetchRequest setSortDescriptors:[NSArray arrayWithObject:sort]];
[fetchRequest setFetchBatchSize:20];
NSFetchedResultsController *theFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:_context sectionNameKeyPath:nil cacheName:@"Root"];
self.fetchedResultsController = theFetchedResultsController;
_fetchedResultsController.delegate = self;
[fetchRequest release];
[theFetchedResultsController release];
return _fetchedResultsController;
}
..Without the predicate, the FetchedResultsController works fine.. however, the moment I add it, there is an error and the app crashes.. can someone please tell me if I've defined my NSPredicate incorrectly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试将cacheName设置为nil:
它在类似的情况下帮助了我。
try to set cacheName to nil:
it helped me in similar case.
像这样的东西会起作用吗?
Would something like this work?