這裏是東西,我得到了一些代碼,不執行(編譯運行的代碼,但什麼都不做)......這裏是代碼...使用NSURLSessionDelegate
和[NSOperationQueue mainQueue] addOperationWithBlock
爲什麼不NSOperationQueue執行與addOperationWithBlock提交了塊?
@interface tablaPosViewController : UIViewController <NSURLSessionDelegate>
@end
@implementation tablaPosViewController()
- (void)viewDidLoad
{
//some code to set the view, labels and stuff
[self downloadTheHTMLdata] // this code download some data from WWW
}
- (void)downloadTheHMTLdata
{
//some code to set the session using an object
[object.downloadTask resume]; //Begins the download
}
- (void)toFixTablaPosiciones
{
//some code to do with the downloaded data from WWW (and HTML sheet)
//here, parse the HTML Sheet, and put some data into an Arrays, and another public vars
//call another method
[self PutTheDataInLabel];
}
- (void)PutTheDataInLabel
{
//some code to put all the data in every label
//take the public vars that was set in the previous method, and do some code with it
//and put the info into the labels
//call another method
[self MoreStuff];
}
- (void)MoreStuff
{
//some code..
}
-(void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location
{
//When the download finish this method fire up!
//this is to copy file from tmp folder to Docs folder
if ([fileManager fileExistsAtPath:[destinationURL path]])
{
[fileManager removeItemAtURL:destinationURL error:nil];
}
BOOL success = [fileManager copyItemAtURL:location //TMP download folder
toURL:destinationURL //Documents folder
error:&error];
//HERES COMES THE TROUBLE!!!
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
[self toFixTablaPosiciones]; //Call this method, that has other method calls!
}];
}
@end
UPDATE
此隊列中的另一個代碼put方法...
-(void)URLSessionDidFinishEventsForBackgroundURLSession:(NSURLSession *)session{
AppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
[self.session getTasksWithCompletionHandler:^(NSArray *dataTasks, NSArray *uploadTasks, NSArray *downloadTasks) {
if ([downloadTasks count] == 0) {
if (appDelegate.backgroundTransferCompletionHandler != nil) {
void(^completionHandler)() = appDelegate.backgroundTransferCompletionHandler;
appDelegate.backgroundTransferCompletionHandler = nil;
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
completionHandler();
}];
}
}
}];
}
的問題是,當下載文件結束,調用-(void)URLSession:(NSURLSession *)session downloadTask:...
方法,我等待[[NSOperationQueue mainQueue] addOperationWithBlock:^{...
運行è東西......但它不執行任何[self toFixTablaPosiciones]
!!。
我跑的代碼一步一步,我看怎麼編譯運行所有的代碼,在法法......但鑑於從未更新,運行,但不執行,根本就做什麼,我有一個活動指標,並且想要阻止它,但是這些標籤仍然具有demumie數據,活動指標永遠不會停止並且永不消失。 在之前的視圖中,我使用類似的類下載了另一個文件,並且下載得非常快。將這種觀點/類嘗試進行下載,這是事情...
希望能任何機構可以幫助我,給我的任何建議。謝謝!
你在主線程上更新UI? – Joel 2014-09-01 15:56:14
是的,這是完整的類,它與視圖一起工作,我使用viewDidLoad以dummie數據設置視圖,並在下載完成後用主線程上的正確數據更新所有標籤(用戶正在看到的) – 2014-09-01 16:03:01
主操作隊列是一個串行隊列。一次只能運行一個操作。這是您將操作添加到該隊列的唯一位置嗎?或者你添加其他操作?他們中的任何一個都跑了很長時間還是阻止等待其他東西來完成? – 2014-09-01 16:54:56