そのため、サーバーから iOS クライアントにデータをダウンロードしています。データはフォーマットする必要があるため、データのダウンロードが完了したら NSNotification を使用してアプリに通知し、ダウンロードが完了したら、データをフォーマットして画面に表示します。これはすべてクールですが、データのサイズが原因で画面がフリーズします。UI が引き続き応答するように、GCD を使用してデータのダウンロードを別のスレッドにプッシュする必要があると考えました。私がそれをしたとき、私はデータをダウンロードしていないようです。データをダウンロードする方法getTops
があります。NSURLConnection
最初に、viewDidLoad
私はこのメソッドを呼び出し、正常に機能しましたが、GCDを次のように使用しました
dispatch_queue_t getItemsQ = dispatch_queue_create("get Items", NULL);
dispatch_async(getItemsQ, ^{
[self getTops];
});
そして、それは機能しなくなりました。getTops
コンソールにログが表示されるので、に到達することはわかっていますが、-(void)connectionDidFinishLoading:(NSURLConnection *)connection
これが私が使用したコードです:
-(void)getTops{
Keychain *keychain = [[Keychain alloc]init];
NSString *auth_token = [keychain getAuthToken];
NSLog(@"at getTops");
topimageArray = [[NSMutableArray alloc]init];
NSURLConnection *connection;
webData = [[NSMutableData alloc]init];
NSURL *url = [[NSURL alloc]initWithString:base];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]init];
[request setURL: url];
[request setValue:auth_token forHTTPHeaderField:@"X-AUTH-TOKEN"];
connection = [NSURLConnection connectionWithRequest:request delegate:self];
// [connection start];
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
NSLog(@"at getTops conn start");
}
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
NSLog(@"recieved response");
[webData setLength:0];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
if(data) {
NSLog(@"Appending data");
[webData appendData:data];
}
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
NSArray *response= [NSJSONSerialization JSONObjectWithData:webData options:0 error:nil];
NSLog(@"Tops full response::: %@",response);
theArray = [[NSArray alloc]initWithArray:response];
NSLog(@"DONE");
//Notify that the data is ready to be formated
[[NSNotificationCenter defaultCenter]postNotificationName:@"getTops" object:nil];
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
NSLog(@"error::::%@", error);
NSString *errormsg = error.localizedDescription;
UIAlertView *alertview = [[UIAlertView alloc]initWithTitle:@"Error" message:errormsg delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alertview show];
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}
削除する必要があるかもしれないと思った[UIApplication sharedApplication].networkActivityIndicatorVisible
が、それは役に立たなかった
編集::デリゲート メソッドに NSLogs を追加しました。私が得るログは
at getTops conn start
以上です。