Objective-cでNSurlsessionを使用してボタンを押したら指定したウェブサイトの文字列を取得し、
その文字列をlabelに表示させるプログラムを作りました。
しかし、問題が発生しました。
表示までに10秒前後かかってしまいます。
ボタンを押した瞬間にラベルに表示させるにはどうしたらいいでしょうか?
以下がソースになります。
//ボタンから外部カウント数値呼び出し
-
(void)mame_count_value {
// NSURLSessionConfiguration を作成する
NSURLSessionConfiguration* sessionConfig = [NSURLSessionConfiguration defaultSessionConfiguration];// NSURLSession を作成する
URLsession = [NSURLSession sessionWithConfiguration:sessionConfig delegate:nil delegateQueue:nil];// リクエストを作成する
// API (GET http://amazingengine.co.jp/mame/counter.php )NSURL* URL = [NSURL URLWithString:@"http://amazingengine.co.jp/mame/counter.php"];
request = [NSMutableURLRequest requestWithURL:URL];
request.HTTPMethod = @"GET";// NSURLSessionDataTask を作成し、通信処理を登録する
task = [URLsession dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if (error == nil) {
// データを取り出す
responseStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];//ダブルクォーテーション削除・改行削除 responseStr = [responseString stringByReplacingOccurrencesOfString:@"\"" withString:@""]; responseStr = [responseString stringByReplacingOccurrencesOfString:@"\\r\\n" withString:@"\r\n"]; //カウント数値をラベルに代入 count_value.text = [NSString stringWithFormat : @"%@", responseStr]; NSLog(@"B:%@",count_value.text); } else { // 失敗時エラーログ表示 NSLog(@"NSURLSessionDataTask Failed: %@", [error localizedDescription]); }
}];
// 通信開始
[task resume];
}
//カウンターボタン押された場合
-
(IBAction)counter_btn_down:(id)sender {
[self mame_count_value];
}
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2015/12/03 12:23