2012-04-15 9 views
0

私はアプリケーションの応答性を高めようとしていますが、デバッガコンソールをチェックアウトしているうちに、アプリケーションがXMLデータを2回取得しているように見えています。 。 。つまり、ページが表示されたら、いくつかのxmlデータを取得して解析し、オブジェクトの配列を返すバックグラウンドキューをディスパッチします。私は、nslogを使用してコンソールにデータ文字列を入力します。何らかの理由でそれを2回印刷しています。 HERESに私の関連するコード:VCでiOSのバックグラウンドでデータを2回取得していますか?

-(void)startBackgroundQueue{ 
dispatch_async(backgroundThread, ^(void){ 
    SDJConnection *connection = [[SDJConnection alloc]init]; 
    self.dataArray = [connection getVideoData]; 

    [[NSNotificationCenter defaultCenter]postNotificationName:@"tube_data_loaded" object:nil]; 
    }); 
} 

SDJConnectionでgetVideoData方法は、次のようになります。

-(NSMutableArray *)getVideoData { 

NSURL *dataURL = [[NSURL alloc]initWithString:@"https://gdata.youtube.com/feeds/api/users/dancingastronaut/uploads?start-index=1&max-results=10"]; 
NSData *data = [[NSData alloc]initWithContentsOfURL:dataURL]; 

NSString *dataString = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]; 
NSLog(@"data string: %@",dataString); 

YouTubeParser *parser = [[YouTubeParser alloc]initWithData:data]; 
[parser setDelegate:parser]; 

[parser parse]; 

    return [parser videosArray]; 

} 

、その後、VCはこのコードを実行します。

-(void)backgroundQueueDone{ 
dispatch_async(dispatch_get_main_queue(),^(void){ 
    [activityIndicator stopAnimating]; 
    if ([_dataArray count] >1) { 
     self.tableView.separatorColor = [UIColor darkGrayColor]; 
     [self.tableView reloadData];}     
}); 
} 

これは問題なのかどうかわかりませんが、何とか実行してリソースを無駄にしているのではないかと心配していますこれを2回繰り返す。何が起こっているかもしれないかに関するアイデア?

ありがとうございます!

答えて

1

メソッド "startBackgroundQueue"を2回コールした可能性はありますか?私の謙虚な意見では、ブロックは一度実行されるだろう。

なぜブロックの結果を得るために通知を使用するのですか?

);あなたの完了通知を節約

dispatch_async(backgroundThread, ^(void){ 
    SDJConnection *connection = [[SDJConnection alloc]init]; 
    self.dataArray = [connection getVideoData]; 

    dispatch_async(dispatch_get_main_queue(),^(void){ 
     [activityIndicator stopAnimating]; 
     if ([self.dataArray count] >1) { 
      self.tableView.separatorColor = [UIColor darkGrayColor]; 
      [self.tableView reloadData];}     
    }); 
}); 

:あなたのようなネストされたブロックを使用することができます

関連する問題