gpt4 book ai didi

iphone - 调用 dismissViewControllerAnimated 时的 exc_bad_access

转载 作者:行者123 更新时间:2023-11-28 18:24:30 25 4
gpt4 key购买 nike

我有一个向上滑动的 ViewController。当您单击“保存”时,它会向服务器发送请求。请求完成后,它会关闭 ViewController。我将 NSURLConnection 切换为使用异步和 block (https://github.com/rickerbh/NSURLConnection-Blocks)。现在关闭 ViewController 会抛出“线程 2:程序收到信号:EXC_BAD_ACCESS”。如果重要的话,我正在使用 ARC。

- (IBAction) savePressed
{
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://api.com/items/create"]];

//NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[NSURLConnection asyncRequest:request success:^(NSData *data, NSURLResponse *response) {
[self close];
} failure:^(NSData *data, NSError *error) {
[self close];
}];
}

- (void) close
{
[self dismissViewControllerAnimated:YES completion:nil];
}

这是日志

2012-10-24 10:32:43.780 Prayrbox[22268:1703] bool _WebTryThreadLock(bool), 0x1f21fd90: Tried to obtain the web lock from a thread other than the main thread or the web thread. This may be a result of calling to UIKit from a secondary thread. Crashing now...
1 0x347dc927 WebThreadLock
2 0x36718615 <redacted>
3 0x366d0a85 <redacted>
4 0x3678d789 <redacted>
5 0x366c0637 <redacted>
6 0x366d50e7 <redacted>
7 0x368c1f11 <redacted>
8 0x366d4969 <redacted>
9 0x36744745 <redacted>
10 0x366907ad <redacted>
11 0x7ef71 -[ComposeViewController close]
12 0x7eec5 __36-[ComposeViewController savePressed]_block_invoke_0
13 0x82d8f __56+[NSURLConnection(Blocks) asyncRequest:success:failure:]_block_invoke_0
14 0x37e8811f <redacted>
15 0x37e96259 <redacted>
16 0x37e963b9 <redacted>
17 0x37b30a11 <redacted>
18 0x37b308a4 start_wqthread
[Switching to process 10755 thread 0x2a03]
[Switching to process 10755 thread 0x2a03]
[unknown](gdb)

我已经花了 2 个小时来寻求这方面的帮助。如果有人知道什么可以帮助,请说出来! :)

最佳答案

乍一看,我同意上面的评论,即在关闭 View 时您可能走错了线程。

UI 的东西应该在 Main 上完成,所以要强制执行,你可以这样做:

-(void) close {
if([NSThread isMainThread]) {
[self dismissViewControllerAnimated:YES completion:nil];
}
else {
[self performSelectorOnMainThread:@selector(close)
withObject:nil
waitUntilDone:YES];
}
}

而不是从你的 block 中执行 performSelectorOnMainThread,这将确保无论何时调用它,你都会在主线程上。

关于iphone - 调用 dismissViewControllerAnimated 时的 exc_bad_access,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13054127/

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