簡體   English   中英

調用dismissViewControllerAnimated時,exc_bad_access

[英]exc_bad_access when calling dismissViewControllerAnimated

我有一個向上滑動的ViewController。 當您單擊“保存”時,它將向服務器發送請求。 請求完成后,它將關閉ViewController。 我將NSURLConnection切換為使用異步和塊(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個小時在此方面尋求幫助。 如果有人知道有什么幫助,請說出來! :)

乍一想,我同意以上評論,即您在關閉視圖時可能處在錯誤的線程上。

UI Stuff應該在Main上完成,要執行此操作,您可以執行以下操作:

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

而不是從您的代碼塊執行performSelectorOnMainThread,這將確保在每次調用它時您都將處於主狀態。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM