gpt4 book ai didi

iphone - UIAlertView 主队列崩溃。

转载 作者:行者123 更新时间:2023-12-03 21:15:56 27 4
gpt4 key购买 nike

我在主线程上显示 UIAlertView 时遇到问题。我不知道为什么,但它不断崩溃,尽管我在主线程上运行。以下 block 位于后台线程上,但我在主线程上有警报,如下所示:

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 错误。有人可以解释为什么吗?它位于正确的线程上,但我一生都无法理解内存问题可能来自何处!

最佳答案

也许您的 View 在您完成后正在被释放,直到您完成后台队列。所以,为了安全起见,为什么不这样使用它;

...........
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];
}
});
}).

关于iphone - UIAlertView 主队列崩溃。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13534243/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com