2014-09-01 58 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

相关问题