我的新应用程序遇到了小问题。一开始我认为这只是线程的问题,但现在我觉得我必须重写一半代码来修复它。在此之前,你可能会帮助我:)。
现在我正在编写将从服务器下载内容的应用程序。我们有两个对象: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];
}
}];
}
}];
接下来,当我知道服务器上存在差异时,我会同步所有数据。有两种委托方法:beginUploadDataBaseWithNumberOfObjectsToDownload
和fileWasDownloaded
。
首先检查要下载的文件数量。
第二次为下载的文件添加+1。此方法的代表位于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
中有委托方法:
- (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 不想更新其状态。我正在尝试添加调度块,但是如果ProgressView在主线程中没有在屏幕上更改。我试图通过NSTimer
来管理它并发送书籍,但遗憾的是存在问题。
我会很高兴得到任何帮助:)。
答案 0 :(得分:0)
您只需将更新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
}