【问题标题】:Keep getting sigabrt error with core data核心数据不断出现 sigabrt 错误
【发布时间】:2011-09-02 11:52:59
【问题描述】:

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

下面是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 上缺少获取请求是如何导致错误的

谢谢

【问题讨论】:

  • 告诉我们更多关于错误的信息 - 你能查明是哪一行导致的吗?

标签: iphone cocoa-touch cocoa core-data


【解决方案1】:

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

Apple 的 viewDidLoad 方法的一个副作用是它会创建 eventsArray。如果您从未创建过它,但您告诉 tableView 您已经插入了一行,那么您最好让该行可用!

你在打电话

[eventsArray insertObject:event atIndex:0];

但我敢打赌,如果没有 Apple 的 viewDidLoad 方法,eventsArraynil - 如果它是 nil(即 eventsArray = [[NSMutableArray alloc] init];),则需要创建它。

【讨论】:

    【解决方案2】:

    我在阅读 Apple 的教程时遇到了同样的问题。

    使用调试器单步调试它在

    [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    

    上一行 -(无效)添加事件 在 RootViewController.m

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

    [[self locationManager] startUpdatingLocation]

    所以当

    -(void)addEvent 
    

    被触发程序正在返回并退出

    if (!location) {
        return;
    }
    

    因此没有进入违规行,我将 startUpdatingLocation 添加回原始海报代码的 viewDidLoad 并且再次出现 sigabrt 错误。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-15
      • 2012-01-20
      • 1970-01-01
      • 1970-01-01
      • 2015-06-01
      相关资源
      最近更新 更多