2

我正在使用下面的代碼檢查url的應用程序。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變爲真&獲取主線程來更新ui。

我試圖GCD & performSelectorInTheBackground,但我沒有得到它的權利&我得到NSThread失敗的錯誤35.


它曾與NSURLSessionDownloadTask但仍在尋找更好的選擇。

+0

你的意思是performSelectorInTheBackground? –

+0

是的當我處理到背景我沒有在NSURLConnection委託方法中得到響應。 –

+0

你只是想從一個URL加載圖像? –

回答

0

要從服務器加載您的UIImageView中的圖像,可以使用下面的框架。

SDWebImage

你得到completionBlockprogressBlock &很多屬性,你可以用它來更新你的UI。它會以塊形式通知您,然後您可以在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

是@Kapil Choubisa –