【问题标题】:my app gets stuck when I call performFetch some times当我调用 performFetch 几次时,我的应用程序卡住了
【发布时间】:2012-04-14 21:01:31
【问题描述】:

我正在使用斯坦福课程 cs193p 中的 CoreDataTableViewController,并在我的 viewWillAppear 中调用

self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request
                                                                    managedObjectContext:self.dataBase.managedObjectContext
                                                                      sectionNameKeyPath:nil
                                                                               cacheName:nil];

这在某些随机情况下使我的应用程序冻结(它不会崩溃,它只是冻结)。 在 CoreDataTableViewController 中,setter 调用 [self performFetch],如下所示:

- (void)performFetch
{
    if (self.fetchedResultsController) {
        if (self.fetchedResultsController.fetchRequest.predicate) {
            if (self.debug) NSLog(@"[%@ %@] fetching %@ with predicate: %@", NSStringFromClass([self class]), NSStringFromSelector(_cmd), self.fetchedResultsController.fetchRequest.entityName, self.fetchedResultsController.fetchRequest.predicate);
        } else {
            if (self.debug) NSLog(@"[%@ %@] fetching all %@ (i.e., no predicate)", NSStringFromClass([self class]), NSStringFromSelector(_cmd), self.fetchedResultsController.fetchRequest.entityName);
        }
        NSError *error;
        NSLog(@"before");
        [self.fetchedResultsController performFetch:&error];
        NSLog(@"after");
        if (error) NSLog(@"[%@ %@] %@ (%@)", NSStringFromClass([self class]), NSStringFromSelector(_cmd), [error localizedDescription], [error localizedFailureReason]);
    } else {
        if (self.debug) NSLog(@"[%@ %@] no NSFetchedResultsController (yet?)", NSStringFromClass([self class]), NSStringFromSelector(_cmd));
    }
    [self.tableView reloadData];
}

它卡住的那条线是

[self.fetchedResultsController performFetch:&error];

有什么建议吗?

【问题讨论】:

  • 您找到解决此问题的方法了吗?我有同样的问题。

标签: ios core-data cs193p


【解决方案1】:

在我的例子中,我在 viewWillAppear 方法上调用 PerformFetch,它必须在 viewDidLoad() 方法上调用,即使您使用的是导航控制器。 我们知道第二次以后 veiwWillappear 方法不会被调用,在调用 performeguewithidentifer(push) 的情况下,不用担心,每次在 move-back-next 时都会调用 fetechresultcontroller。

希望这可以帮助某人。

【讨论】:

    【解决方案2】:

    该调用不会挂起您的 GUI,因为它发生在后台线程中。因此,如果您的 GUI 挂起,那就是另一个问题了。

    话虽如此,您不应该在正常操作中调用它。通常,您应该执行以下两项操作之一来保存 UIManagedDocument 中的更改:

    使用撤消管理器,或调用

    [doc updateChangeCount: UIDocumentChangeDone];
    

    【讨论】:

    • 你是对的。当我调用 [self.navigationController popViewControllerAnimated:YES] 时,它似乎卡住了;任何想法为什么会这样?
    • 在调试器中单步执行。
    • 经过更多检查,我能够缩小问题范围,并彻底改变了我的问题。你能再看看吗?
    • 我不知道为什么会挂起。如果你在调试器中跟踪它,你可能至少有机会看到什么方法永远不会返回
    猜你喜欢
    • 1970-01-01
    • 2013-08-17
    • 1970-01-01
    • 2018-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多