1

そのため、サーバーから 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

以上です。

4

1 に答える 1

3

最も簡単な方法は、sendAsynchronousRequest:queue:completionHandler: を使用することです。デリゲートは必要ありません。connectionDidFinishLoading: からのコードを完了ブロックに入れるだけです。

URL 要求のデータをロードし、要求が完了または失敗したときに操作キューでハンドラー ブロックを実行します。

[NSURLConnection sendAsynchronousRequest:request
   queue:[NSOperationQueue mainQueue]
   completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
        < your code >
}];
于 2013-09-27T10:34:54.177 に答える