我有一個ViewController向上滑動。當你點擊「保存」時,它向服務器發送一個請求。請求完成後,它將關閉ViewController。我將NSURLConnection切換爲使用異步和塊(https://github.com/rickerbh/NSURLConnection-Blocks)。現在關閉ViewController會拋出「線程2:編程接收到的信號:EXC_BAD_ACCESS」。如果有問題,我正在使用ARC。exc_bad_access當調用dismissViewControllerAnimated
- (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小時來尋找關於此幫助。如果有人知道什麼可以幫助,請說出來! :)
有兩個問題需要澄清:1)您確定完成處理程序正在主線程中調用嗎? 2)NSURLConnection已經有一個異步請求方法...你有沒有嘗試過使用它?也許這是您使用的第三方庫的問題。 – jmstone617
1)我不確定。我對Objective C非常新鮮,不知道如何解決這個問題。 2)我正在使用的類別(請參閱後鏈接)正在dispatch_async中打包同步調用並提供塊。如果有更好的方法來使用塊,我很想知道。 –
對於第一個問題,您可以在完成處理程序中設置斷點,並且出現的堆棧跟蹤將告訴您正在調用哪個線程。 NSURLConnection有一個方法+(void)sendAsynchronousRequest:(NSURLRequest *)請求隊列:(NSOperationQueue *)隊列completionHandler:(void(^)(NSURLResponse *,NSData *,NSError *))處理程序,你應該試試看看你是否有相同的問題 – jmstone617