iPhone:核心数据:NSManagedObjects 和 UITableView 崩溃
这个 fetch 方法工作完美,NSLog 打印出数据库的内容...(NSArray 中的 fetchedObjects):
NSError *error2;
fetchedObjects = [moc executeFetchRequest:fetchRequest error:&error2];
if (!fetchedObjects) {
NSLog(@"Error %@",[error2 localizedDescription]);
}
for (NSManagedObject *info in fetchedObjects) {
NSLog(@"Name: %@", [info valueForKey:@"Name"]);
NSLog(@"Description: %@", [info valueForKey:@"Description"]);
NSLog(@"Ingredients: %@", [info valueForKey:@"Ingredients"]);
..但是当我尝试填充 tableView 时(我已经构建了很多表,并且感觉我很好地理解了如何他们工作),程序崩溃并显示“EXE_BAD_ACCESS at int retVal = UIApplicationMain(argc, argv, nil, nil);”。就是这样,没有其他错误消息。
为了尝试调试,我将这个简单的 NSLog 放入“cellForRowAtIndexPath”方法中,但它在 NSLog(@"fetchedObjects count in tableview %i",[fetchedObjects count]) 处崩溃。
据我所知,我还没有发布该数组。
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell.
NSLog(@"fetchedObjects count in tableview %i",[fetchedObjects count]);
NSManagedObject *info3 = [fetchedObjects objectAtIndex:indexPath.row];
NSLog(@"Name: %@", [info3 valueForKey:@"Name"]);
cell.textLabel.text = [info3 valueForKey:@"Name"];
This fetch method work perfectly and the NSLog prints out the contents of the database...(fetchedObjects in an NSArray):
NSError *error2;
fetchedObjects = [moc executeFetchRequest:fetchRequest error:&error2];
if (!fetchedObjects) {
NSLog(@"Error %@",[error2 localizedDescription]);
}
for (NSManagedObject *info in fetchedObjects) {
NSLog(@"Name: %@", [info valueForKey:@"Name"]);
NSLog(@"Description: %@", [info valueForKey:@"Description"]);
NSLog(@"Ingredients: %@", [info valueForKey:@"Ingredients"]);
.. but when I try to populate a tableView (I have built many tables and feel i have a good understanding of how they work), the program crashes with a "EXE_BAD_ACCESS at int retVal = UIApplicationMain(argc, argv, nil, nil);." That is it, no other error messages.
To try and debug, I put this simple NSLog in the "cellForRowAtIndexPath" method, but it crashes at NSLog(@"fetchedObjects count in tableview %i",[fetchedObjects count]).
I haven't released the array as far as I can tell.
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell.
NSLog(@"fetchedObjects count in tableview %i",[fetchedObjects count]);
NSManagedObject *info3 = [fetchedObjects objectAtIndex:indexPath.row];
NSLog(@"Name: %@", [info3 valueForKey:@"Name"]);
cell.textLabel.text = [info3 valueForKey:@"Name"];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看起来您的 fetchedObjects 实例变量由于自动释放池刷新而被释放。
NSManagedObjectContext
的-executeFetchRequest:error:
方法返回一个 autoreleasedNSArray
实例,您没有获得其所有权,因此,当执行到达 -tableView:cellForRowAtIndexPath: 方法时,自动释放池已被刷新,并且该对象被释放,现在您有一个悬空指针。啊,经典。当然,解决方案是通过将
-executeFetchRequest:error:
发送到您的来将
。-retain
消息发送到返回的NSArray
NSManagedObjectContext不要忘记在
-dealloc
方法中向其发送-release
消息。Looks like your
fetchedObjects
instance variable is being deallocated because of an autorelease pool flush.NSManagedObjectContext
's-executeFetchRequest:error:
method returns an autoreleasedNSArray
instance, which you aren't obtaining ownership of, therefore by the time execution reaches the-tableView:cellForRowAtIndexPath:
method, the autorelease pool has been flushed, and that object is deallocated, and you now have a dangling pointer. Ah, classic.The solution, of course, is to send the
-retain
message to the returnedNSArray
from sending-executeFetchRequest:error:
to yourNSManagedObjectContext
.Don't forget to send the
-release
message to it in your-dealloc
method.问题似乎出在这一行
NSManagedObject *info3 = [fetchedObjects objectAtIndex:indexPath.row];
尝试使用
NSManagedObject *info3 = [fetchedObjects objectAtIndex:0];
因为您的 UiTableViewCell 计数和 fetchedObjects 计数在整个过程中应该相等。
如果不匹配,则会出现索引错误。
The problem seems to be on this line
NSManagedObject *info3 = [fetchedObjects objectAtIndex:indexPath.row];
Try to use
NSManagedObject *info3 = [fetchedObjects objectAtIndex:0];
Because your UiTableViewCell count and fetchedObjects count should be equal at whole process.
If it doesn't match then it will give you index error.