显示AlertView,使用GCD解析和解除AlertView

时间:2013-05-06 18:18:25

标签: ios objective-c grand-central-dispatch

我对iOS很新,如上面的问题所述;我试图做这3个简单的步骤。

  1. 显示提醒视图
  2. 解析内容
  3. 解除警报
  4. 我正在寻找类似于我们在Android中的内容,即Pre Execute,doInBackground和Post Execute()。

    这就是我的尝试。

    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];
    
       });
    
    });
    

    以下是fetchResult方法。

    - (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];
    
    
    }
    

    这是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];
    

    当我运行上面的代码时,Alerview会在一秒钟内显示并解散。在方法上添加日志我观察到fetchresults方法立即返回并且警报视图被解除。但是fetchResults关联的线程(连接方法,Parser方法)继续执行但是alerview被解除。

    我需要一个指导如何阻止代码,直到完成所有相关方法。

    感谢您的时间。

2 个答案:

答案 0 :(得分:1)

我知道这不是你想要的答案,但不要使用警报视图。覆盖耗时活动的一个很好的方法是建立一个UIActivityIndi​​catorView,或者一个包含一个的视图,并将其设置为旋转:

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

您还可以在耗时的活动发生时阻止用户交互,共享应用程序对象的beginIgnoring...(并在完成时将其与endIgnoring...关闭)。但是,显然你不能这样做,如果要给用户一个取消按钮。在这种情况下,用userInteractionEnabled为YES的不可见视图(清晰的背景颜色)覆盖其他所有内容,以便它可以用于除按钮之外的任何其他操作。

此外,使用dispatch_sync几乎永远不会正确答案。一旦你以我刚才描述的方式冻结了界面,你就可以进行连接(异步)和解析(在后台线程上),然后回到主线程来解除活动指示。

最后,如果出现问题,你会想要离开自己。例如,您可以运行NSTimer。

编辑:现在对于实际问题的实际答案,即为什么我的代码没有暂停,即使我使用dispatch_sync:这是因为[[NSURLConnection alloc] initWithRequest:request delegate:self]立即返回;网络是另一个背景线程。所以你的startParse返回,你的fetchResults返回,同时网络继续,NSURLConnection委托方法在一段时间后被调用。

答案 1 :(得分:0)

以下是the link您要找的内容MBProgressHUD 首先在viewDidLoad

中分配它的MBProgressHUD实例
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中显示的Activity指示符将被隐藏,而委托方法将被调用

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

希望它会对你有所帮助。