核心数据丢失实体

时间:2012-09-06 11:00:27

标签: debugging ios5 core-data

我无法理解这里发生的事情......

    -(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

这是日志输出... 任何想法??

1 个答案:

答案 0 :(得分:0)

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

在方法的第一行放置断点并逐步执行,检查所有内容是否符合您的预期。