2014-09-01 59 views
0

這裏是東西,我得到了一些代碼,不執行(編譯運行的代碼,但什麼都不做)......這裏是代碼...使用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數據,活動指標永遠不會停止並且永不消失。 在之前的視圖中,我使用類似的類下載了另一個文件,並且下載得非常快。將這種觀點/類嘗試進行下載,這是事情...

希望能任何機構可以幫助我,給我的任何建議。謝謝!

+1

你在主線程上更新UI? – Joel 2014-09-01 15:56:14

+0

是的,這是完整的類,它與視圖一起工作,我使用viewDidLoad以dummie數據設置視圖,並在下載完成後用主線程上的正確數據更新所有標籤(用戶正在看到的) – 2014-09-01 16:03:01

+0

主操作隊列是一個串行隊列。一次只能運行一個操作。這是您將操作添加到該隊列的唯一位置嗎?或者你添加其他操作?他們中的任何一個都跑了很長時間還是阻止等待其他東西來完成? – 2014-09-01 16:54:56

回答

1

一種技術我使用,因爲這樣做最有經驗的開發人員來說,就是用斷言還的NSLog(你可以發表評論和關閉),以確認你正在你的代碼假設在事實上也確實如此。嘗試剪切並粘貼下面的代碼,看看會發生什麼 - 它應該有所幫助。在將來,不要將你的頭靠在牆上 - 開始添加斷言和日誌。在某些時候,你會發現一些假設是不真實的。

-(void)URLSessionDidFinishEventsForBackgroundURLSession:(NSURLSession *)session { 
    NSLog(@"Received URLSessionDidFinishEventsForBackgroundURLSession: application state is %d", [UIApplication sharedApplication] applicationState]; 
    AppDelegate *appDelegate = [UIApplication sharedApplication].delegate; 

    [self.session getTasksWithCompletionHandler:^(NSArray *dataTasks, NSArray *uploadTasks, NSArray *downloadTasks) { 
     NSLog(@"Download task count %d", [downloadTasks count]); 
#warning "This looks like incorrect logic, but I don't know this background code. Wouldn't you see "1", or more? Won't you get an array of the tasks you submitted??? 
     if ([downloadTasks count] == 0) { 
      assert(appDelegate.backgroundTransferCompletionHandler); // if this is nil, your app will be stuck in some odd state forever, so treat this like a fatal error 
      void(^completionHandler)() = appDelegate.backgroundTransferCompletionHandler; 
      appDelegate.backgroundTransferCompletionHandler = nil; 

      assert([NSOperationQueue mainQueue]); // why not? 
      assert(![NSOperationQueue mainQueue].isSuspended); // I looked at the class description, the queue **could** be suspended 
      [[NSOperationQueue mainQueue] addOperationWithBlock:^{ 
       NSLog(@"Completion Operation called!"); 
       assert(completionHandler); // perhaps an ARC bug, unlikely, but then you cannot get this code to work, right? 
       completionHandler(); 
      }]; 
     } 
    }]; 
} 
+0

您也可以使用構建系統來禁用發佈版本中的聲明(和NSAsserts),這是我做的,但有些人喜歡留下它們。 – 2014-09-08 13:41:10

相關問題