2013-05-30 4 views
0

내 코드 :비동기 호출

NSMutableArray *sstr = [[DBModel database]getCName]; 
NSArray *aarr = [[sstr objectAtIndex:0] componentsSeparatedByString:@"!"]; 

acName = [aarr objectAtIndex:0]; 
acMobileno = [aarr objectAtIndex:1]; 

NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http:///userstatus.php?name=%@&mobileno=%@&status=off",[aarr objectAtIndex:0],[aarr objectAtIndex:1]]]; 

NSMutableURLRequest *postRequest = [[NSMutableURLRequest alloc]initWithURL:url]; 
[postRequest setHTTPMethod:@"POST"]; 

NSString *stringBoundary = @"------------------------14737809831466499882746641449"; 
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",stringBoundary]; 
[postRequest addValue:contentType forHTTPHeaderField: @"Content-Type"]; 
NSMutableData *postBody = [NSMutableData data]; 

//name 
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
[postBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"name\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 
[postBody appendData:[[NSString stringWithFormat:@"%@",[aarr objectAtIndex:0]] dataUsingEncoding:NSUTF8StringEncoding]]; 
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
//mobileno 
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
[postBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"mobileno\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 
[postBody appendData:[[NSString stringWithFormat:@"%@",[aarr objectAtIndex:1]] dataUsingEncoding:NSUTF8StringEncoding]]; 
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
//status 
NSString *status = @"off"; 
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
[postBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"status\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 
[postBody appendData:[[NSString stringWithFormat:@"%@",status] dataUsingEncoding:NSUTF8StringEncoding]]; 
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]]; 

[postRequest setHTTPBody:postBody]; 

NSData *returndata=[NSURLConnection sendSynchronousRequest:postRequest returningResponse:nil error:nil]; 

NSString *string1=[[NSString alloc]initWithData:returndata encoding:NSUTF8StringEncoding]; 

NSLog(@"OFF Status report = %@",string1); 

내가 didEnterBackground 방법 performSelectorInBackground:이 코드를 실행하고 있습니다.

때로는 나에게 서버의 테이블을 갱신하지 않는 이상한 문제를 제공합니다.

대리자 performSelector 방법을 사용으로 문제가 아니면 비동기이 요청을 변경해야 하는가?

+0

UI 변경 사항은 주 스레드에서 가장 잘 수행됩니다. 당신이 백그라운드 스레드에서 행동으로 수행되면, 다시'사용하여 메인 스레드 호출 - (무효)가 performSelectorOnMainThread을 (SEL) aSelector withObject : (ID) 인수 waitUntilDone : (BOOL) wait' 테이블을 업데이트 .. – Roshit

+0

오른쪽 .. 내 나쁜 .. ** ** 테이블 ** 서버 ** 테이블을 업데이트하는 대신 ** 업데이트 테이블을 읽으십시오. – Roshit

답변

1

당신은 앱이 배경에가는 네트워크 요청 시작할 것으로 보인다. 예를 들어 사용자가 앱을 닫으면 네트워크 트랜잭션이 시작됩니다. 이는 애플이 일하기를 애플이 의도하는 방식이 아니다 (이상적으로는). the documentation on applicationDidEnterBackground:에서

(굵은 광산) : 에 약 5 초 어떤 작업을 수행하고를 반환있다 이 방법의

귀하의 구현입니다. 당신이 어떤 최종 작업을 수행하는 데 시간이 더 필요한 경우에는 beginBackgroundTaskWithExpirationHandler를 호출하여 시스템에서 추가 실행 시간을 요청할 수 있습니다 :. 연습에서 가능한 한 빨리 applicationDidEnterBackground : 으로 돌아와야합니다. 메서드가 실행되기 전에 반환되지 않으면 응용 프로그램이 종료되고 메모리에서 제거됩니다.

따라서 네트워크 트랜잭션이 항상 신속하게 완료되는 것은 아닙니다.

내가 앱을 다시 생각 권하고 싶습니다. applicationDidEnterBackground:은 아마도이 작업을 수행 할 적절한시기가 아닙니다. 백그라운드에서 실제로 작업해야하는 경우 배경 을 사용하는 this example을 참조하십시오. 코드를 백그라운드 작업에 넣으면 NSURLConnection을 동기식 인 또는 비동기 요청과 함께 사용할 수 있어야합니다.

+0

willEnterBackground 대리자 메서드를 사용하여 좋은 옵션이 무엇입니까? – Xcode

+0

어떤 방법으로 이야기하고 있습니까? 나는 단지'applicationDidEnterBackground :'메소드 만 알고있다. 'applicationWillEnterForeground :'가 있지만 사용자가 ** 앱을 다시 열 때 **를위한 것입니다. – Nate

+0

sorry.i 틀린 질문 – Xcode