ios - 如何暂停我的应用程序足够长的时间以使我的屏幕显示为 "catch up"

标签 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;
}

最佳答案

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

为了做到这一点,您应该将代码分成两种方法:

  1. UI变化(activity indicator等)直到同步网络请求;

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

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

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

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

关于ios - 如何暂停我的应用程序足够长的时间以使我的屏幕显示为 "catch up",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6866443/

相关文章:

xcode - 使用 XCode 和 BitBucket 理解 Git

c++ - C++ 中的 Facebook 图形 API

ios - 如果响应通知调用 view.window.center 为 0,0,否则为 160,284

objective-c - 如何将单例类设置为委托(delegate)?

swift - slider 在 iPhone 上使程序崩溃,但在模拟器上运行

iphone - 如何在 iPhone 中实现搜索栏?

css - 变换:scale to fullscreen?

ios - UIScrollview 在 ipad 上不工作

ios - 在 iPad 应用程序中显示来自 JSON 的数据表

ios - 调用drawrect时xcode报告 "libsystem_kernel.dylib __abort_with_payload"SIGABRT