【问题标题】:How can I pause my app long enough for my screen to "catch up"我怎样才能暂停我的应用程序足够长的时间让我的屏幕“赶上”
【发布时间】:2011-07-28 22:16:40
【问题描述】:

我正在做一个同步网络请求,我想显示进度,但更重要的是,一旦我点击“开始”,它就会开始比赛,忽略诸如打开 NetworkActivty 微调器或更改标签之类的事情.

我意识到进度条可能必须等到我有时间异步编写提取,但是我可以让程序等到标签改变吗?或者至少是一种伪造的方法?

在我拥有的 UIButton 上的 TouchUpInside 上。 . .

-(IBAction)startButtonPressed

{ [UIApplication sharedApplication].networkActivityIndi​​catorVisible = YES;

NSString *lastUpdate = [[self getLastUpdateDate] stringByReplacingOccurrencesOfString:@" " withString:@"%20"];
startButton.enabled = NO;
cancelButton.enabled = NO;
resetSwitch.enabled = NO;
cleanSwitch.enabled = NO;
//check the # of customer updates and update accordingly

statusLabel.text = @"Updating Customers. . .";
//[self performSelector:(@selector(updateProgress:withStatus:)) withObject:0.0f withObject:@"Updating Customers. . . " afterDelay:500];
//[self performSelector:(@selector(updateProgressIndicators)) withObject:nil afterDelay:0];

if (clean) {
    [self deleteALL];
}

if ([self customersCount] < 1000 && !reset)
    [self addCustomers:lastUpdate];
else
    [self addCustomers:nil];

//check the # of product updates and update accordingly, reset does not affect products
statusLabel.text = @"Updating Products. . .";

if ([self productsCount] < 25000 || !clean)
    [self addProducts:lastUpdate];
else
    [self addProducts:nil];

//check the # of vendor updates and update accordingly, reset does not affect vendors
statusLabel.text = @"Updating Vendors. . . ";
if ([self vendorsCount] < 1000 || !clean || [[self fetchVendor:@"0002"] count] == 0) {
    [self addVendors:lastUpdate];
}else{
    [self addVendors:nil];
}

statusLabel.text = @"Updating History. . . ";
[self getHistory];



statusLabel.text = @"Updating Custom Pricing. . . ";
if ([self customPricesCount] < 500 && !reset) {
    [self addCustomPrices:lastUpdate];
}else{
    [self addCustomPrices:nil];
}

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"MM-dd-yyyy HH:mm:ss"];
[self setLastUpdateDate:[dateFormatter stringFromDate:[NSDate date]]];  
[dateFormatter release];

statusLabel.text = @"Done!";
cancelButton.enabled = YES;
cancelButton.tag = 1;
cancelButton.titleLabel.text = @"Close";
startButton.enabled = NO;

[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}

【问题讨论】:

    标签: ios xcode ipad asihttprequest


    【解决方案1】:

    您有一种可能性是安排网络请求在主线程上执行。这将允许在发送网络请求之前更新 UI。

    为此,您应该将代码拆分为两种方法:

    1. UI 变化(活动指示器等)直到同步网络请求;

    2. 同步网络请求和响应处理(带有 UI 更新);

    假设 2. 由一个名为 offToTheRaces 的方法实现,您将从第一个方法开始调度此方法:

    [self performSelector:@selector(offToTheRaces) withObject:nil afterDelay:0];
    

    请注意,您并没有以这种方式进行任何类型的多线程处理,也没有引入延迟。您只需安排在再次执行主循环后执行的方法,这将使 tue UI 有机会得到更新。

    【讨论】:

    • 太棒了!你塞尔吉奥,是一位绅士和学者!完美!
    猜你喜欢
    • 2012-10-18
    • 2021-01-18
    • 2019-11-12
    • 1970-01-01
    • 1970-01-01
    • 2020-03-24
    • 1970-01-01
    • 2014-09-07
    • 1970-01-01
    相关资源
    最近更新 更多