0

_queue는 NSOperationQueue 개체입니다.네비게이션 스택에서 해당 뷰가 팝업되면 백그라운드 작업을 완료하는 방법

[_queue addOperationWithBlock:^{ 
    //POST request used to upload photo to server 
    //request has already been successfully configured before this step 
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; 
}]; 

이 몇 초 정도 걸릴 수 있습니다 내가 탐색 컨트롤러의 뒤로 가기 (back) 버튼을 누르면 연결이 닫히고 이미지가 업로드되지 않습니다 : 나는 다음을 사용하여 서버에 이미지를 업로드 할 수 있습니다. 보기 컨트롤러가 탐색 스택에서 팝 되어도이 백그라운드 작업을 수행하려면 어떻게해야합니까?

sendSynchronousRequest가 더 이상 사용되지 않는다는 것을 알고 있습니다.

+1

'NSURLConnection'을 사용하여 동기 요청을 보내는 대신'NSURLSessi on '을 비동기 요청과 함께 사용합니다. 동기 요청은 스레드를 차단합니다. 세션은 사용하기 쉽고 스레드를 차단하지 않으며 실행 큐를 유지합니다. – clemens

답변

1

아마도 _queue는 뷰 컨트롤러의 멤버 변수입니까? 그렇다면 초기 수정 작업을 신속하게 수행하여 정적 멤버 변수 (수명을 변경하기 위해)로 변경할 수는 있지만 모델 클래스로 이동하여 모델 업로드 이미지를 업로드하는 것이 좋습니다 뷰 컨트롤러
이 대신이 비동기되고, 특히되면, 더 나은 디자인을 선도 - 이미지를이 시나리오 :

- view controller A starts the upload 
- user navigates to view controller B 
- upload fails and you need to notify the user of the failure or retry the upload 
- now what? A started the upload but it no longer exists how do you notify the user or retry the upload? 

당신이 그것을 다음이 이미지를 업로드 할 수있는 모델의 책임 한 경우하면 상황 :

- view controller A starts the upload 
- user navigates to view controller B 
- upload fails and you need to notify the user or retry 
- model broadcasts a notification the upload has failed or just retires the upload 
- a meta view controller listens for the notification and displays a dialog to the user informing of the failure 
+1

정적 변수, 특히 동기식 요청은 피해야합니다. – clemens

+0

고마워요, 지금은 정적이라고 선언했습니다. 나는 그것을 결국 모델 클래스로 옮길 것이다. –

+4

@macmoonshine, 나는 "빠른 수정"이라고 말했다. 즉 장기적인 해결책이 아닙니다. – Gruntcakes

관련 문제