2

나는 다음 코드를 사용하여 url을 검사하는 응용 프로그램에서 작업 중입니다.doback while 루프를 사용하여 setbackground 로딩

 
-(void)exists 
{ 
    NSString *strImg = @"some url"; 


    NSURL *aImgUrl = [NSURL URLWithString:strImg]; 

    NSMutableURLRequest *imgRequest = [NSMutableURLRequest requestWithURL:strImg]; 
    [imgRequest setHTTPMethod:@"HEAD"]; 

    imgConnection = [NSURLConnection connectionWithRequest:imgRequest delegate:self]; 
} 

이것은 내가 응답을받을 때하는 일입니다.

 
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
{ 

    if ([(NSHTTPURLResponse *)response statusCode] == 200) 
    { 
     // url exists 
     appDel.isExists = TRUE; 
     temp = FALSE; 
     [self loadData]; 
     NSLog(@"%ld",(long)[(NSHTTPURLResponse *)response statusCode]); 
    } 
    else if([(NSHTTPURLResponse*)response statusCode] == 404) 
    { 
     //url does not exists 
     appDel.isExists = FALSE; 
     temp = TRUE; 
     [self loadData]; 
     NSLog(@"%ld",(long)[(NSHTTPURLResponse *)response statusCode]); 
    } 
} 

이 내 부하 데이터 코드

 
-(void)loadData 
{ 


    result = FALSE; 

    isReqSent = FALSE; 

    do 
    { 
     if (appDel.isExists == TRUE) 
     { 
      NSURL *aTxtUrl = [NSURL URLWithString:apiText]; 
      NSURL *aImgUrl = [NSURL URLWithString:apiImage]; 

      NSURLRequest *imgRequest = [NSURLRequest requestWithURL:aImgUrl]; 
      NSURLRequest *txtRequest = [NSURLRequest requestWithURL:aTxtUrl]; 

      [NSURLConnection sendAsynchronousRequest:txtRequest queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) 
      { 
       if (data) 
       { 
         NSString *strTxt = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]; 
         [lblTime setText:strTxt]; 
          [policyImgWebView loadRequest:imgRequest]; 

         result = TRUE;      
       } 

      }]; 

     } 

     if (result) 
     { 
      appDel.isExists = TRUE; 
     } 
     else 
     { 
      if(!isReqSent) 
      { 
       NSString *soapMessage = @"my soap message"; 

       NSString *soapAction = @"my soap url"; 


       [objWebServiceParser xmlParsing:soapMessage :soapAction :Number]; 
       isReqSent = TRUE; 
      } 

      } 
     if (temp) 
     { 
      dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{ 
       [self backgroundProcess]; 
      }); 
     } 

    } while (result == FALSE); 

이 내 배경 프로세스

내가 잘못하고 있어요 어디에서 모르는
 
-(void)backgroundProcess 
{ 

    NSString *strImg = @"some url"; 

    NSURL *aImgUrl = [NSURL URLWithString:apiImage]; 

    NSMutableURLRequest *imgRequest = [NSMutableURLRequest requestWithURL:aImgUrl]; 
    [imgRequest setHTTPMethod:@"HEAD"]; 

    imgConnection = [NSURLConnection connectionWithRequest:imgRequest delegate:self]; 

} 

입니다, 하나가 나를 인도 할 수 있습니까?

데이터를 수신 할 때까지 백그라운드 프로세스가 계속 실행되기를 원합니다. 데이터가 수신되면 appdel.isExists가 true가됩니다. &이 ui를 업데이트하기 위해 메인 스레드에옵니다.

나는 performSelectorInTheBackground GCD &을 시도하지만 난 오류 (35)


그것은 NSURLSessionDownloadTask에 여전히 더 나은 방법을 찾는 일이 바로 & 내가 NSThread 실패 얻을지고 있지 않다.

+0

당신은 PerformSelectorInTheBackground를 수행하는 것을 의미합니까? –

+0

예 백그라운드로 처리 할 때 NSURLConnection 대리자 메소드에서 응답을받지 못합니다. –

+0

URL에서 이미지를로드하기 만 하시겠습니까? –

답변

0

서버에서 UIImageView의 이미지를로드하려면 아래 프레임 워크를 사용할 수 있습니다. 당신이 당신의 UI를 업데이트하는 데 사용할 수 있습니다 당신은 completionBlock 얻을 SDWebImage

, progressBlock & 더 많은 속성. 블록 단위로 알려주고 UI에서 원하는 것을 업데이트 할 수 있습니다.

샘플 : 그것은 imageURL이 유효한 경우이 코드는 단지 이미지를로드 할만큼 간단

(그것은 당신의 경우입니다)

UIImageView *imageView; 
[imageView setImageWithURL:[NSURL URLWithString:YOUR_URL]]; 

그리고 그것은 또한 같은 다양한 방법이있어

아래
UIImageView *imageView; 
[imageView setImageWithURL:[NSURL URLWithString:YOUR_URL] 
      placeholderImage:[UIImage imageNamed:@"placeholder.png"] 
      completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) 
      { 
       // in "image" you'll get the downloaded image from the URL 
       ... completion code here ... 

      }]; 
2

NSURLConnection으로 전화하지 마세요. NSURLConnection은 자동으로 작동합니다. 비동기 요청을 보내지 않으므로 백그라운드에서 작동하며 기본 스레드를 중단하지 않습니다. 로드 데이터에서 디스패치를 ​​제거하십시오. 당신은이 :

if (temp) 
    { 
     dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{ 
      [self backgroundProcess]; 
     }); 
    } 

그것을 만들 :

if (temp) 
    { 
     [self backgroundProcess]; 
    } 

당신은 파견을 사용할 필요가 없습니다.

+0

while 루프는 많은 양의 메모리를 사용합니다. –

+0

백그라운드에서 메모리 및 수행 작업은 아무 관계도 없습니다. 기억은 완전히 다른 것입니다. ARC를 사용하고 있습니까? –

+0

예 @ 카필 츄 비사 –