0

首先让我解释一下我的应用程序的流程,

当我启动应用程序时,我会检查用户是否已登录,-(void) viewWillAppear:(BOOL)animated如果没有,我会执行此检查,我会显示登录控制器。现在这完美无缺。

在我的 loadView 中,我访问了我的代码数据堆栈,并尝试通过单击其中一个单元格来将 fetchedObjects 显示在表格视图中,我会显示有关单击的单元格对象的更多信息。

我就是这样做的。

AppDelegate *app = (AppDelegate*)[[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context = [app managedObjectContext];
NSFetchRequest* fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Sites" inManagedObjectContext:context];
NSError *error;

[fetchRequest setEntity:entity];

fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];

现在,当第一次加载视图时,我在调试器中得到以下信息

(lldb) po fetchedObjects
(NSArray *) $1 = 0x00352860 <_PFArray 0x352860>(
<NSManagedObject: 0x352550> (entity: Sites; id: 0x34c9b0 <x-coredata://7CD0A735-BC41-4E7A-8B07-C957E6096320/Sites/p1> ; data: <fault>)
)

这似乎很好。

现在 viewWillAppear 被调用,登录视图被显示,用户登录,登录视图从导航堆栈中弹出,然后 tableview 的 cellforrowatindexpath 再次被调用,当我在那里休息时,我再次 po 我的 fetchedObjects 我得到了

(lldb) po fetchedObjects
(NSArray *) $3 = 0x00352860 [no Objective-C description available]

这我不明白,为什么数据没有得到持久化?

异常被抛出

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [fetchedObjects count];
}

fetchedObjects 是该类的成员,我还没有发布它,我从来没有改变它的价值?

4

1 回答 1

1
fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];

返回一个自动释放的对象。如果您希望它挂起,您必须保留它或通过您的retained 属性访问器分配它,如果它存在(这是首选方法):

self.fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];
于 2012-03-12T09:17:59.477 回答