2012-10-24 4 views
0

위로 올라가는 ViewController가 있습니다. "저장"을 클릭하면 서버에 요청이 전송됩니다. 요청이 완료되면 ViewController를 닫습니다. 비동기 및 블록 (https://github.com/rickerbh/NSURLConnection-Blocks)을 사용하여 NSURLConnection을 전환했습니다. 이제 ViewController를 닫으면 "스레드 2 : 프로그램 수신 신호 : EXC_BAD_ACCESS"가 발생합니다. 그게 중요하다면 ARC를 사용하고 있습니다.exc_bad_access when 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]; 
} 

여기에 나는이에 대한 도움말을 검색 2 시간 보냈다

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) 

로그입니다. 누구든지 도움이 될 수있는 것이 무엇인지 아는 경우 위로 말하십시오! :)

+1

설명을위한 두 가지 질문 : 1) 완료 핸들러가 주 스레드에서 호출되고 있습니까? 2) NSURLConnection 이미 비동기 요청 메서드가 있습니다 ... 사용하려고 했습니까? 즉, 사용중인 타사 라이브러리에 문제가있는 것일 수 있습니다. – jmstone617

+0

1) 잘 모르겠습니다. 나는 Objective C에 꽤 신선하고 그것을 이해하는 방법을 모른다. 2) 내가 사용하는 카테고리 (게시물의 링크 참조)는 dispatch_async에서 동기 호출을 래핑하고 블록을 제공합니다. 블록을 사용하는 더 좋은 방법이 있다면, 알고 싶습니다. –

+0

첫 번째 질문의 경우 완료 핸들러 내에 중단 점을 설정할 수 있으며 표시되는 스택 추적은 호출되는 스레드를 알려줍니다. (NSURLRequest *) 요청 대기열 : (NSOperationQueue *) 대기열 completionHandler : (void (^) (NSURLResponse *, NSData *, NSError *)) 핸들러는 + (void) sendAsynchronousRequest를 가지고 있습니다. 같은 문제가있어 – jmstone617

답변

6

위의 의견에 동의 할 때보기를 닫을 때 잘못된 스레드에있을 수 있습니다.

UI 물건은되도록을 적용, 당신이 할 수있는, 홈페이지에서 수행해야합니다

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

오히려 당신의 블록에서 performSelectorOnMainThread을 수행 않고, 당신이 메인에있을거야라고 그 언제를 보장합니다 .

+0

그랬어! 정말 고맙습니다! –

+0

그래, 내가 제안한 것처럼, 사용중인 타사 라이브러리가 실제로 주 스레드에서 완료 핸들러를 디스패치하지 않는 것처럼 들린다. 주 스레드에서 완료 처리기를 파견하는 것이 표준적인 방법이라는 것을 나는 모르지만 나는 항상 그렇게한다. 스레드를 가지고 놀기 위해 프레임 워크를 사용하는 사람의 책임이되어서는 안됩니다. – jmstone617

+0

굉장합니다. 저에게 양식이 효과가있었습니다. – Harikrishnan

관련 문제