ボタンのクリックでサーバーを呼び出すと、接続didReceiveData
が返されNull
、アプリケーションがクラッシュします。しかし、データが であるかどうかを確認する条件がif
あり、これを行うためのベスト プラクティスの条件でサーバー呼び出しを再度開始しますか?else
Null
if
-(IBAction)btnClicked:(id)sender
{
NSMutableURLRequest *storeRequest = [NSMutableURLRequest requestWithURL:url];
[storeRequest setHTTPMethod:@"POST"];
[storeRequest setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[storeRequest setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[storeRequest setValue:postLength forHTTPHeaderField:@"Content-Length"];
[storeRequest setHTTPBody:postData];
NSURLConnection *theConnection;
theConnection =[[NSURLConnection alloc] initWithRequest:storeRequest delegate:self];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data1
{
if (data1==nil)
{
[self downloadDetails];//Restart the download
}
else
{
receivedData =[[NSMutableData alloc]init];
[receivedData appendData:data1];
}
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSError* error;
NSMutableArray* jsonArray = [NSJSONSerialization
JSONObjectWithData:receivedData
options:kNilOptions
error:&error];
[appDelegate.inboxArray addObjectsFromArray:jsonArray];
}