2014-10-25 2 views
0

누군가가 도와 줄 수 있습니까?블록 완료시기를 알아 보는 방법

약 1000 개의 데이터 항목이 포함 된 NSMutablearray가 있습니다.

내가 원하는 것은 이러한 항목을 하나씩 차례로 보내고 HTTP 요청을 사용하여 서버로 보냅니다.

그러나 그 중요한 내가 한 번에 하나씩 보내, 그래서 내가 이상적으로 수행 할 작업을 다음 의사 코드

for(int i =0; i < array.count; i++) 
{ 
    [ServerLayer ServerUploadRow:[array objectAtIndex:i] : ^void (int ReturnValue, BOOL err){ 
     if(err == true) 
     { 
      //Do some local stuff here. 
     } 
    }]; 
} 

을 가지고 다음

for(int i =0; i < array.count; i++) 
{ 
    //Wait here till i know that we not waiting on a block to complete 

    [ServerLayer ServerUploadRow:[array objectAtIndex:i] : ^void (int ReturnValue, BOOL err){ 
     if(err == true) 
     { 
      //Do some local stuff here. 
      //Signify that we done 
     } 
    }]; 
} 

같은 것을 가지고있다 ServerUploadRow 함수는 HTTP 명령을 전송합니다. 당신이이 작업을 시작 했죠

- (void)initiateRequestNumber:(NSInteger)index 
{ 
    [ServerLayer ServerUploadRow:array[index] : ^(int ReturnValue, BOOL err){ 
     if (err == true) { 
      //Do some local stuff here. 
     } else { 
      NSInteger nextIndex = index + 1; 

      if (nextIndex < array.count) { 
       [self initiateRequestNumber:nextIndex]; 
      } else { 
       // do whatever you want now that everything is done 
      } 
     } 
    }]; 
} 

그리고 : 비동기 ServerUploadRow 실행, 당신은 요청을 할 수 있다고 가정

감사

+0

[sendSynchronousRequest] (https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSURLConnection_Class/index.html#//apple_ref/occ/clm/NSURLConnection)해야합니다./sendSynchronousRequest : returningResponse : 오류 :) –

답변

2

는 이전의 완료 블록의 다음 요청을 시작

[self initiateRequestNumber:0]; 

이러한 요청을 동시에 실행할 수있는 경우 또는 b 이 모든 것을 하나의 요청으로 결합하면 훨씬 빨라집니다. 가능한 경우 웹 서비스를 리팩토링하여이 기능을 활성화하는 것이 좋습니다. 그러나 순차적으로 1000 건의 네트워크 요청을 제출하면 네트워크 대기 시간이 1000 배로 길어지기 때문에 엄청나게 느려질 것입니다.