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);

このコードをperformSelectorInBackground:indidEnterBackgroundメソッドで実行しています。

サーバーのテーブルを更新しないという奇妙な問題が時々発生します。

デリゲート メソッドでの使用に問題がありperformSelectorますか、それともこのリクエストを非同期に変更する必要がありますか?

4

1 に答える 1

1

アプリがバックグラウンドに移行するときに、ネットワーク リクエストを開始しているように見えます。これは、たとえば、ユーザーがアプリを閉じると、ネットワーク トランザクションが開始されることを意味します。これは、Apple が意図するアプリの動作方法ではありません (理想的には)。

applicationDidEnterBackground のドキュメントから: (太字は私のものです):

このメソッドの実装では、タスクを実行して を返すのに約 5 秒かかります。最終タスクを実行するために追加の時間が必要な場合は、beginBackgroundTaskWithExpirationHandler: を呼び出して、システムから追加の実行時間を要求できます。実際には、できるだけ早く applicationDidEnterBackground: から戻る必要があります。時間切れになる前にメソッドが戻らない場合、アプリケーションは終了し、メモリから消去されます。

そのため、ネットワーク トランザクションが必ずしも十分な速さで終了していないことがわかります。

アプリを見直すことをお勧めします。 applicationDidEnterBackground:おそらく、この作業を行うのに適切な時期ではありません。バックグラウンドで何らかの作業を行う必要がある場合は、バックグラウンドタスクの使用例を参照してください。コードをバックグラウンド タスクに配置すると、同期要求または非同期要求のいずれかで使用できるようになります。NSURLConnection

于 2013-05-30T04:42:46.953 に答える