iOS: [self.fetchedResultsController PerformFetch:&error];使我的应用程序崩溃

发布于 2024-11-06 08:42:17 字数 1853 浏览 0 评论 0原文

我有一个 UITableViewController,我想向它提供核心数据模型的内容。 但是,当我获取内容时,我的应用程序崩溃了。这是 init 方法(我将 NSManagedObjectContext 传递给它)。

- (id)initInManagedObjectContext:(NSManagedObjectContext *)context
{
    self = [super initWithStyle:UITableViewStylePlain];

    if (self) {
        NSFetchRequest *request = [[NSFetchRequest alloc] init];
        request.entity = [NSEntityDescription entityForName:@"Document" inManagedObjectContext:context];
        request.predicate = nil;    
        request.sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"idDoc" 
                                                                                     ascending:YES]];
         /*
         NSError *error = nil;
         NSManagedObject *retrievedDocument = [[context executeFetchRequest:request error:&error] lastObject];
         NSLog(@"retrievedDocument %@", retrievedDocument);
         */

        NSFetchedResultsController *frc = [[NSFetchedResultsController alloc]
                                             initWithFetchRequest:request 
                                             managedObjectContext:context 
                                               sectionNameKeyPath:nil 
                                                        cacheName:@"CollectionCache"];

        self.fetchedResultsController = frc;
        [frc release];
        [request release];


        //HERE IT CRASHES
        NSError *error;
        [self.fetchedResultsController performFetch:&error];

        if (error) {
            // Update to handle the error appropriately.
            NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
            //exit(-1);  // Fail
        }

    }
    return self;
}

我确信上下文已正确传递,因为如果我取消注释注释片段,则存储的数据将正确打印。

我的猜测是 fetchedResultsController 有问题。

谢谢

I have a UITableViewController, and I want to feed it with the content of a core data model.
However, when I fetch the content my app crashes. This is the init method (I pass a NSManagedObjectContext to it).

- (id)initInManagedObjectContext:(NSManagedObjectContext *)context
{
    self = [super initWithStyle:UITableViewStylePlain];

    if (self) {
        NSFetchRequest *request = [[NSFetchRequest alloc] init];
        request.entity = [NSEntityDescription entityForName:@"Document" inManagedObjectContext:context];
        request.predicate = nil;    
        request.sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"idDoc" 
                                                                                     ascending:YES]];
         /*
         NSError *error = nil;
         NSManagedObject *retrievedDocument = [[context executeFetchRequest:request error:&error] lastObject];
         NSLog(@"retrievedDocument %@", retrievedDocument);
         */

        NSFetchedResultsController *frc = [[NSFetchedResultsController alloc]
                                             initWithFetchRequest:request 
                                             managedObjectContext:context 
                                               sectionNameKeyPath:nil 
                                                        cacheName:@"CollectionCache"];

        self.fetchedResultsController = frc;
        [frc release];
        [request release];


        //HERE IT CRASHES
        NSError *error;
        [self.fetchedResultsController performFetch:&error];

        if (error) {
            // Update to handle the error appropriately.
            NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
            //exit(-1);  // Fail
        }

    }
    return self;
}

I'm sure the context is correctly passed because if I uncomment the commented snippet, the stored data are correctly printed.

My guess is that something is wrong with the fetchedResultsController.

thanks

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

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

发布评论

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

评论(2

不喜欢何必死缠烂打 2024-11-13 08:42:17

该异常与您错误使用performFetch:有关:

它返回一个BOOL,告诉您提取是否成功。如果返回“否”,您可以检查 NSError 对象。否则你绝对不能碰它。

可能所有使用 &error 的方法都应该像这样使用:

    NSError *error;
    if (![self.fetchedResultsController performFetch:&error]) {
        // Update to handle the error appropriately.
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        //exit(-1);  // Fail
    }

The exception was related to your wrong use of performFetch:

It returns a BOOL that tells you the success of the fetch. If you get a NO back you are allowed to check the NSError object. Otherwise you must not touch it.

Probably all the methods that use &error should be used like this:

    NSError *error;
    if (![self.fetchedResultsController performFetch:&error]) {
        // Update to handle the error appropriately.
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        //exit(-1);  // Fail
    }
海螺姑娘 2024-11-13 08:42:17

该异常与 fetchedResultsController 无关,而是由于未初始化 NSError NSError

*error = nil;

The exception was not related to fetchedResultsController but due to the not initialized NSError

NSError *error = nil;

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