Iphone 不断获取核心数据的sigabrt错误

Iphone 不断获取核心数据的sigabrt错误,iphone,cocoa-touch,cocoa,core-data,Iphone,Cocoa Touch,Cocoa,Core Data,目前我正在学习核心数据,并正在编写一个简单版本的苹果核心数据教程(位置) 以下是addEvent方法的代码: Event *event = (Event *)[NSEntityDescription insertNewObjectForEntityForName:@"Event" inManagedObjectContext:managedObjectContext]; [event setCreationDate:[NSDate date]]; NSError *error; if (![

目前我正在学习核心数据,并正在编写一个简单版本的苹果核心数据教程(位置)

以下是addEvent方法的代码:

Event *event = (Event *)[NSEntityDescription insertNewObjectForEntityForName:@"Event" inManagedObjectContext:managedObjectContext];
[event setCreationDate:[NSDate date]];


NSError *error;
if (![managedObjectContext save:&error]) {
    // Handle the error.
}


[eventsArray insertObject:event atIndex:0];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];
当这个方法被触发时,我得到一个sigabrt错误。然而,我发现如果我加入苹果的viewDidLoad代码,错误就不会再发生了,我一辈子都不知道为什么了

以下是阻止错误发生的代码:

- (void)viewDidLoad {

[super viewDidLoad];

// Set the title.
self.title = @"Locations";

// Configure the add and edit buttons.
self.navigationItem.leftBarButtonItem = self.editButtonItem;

addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addEvent)];
self.navigationItem.rightBarButtonItem = addButton;



/*
 Fetch existing events.
 Create a fetch request; find the Event entity and assign it to the request; add a sort descriptor; then execute the fetch.
 */

NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Event" inManagedObjectContext:managedObjectContext];
[request setEntity:entity];

// Order the events by creation date, most recent first.
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"creationDate" ascending:NO];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[request setSortDescriptors:sortDescriptors];
[sortDescriptor release];
[sortDescriptors release];

// Execute the fetch -- create a mutable copy of the result.
NSError *error = nil;
NSMutableArray *mutableFetchResults = [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy];
if (mutableFetchResults == nil) {
    // Handle the error.
}

// Set self's events array to the mutable array, then clean up.
[self setEventsArray:mutableFetchResults];
[mutableFetchResults release];
[request release];
我错过了什么?我看不出viewDidLoad上缺少获取请求是如何导致错误的


谢谢

您的tableview如何知道要显示哪些事件

苹果的viewDidLoad方法的一个副作用是它将创建eventsArray。如果您从未创建过该行,但告诉tableView您已插入了一行,则最好将该行设置为可用

你在打电话吗

[eventsArray insertObject:event atIndex:0];

但我敢打赌,如果没有苹果的
viewDidLoad
方法,
eventsArray
就是
nil
——如果它是
nil
(即
eventsArray=[[NSMutableArray alloc]init];
我在阅读苹果教程时也遇到了同样的问题

使用调试器单步执行,它将在

[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
在线 -(无效)补遗 在RootViewController.m中

原始海报上的代码阻止错误发生的原因是-(void)viewDidLoad中缺少位置管理器触发的行

[[self locationManager]startUpdatingLocation]

因此,当

-(void)addEvent 
当程序点击返回并退出时触发

if (!location) {
    return;
}
因此,由于没有达到令人不快的程度,我将startUpdatingLocation添加回原始海报代码的viewDidLoad,sigabrt错误再次出现


我知道这不是一个答案,但我希望这能为其他人节省半个小时。

告诉我们有关错误的更多信息-您能指出是哪一行造成的吗?