我在主线程上显示UIAlertView
时遇到问题。我不确定为什么,但是尽管我在主线程上运行,它仍然崩溃。以下块位于后台线程上,但主要警告如下:
void (^removeFromCalendar)(NSString *, NSString *, EKEventStore *) = ^(NSString *error, NSString *eventKey, EKEventStore *eventDB) {
EKEvent *myEvent = [eventDB eventWithIdentifier:eventKey];
NSError *err = noErr;
if(myEvent != NULL && myEvent != (id)[NSNull null]) {
[eventDB removeEvent:myEvent span:EKSpanThisEvent error:&err];
} else {
// Event was not found, nothing to do
return;
}
[eventDB release];
if (!err || err == noErr) {
NSLog(@"Deleted event %@", myEvent.title);
// Show alert on the main thread
dispatch_async(dispatch_get_main_queue(), ^{
// Showing the alert for unattending
NSString *resultString = @"This event was removed from your calendar.";
UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:@"Info" message:resultString delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil] autorelease];
[alert show];
});
return;
}
error = err.description;
};
如果我在显示警报的底部注释掉,一切都很好。但是对于警报,我一直收到
EXC_BAD_ACCESS
错误。有人可以解释为什么吗?它在正确的线程上,我终生无法理解内存问题的根源! 最佳答案
您可能会在完成背景队列之前完成视图的释放。因此,为了安全起见,为什么不这样使用它?
...........
UIViewController __weak *myController = self;
dispathch_async(backgroundQueue, ^{
UIViewController __strong *myStrongController = myController;
...............
dispatch_async(dispatch_get_main_queue(), ^{
if(myStrongController){
// Showing the alert for unattending
NSString *resultString = @"This event was removed from your calendar.";
UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:@"Info" message:resultString delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil] autorelease];
[alert show];
}
});
}).