나는 다음 코드를 사용하여 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
실패 얻을지고 있지 않다.
당신은 PerformSelectorInTheBackground를 수행하는 것을 의미합니까? –
예 백그라운드로 처리 할 때 NSURLConnection 대리자 메소드에서 응답을받지 못합니다. –
URL에서 이미지를로드하기 만 하시겠습니까? –