UIAlertView中悬而在后台线程加载数据 [英] UIAlertview hanging while the thread in background is loading data

查看:295
本文介绍了UIAlertView中悬而在后台线程加载数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为刷新数据的UIBarButtonItem。当用户点击它,应用程序应该刷新其数据。什么情况该按钮的点击是Web服务推出,XML数据带来的,他们的顺序30000-40000记录。因此,要prevent UI从挂我写了一个后台线程,并做到了装载有。

I have a UIBarButtonItem called "Refresh Data". When user clicks it, the app should refresh its data. What happens on that button click is web services are launched and xml data is brought and they are of the order 30000-40000 records. So to prevent UI from hanging i wrote a background thread and did that loading there.

- (void)refreshDataAction
{
NSLog(@"Refresh Data");
//Put up an alert box indicating user to wait while data is loading.

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Data is Loading"
                                                        message:@"Please wait while data is being refreshed."
                                                       delegate:self
                                              cancelButtonTitle:@"OK"
                                          otherButtonTitles:nil, nil];
alert.tag = 10;
[alert show];
 }

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSLog(@"hit in clickedbuttonatindex alertview at 259");
self.refreshActIndicator.hidden = NO;
[self.refreshActIndicator startAnimating];
 dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,
                                         (unsigned long)NULL), ^(void) {
    [self getAllCustomerValues];

    NSError *nwerror = nil;
    if (![self.secondMOC save:&nwerror])
    {
        NSLog(@"209 Failed to save second MOC");
    }
    else
    {
        //NSLog(@"saved success");
    }
 });
}

就像你所看到的警报视图中显示出来。我点击确定,盒子仍然存在其熏黑,其在屏幕上有挂。然后7-8秒钟后框消失和动画开始了活动的指标。我的目标是得到这样的事情。
1.用户点击刷新数据按钮。
2.Alert视图显示出来。
3.用户点击确定。
4.Alert框消失,并立即在后台线程开始工作,我看到了活动的指标工作/动画。

Like you can see alert view shows up. I click OK and the box is still there its "blackened" and its hanging there on the screen. And then 7-8 seconds later the box disappears and the animation starts for activity indicator. My goal is to get something like this. 1.User clicks on the refresh data button. 2.Alert view shows up. 3.User clicks OK. 4.Alert box disappears and immediately the background thread starts working and i see the activity indicator working/animating.

用户会知道,而活动的指标是动画等。所以,我怎么应用程式启动用户点击确定后和alertview确定不会立刻变黑的动画。难道我清楚了吗?如果您需要了解更多信息,请询问。谢谢

User will know to wait while the activity indicator is animating. So how do i get the app to start animating immediately after user clicks "OK" and the alertview OK not be blackened. Am i clear?If you need more information, please ask. Thanks

编辑:这屏幕有一个后台线程处理它在加载时第一次的那一天。有一个画面在此之前。在这我有继续按钮,点击该启动后台线程这不完全一样的东西像这样在不这儿过得杀死它或任何东西。

This screen has a background thread working on it when it loads the first time for that day. There is a screen before this. On it i have continue button and clicking that launches background thread which does exactly the same thing like this one over here.I dont kill it or anything.

编辑2:

 - (void)refreshDataAction
   {
NSLog(@"Refresh Data");
self.txtCustomerSearch.text =@"";
[self cleanUPPreviousLabels];

//Put up an alert box indicating user to wait while data is loading.

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Data is Loading"
                                                message:@"Please wait while data is being refreshed."
                                               delegate:self
                                      cancelButtonTitle:@"OK"
                                      otherButtonTitles:nil, nil];
//alert.tag = 10;
//[alert show];
[alert performSelectorOnMainThread:@selector(show) withObject:nil waitUntilDone:FALSE];

NSLog(@"hit in willpresenet alertview at 221");
    self.refreshActIndicator.hidden = NO;
    [self.refreshActIndicator startAnimating];
NSLog(@"Dispatching");

//Disable the view and all the other controls
self.txtCustomerSearch.userInteractionEnabled =NO;
self.txtCustomerSearch.enabled =NO;
self.btnSearch.enabled =NO;
self.btnSearch.userInteractionEnabled = NO;
self.scrollView.userInteractionEnabled = NO;
//self.view.userInteractionEnabled =NO;
self.chkButton.enabled = NO;


[self deletePreviousValues]; 

dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
    NSLog(@"Getting customer values");
    [self getAllCustomerValues];
    NSLog(@"Got customer values");

    NSError *nwerror = nil;
    if (![self.secondMOC save:&nwerror])
    {
        NSLog(@"209 Failed to save second MOC");
    }
    else
    {
        //NSLog(@"saved success");
    }


    self.txtCustomerSearch.userInteractionEnabled = YES;
    self.txtCustomerSearch.enabled =YES;
    self.btnSearch.enabled =YES;
    self.btnSearch.userInteractionEnabled = YES;
    self.scrollView.userInteractionEnabled = YES;
    self.view.userInteractionEnabled =YES;
    self.chkButton.enabled = YES;

    [self.refreshActIndicator stopAnimating];

    NSLog(@"Saved");


});
NSLog(@"Dispatched");

}

推荐答案

您说这一切发生在后台线程 - 确保你加载UIAlertView中的主线上:

You say this is all happening on a background thread - make sure you load the UIAlertView on the main thread:

//[alert show];
[alert performSelectorOnMainThread:@selector(show) withObject:nil waitUntilDone:FALSE];

这可能会解决这个问题。您的异步调用实际发生的运行循环当前线程(因为它是一个后台线程)上,阻止你试图显示(在相同的运行循环)警报视图。

This will probably fix the problem. Your async call is actually happening on the current thread in the run loop (since it's a background thread), blocking the alert view you're attempting to display (in the same run loop).

这篇关于UIAlertView中悬而在后台线程加载数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆