我目前试图弄清楚为什么下面的代码片段会产生错误。我正在使用 Google Calendar API 加载日历。但是我假设它异步加载其数据,因此当我调用 -reloadData 我的应用程序崩溃时没有控制台错误。
[[GDataHandler sharedDataHandler] fetchCalendarsWithCompletion:^(GDataFeedBase *feed) {
//CRASH !!! Trying to execute code on main thread
dispatch_async(dispatch_get_main_queue(), ^{
for(GDataEntryCalendar *cal in feed.entries){
if([cal isKindOfClass:[GDataEntryCalendar class]]){
NSLog(@"adding %@", cal.title.stringValue);
[calendarArray addObject:cal];
}
}
[list reloadData];
});
}];
回溯看起来像这样:
* thread #1: tid = 0xeabe5, 0x39ed81fc libsystem_kernel.dylib`__pthread_kill + 8, queue = 'com.apple.main-thread, stop reason = signal SIGABRT
frame #0: 0x39ed81fc libsystem_kernel.dylib`__pthread_kill + 8
frame #1: 0x39f3fa52 libsystem_pthread.dylib`pthread_kill + 58
frame #2: 0x39e8902c libsystem_c.dylib`abort + 76
frame #3: 0x392d798e libc++abi.dylib`abort_message + 74
frame #4: 0x392f06e6 libc++abi.dylib`default_terminate_handler() + 254
frame #5: 0x39928938 libobjc.A.dylib`_objc_terminate() + 192
frame #6: 0x392ee1b2 libc++abi.dylib`std::__terminate(void (*)()) + 78
frame #7: 0x392edd16 libc++abi.dylib`__cxa_rethrow + 102
frame #8: 0x3992880e libobjc.A.dylib`objc_exception_rethrow + 42
frame #9: 0x2f5615b6 CoreFoundation`CFRunLoopRunSpecific + 642
frame #10: 0x2f561322 CoreFoundation`CFRunLoopRunInMode + 106
frame #11: 0x342982ea GraphicsServices`GSEventRunModal + 138
frame #12: 0x31e181e4 UIKit`UIApplicationMain + 1136
frame #13: 0x000b8e44 MyApp`main(argc=1, argv=0x27d54d0c) + 116 at main.m:16
frame #14: 0x39e21ab6 libdyld.dylib`start + 2
以下是委托方法:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return calendarArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellId = @"CellIdentifier";
CustomListCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
if(cell == nil){
cell = [[CustomListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
}
cell.calendar = [calendarArray objectAtIndex:indexPath.row];
return cell;
}