0

I m very new to iOS, as stated in the question above; im trying to do these 3 simple step.

  1. Show Alert view
  2. Do parsing stuff
  3. Dismiss Alert

I was looking for something like we have in android i.e Pre Execute, doInBackground and Post Execute().

This is what i have tried.

parserAlert = [[UIAlertView alloc] initWithTitle:@"Loading" message:@"Please Wait"    delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];

[parserAlert show];

dispatch_queue_t queue = dispatch_queue_create("com.abc.testing",  DISPATCH_QUEUE_SERIAL);

dispatch_sync(queue,^{
   DBHandler *myDB= [[DBHandler alloc] init];
   [myDB fetchResults];
       dispatch_async(dispatch_get_main_queue(),^{
         [parserAlert dismissWithClickedButtonIndex:0 animated:YES];

   });

});

Below is the fetchResult method.

- (void) fetchResults
{

 IParser *i = [[IParser alloc] init];
 [i startParse];


 AGParser *ag = [[AGParser alloc] init];
 [ag startParse];


 GParser *g = [[GParser alloc] init];
 [g startParse];

 HParser *h = [[HParser alloc] init];
 [h startParse];

 SParser *s = [[SParser alloc] init];
 [s startParse];


}

This is startParse.

NSString *url = @"http://abcd.com/Service_URL/Service.asmx/GetNotes";

NSURL *nsUrl = [[NSURL alloc] initWithString:url];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:nsUrl];

NSURLConnection *con = [[NSURLConnection alloc] initWithRequest:request delegate:self];

responseData = [[NSMutableData alloc] init];

[con start];

When i run the above code, Alerview show and dismiss within a second. Adding logs on methods i observed that fetchresults method return immediately and alert view gets dismiss. However fetchResults associated threads(Connection methods, Parser methods) keep executing but alerview is dismissed.

I need a guideline how to block the code until all associated methods are finished.

Thanks for your time.

4

2 に答える 2

1

これはあなたが望む答えではないことはわかっていますが、これにはアラート ビューを使用しないでください。時間のかかるアクティビティをカバーする良い方法は、UIActivityIndi​​catorView、またはそれを含むビューを配置し、回転するように設定することです。

http://www.apeth.com/iOSBook/ch25.html#_uiactivityindicatorview

また、共有アプリケーション オブジェクトを使用して、時間のかかるアクティビティが発生している間、ユーザーの操作を防ぐこともできますbeginIgnoring...(完了したら、それをオフにしendIgnoring...ます)。ただし、ユーザーに [キャンセル] ボタンが表示される場合は、明らかにそれを行うことはできません。その場合、他のすべてを非表示のビュー (クリアな背景色) で覆い、userInteractionEnabledYES に設定して、ボタン以外のすべてのタッチを食べるようにします。

また、を使用するのが正しい答えになることはほとんどありませんdispatch_sync。先ほど説明した方法でインターフェイスをフリーズしたら、接続 (非同期) と解析 (バックグラウンド スレッドで) を実行してから、メイン スレッドに戻ってアクティビティ インジケーターを閉じることができます。

最後に、問題が発生した場合に備えて、抜け道を残しておきたいと思うでしょう。たとえば、NSTimer を実行できます。

編集:そして今、あなたの実際の質問に対する実際の答え、つまり、私が使用したにもかかわらず私のコードが一時停止しないのはなぜですかdispatch_sync:それ[[NSURLConnection alloc] initWithRequest:request delegate:self]はすぐに戻るからです。ネットワークはさらに別のバックグラウンド スレッドにあります。したがって、あなたのstartParseリターン、あなたのfetchResultsリターン、そしてその間ネットワークは継続し、NSURLConnectionデリゲートメソッドはしばらくして呼び出されます.

于 2013-05-06T18:23:41.697 に答える
0

ここにあなたが探しているリンクMBProgressHUDがあります 最初にそのMBProgressHUDインスタンスをviewDidLoad

MBProgressHUD *progressHUD = [[MBProgressHUD alloc] initWithView:self.view];
progress.delegate=self;
[progressHUD showWhileExecuting:@selector(performBackgroundTask) onTarget:self withObject:nil animated:YES]

そしてバックグラウンドメソッドで

-(void)performBackgroundTask
{
    //Do some stuff
}

)performBackgroundTaskmethod のタスクが完了するとすぐに、MBProgressHUD意志に表示されるアクティビティ インジケーターが非表示になり、デリゲート メソッドが呼び出されます。

-(void)hudWasHidden
{ 
   //Do additional stuff after completion of background task 
}

それがあなたを助けることを願っています。

于 2013-05-06T18:34:29.693 に答える