2014-04-07 3 views
0

Im 종류의 새로운 목표 C 및 누군가가 내 iOS 응용 프로그램에 .plist 파일을 다운로드 한 다음 (자습서를 가리킨다면) 파일을 읽고 비동기식으로 다운로드해야하는지 궁금합니다. 다운로드하는 동안 앱을 일시 중지하지 않습니다.비동기식으로 다운로드

내가 사용하고 현재의 코드는 다음과 같습니다

//UERootArray = [[NSArray alloc] initWithContentsOfURL:[NSURL URLWithString:@"file to url"]]; 

필자는 온라인을 많이 보였다 및 자습서를 찾을 수 없습니다, 나는이 간단하지만 당신의 도움이 많이 주시면 감사하겠습니다 알고있다. 감사합니다. .

+0

온라인에서 파일을 가져와야하거나 컴퓨터에 파일을 가져와야합니까? –

답변

0

이렇게하려면 NSURLConnection을 사용할 수 있습니다.

또는

당신은 단순히처럼,이에 대한 GCD을 사용할 수 있습니다 : 당신이 당신의 앱 다운로드가 완료되었음을 알고 싶어하는 경우

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 
    UERootArray = [[NSArray alloc] initWithContentsOfURL:[NSURL URLWithString:@"file to url"]]; 
}); 
+0

다운로드가 완료되면 어떻게 알 수 있습니까? –

+0

'initWithContentsOfURL' 다음에 'NSNotificationCentre'를 사용하여 알림을 게시하거나'[self doneLoading]'같은 메소드를 호출 할 수도 있습니다. – Paulw11

+0

고맙습니다! 덕분에 많은 시간을 아끼 셨습니다. @Midhun –

0

그 후 다음 몇 가지 작업을 수행하고 싶어 이 경우 앱 다운로드가 완료되면 업데이트되는 맞춤 위임자를 작성해야합니다. 그러나 비동기 다운로드의 경우 Midhun에서 언급 한대로 GCD를 사용합니다. 이것을 참조하십시오 How to write Custom Delegate?

0

다음은 NSURLConnection을 사용하여 구현 된 스케치입니다. 다운로드가 완료되면 (확인 또는 오류와 함께) completionHandler이 호출되며 여기에서 Array을 처리하는 함수를 호출 할 수 있습니다.

여기에 제공된 기타 답변도 유효하며 궁극적으로 귀하의 케이스에 가장 적합한 것을 찾아야합니다.

NSURLRequest* theRequest = [NSURL URLWithString:@"file to url"]; 
[NSURLConnection sendAsynchronousRequest:theRequest 
            queue:[NSOperationQueue mainQueue] 
         completionHandler:^(NSURLResponse* theResponse, NSData* theData, NSError* theError) { 
    if (theData) { 
     NSError* err = nil; 
     id Array [NSPropertyListSerialization propertyListWithData:theData 
                  options:NSPropertyListImmutable 
                  format:NULL 
                   error:&err]; 
     if ([Array isKindOfClass:[NSArray class]) { 
      // Do whatever you need with downloaded array 
     } else { 
      // Error -- wrong data, check err 
     } 
    } else { 
     // Error while downloading, check theError 
    } 
}]; 
0

완성 핸드로 다운로드하십시오.

NSMutableURLRequest *request=[[NSMutableURLRequest alloc]initWithURL:[NSURL URLWithString:@"http://"]]; 


dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue currentQueue] completionHandler:^(NSURLResponse *response,NSData *data,NSError *error) 
    { 

    //do your stuff when downloading complete.. 
} 
관련 문제