0

我无法理解这里发生了什么......

    -(void)viewWillAppear:(BOOL)animated
{
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [ NSEntityDescription entityForName:kTransaction inManagedObjectContext:self.context];
    [fetchRequest setEntity:entity];
    NSSortDescriptor *sort = [[NSSortDescriptor alloc]
                              initWithKey:kDate ascending:NO];
    [fetchRequest setSortDescriptors:[NSArray arrayWithObject:sort]];

    NSPredicate *pred;

    if (self.willShowDebts) {
        pred = [NSPredicate predicateWithFormat:@"isDebt = YES"];  
    }else {
        pred = [NSPredicate predicateWithFormat:@"isLoan = YES"];
    }
    [fetchRequest setPredicate:pred];

    [self setFetchController:nil];
    self.fetchController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.context sectionNameKeyPath:nil cacheName:nil];

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

    NSLog(@"fetch objects  %i",[[self.fetchController fetchedObjects] count]);

    [super viewWillAppear:animated];
}

我在我的应用程序中这样做了大约 4 次......在这里它不太工作......它第一次显示所有实体,但如果我返回(它在导航控制器中)然后再次转到此视图一切都消失了......什么给了

2012-09-06 13:54:15.994 app[1126:fe03] fetch objects  3
2012-09-06 13:54:19.254 app[1126:fe03] fetch objects  0
2012-09-06 13:54:22.145 app[1126:fe03] fetch objects  0
2012-09-06 13:54:24.328 app[1126:fe03] fetch objects  0

这是日志输出...有什么想法吗?

4

1 回答 1

0

您的托管对象上下文是否每次都正确设置(或者是正确设置nil)?

在方法的第一行设置断点并逐步执行,检查一切是否符合您的预期。

于 2012-09-06T11:34:25.413 回答