如何暂停我的应用程序足够长的时间让我的屏幕“赶上”

时间:2011-07-28 22:16:41

标签: ios xcode ipad asihttprequest

我正在做一个同步的网络请求,我想展示进展,但更重要的是,只要我点击“开始”它就会离开比赛,忽略了打开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;
}

1 个答案:

答案 0 :(得分:2)

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

如果要这样做,您应该用两种方法分割代码:

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

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

  3. 假设2.由一个名为offToTheRaces的方法实现,您可以从第一个方法安排此方法:

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

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