iphone:NSPredicate 与 NSFetchedResultsController 的问题

发布于 2024-09-25 23:19:28 字数 1627 浏览 3 评论 0原文

目前,我的系统中定义了一个“主题”实体:

@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 技术交流群。

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

发布评论

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

评论(2

舟遥客 2024-10-02 23:19:28

尝试将cacheName设置为nil:

NSFetchedResultsController *theFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:_context sectionNameKeyPath:nil cacheName:nil];

它在类似的情况下帮助了我。

try to set cacheName to nil:

NSFetchedResultsController *theFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:_context sectionNameKeyPath:nil cacheName:nil];

it helped me in similar case.

沉睡月亮 2024-10-02 23:19:28

像这样的东西会起作用吗?

[NSPredicate predicateWithFormat:@"topicID == %@", [NSNumber numberWithInt:4100]];

Would something like this work?

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