gpt4 book ai didi

ios - UIProgressView 不更新状态

转载 作者:行者123 更新时间:2023-12-01 16:29:08 26 4
gpt4 key购买 nike

我的新应用程序有小问题。一开始我以为这只是线程的问题,但现在我觉得我必须重写一半的代码来修复它。在此之前,也许你会帮助我:)

现在我正在编写将从服务器下载东西的应用程序。我们有两个对象:MainViewController UI 在哪里和 DataManager我在哪里处理文件。一开始,我正在检查服务器上的更改 DataManager :

[NSURLConnection sendAsynchronousRequest:[[NSURLRequest alloc] initWithURL:dataURL] queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
if (error) {
//Data was not downloaded
} else {
//Downloading data base
[[NSOperationQueue mainQueue] addOperationWithBlock:^{

if (dataFileExists) { //compare and save downloaded data base: localDataArray i downloadedDataArray

if ([localDataArray isEqual:DownloadedDataArray]) {
[self.delegate dataWasLoaded];
}
} else {

[self synchronizeAllTheFiles]; //Data base sync

[self.delegate dataWasLoaded];
}

}];
}
}];

接下来,当我知道服务器上存在差异时,我会同步所有数据。有两种委托(delegate)方法: beginUploadDataBaseWithNumberOfObjectsToDownloadfileWasDownloaded .

首先检查要下载多少文件。

其次将+1添加到下载的文件。此方法的委托(delegate)在 MainViewController .
- (void)synchronizeAllTheFiles {
NSMutableArray *arrayWithLinksToDownload = [[NSMutableArray alloc] init]; //Table of links

[self.delegate beginUploadDataBaseWithNumberOfObjectsToDownload:[arrayWithLinksToDownload count]]; //How many things are to download

for (int d = 0; d < [arrayWithLinksToDownload count]; d++) { //Runloop to download links
//Downloading Data

if (urlData) { //If data was downloaded

[urlData writeToFile:filePath atomically:YES]; //Save on device

[self.delegate fileWasDownloaded]; //MainViewController Add next downloaded file
}
}
}
MainViewController 中有委托(delegate)方法:
- (void)beginUploadDataBaseWithNumberOfObjectsToDownload:(int)numberToDownload {
IfDownloadingData = YES;
ItemsToDownloadCount = numberToDownload;
ItemsDownloadedCount = 0;
}

- (void)fileWasDownloaded {
ItemsDownloadedCount++;
float progress = (float)ItemsDownloadedCount/(float)ItemsToDownloadCount;
NSLog(@"%ld/%li - %f", (long)ItemsDownloadedCount, (long)ItemsToDownloadCount, progress);
[self.LoadingProgressView setProgress:progress animated:YES];
}

问题是 ProgressView 不想更新他们的状态。我正在尝试添加调度 block ,但如果 ProgressView 在主线程中,屏幕上根本不会改变。我正在尝试通过 NSTimer 来管理它和调度书籍,但不幸的是问题存在。

我会很高兴得到任何帮助:)

最佳答案

您只需将更新 GUI 代码放在主线程中,无需其他任何内容。

 dispatch_async(dispatch_get_main_queue(), ^{
[self.LoadingProgressView setProgress:progress animated:YES];
});

然后乘坐 addOperationWithBlock线:
[NSURLConnection sendAsynchronousRequest:[[NSURLRequest alloc] initWithURL:dataURL] queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
if (error) {
//Data was not downloaded
} else {
// **Remove this line**
// [[NSOperationQueue mainQueue] addOperationWithBlock:^{

if (dataFileExists) { //compare and save downloaded data base: localDataArray i downloadedDataArray

if ([localDataArray isEqual:DownloadedDataArray]) {
[self.delegate dataWasLoaded];
}
} else {

[self synchronizeAllTheFiles]; //Data base sync

[self.delegate dataWasLoaded];
}
// **Remove this line**
// }];
}
}];

而且,我看不到你从哪里得到 urlData .但无论如何,这个循环不应该在主线程中,因为它会使主线程保持忙碌,因此无法更新您的 GUI。
for (int d = 0; d < [arrayWithLinksToDownload count]; d++) { //Runloop to download links

//Downloading Data

if (urlData) {
//If data was downloaded
[urlData writeToFile:filePath atomically:YES];
//Save on device
[self.delegate fileWasDownloaded]; //MainViewController Add next downloaded file
}

关于ios - UIProgressView 不更新状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33064513/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com