0
여기

는 일이 내가 가지고 (컴파일러가 코드를 실행하지만 아무것도하지 않음) 실행되지 않습니다 일부 코드, ... 여기 코드가 NSURLSessionDelegate[NSOperationQueue mainQueue] addOperationWithBlockNSOperationQueue가 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를 사용하여 ...입니다

대기열이 다른 코드 풋 방법 ...

-(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]의 anythig는 실행되지 않습니다 !! !!

나는 코드를 한 단계 씩 실행했다. 컴파일러가 모든 코드를 메소드를 통해 실행하는 방법을 본다.하지만 뷰가 업데이트되지 않고 실행되지는 않지만 단순히 아무것도하지 않는다. 표시기를 멈추고 싶지만 lablels는 dummie 데이터로 계속 남아 있고 활동 표시기는 절대로 멈추지 않으며 절대로 사라지지 않습니다. 이전보기에서 비슷한 파일을 사용하여 다른 파일을 다운로드하고 매우 빠르게 다운로드합니다. 이 뷰/클래스에 가서 다운로드를 수행하려고하면이게 문제가됩니다. ...

어떤 신체가 나에게 도움이 될 수 있으면 어떤 조언을 나에게 보낼 수 있습니다. 감사!

+1

주 스레드에서 UI를 업데이트하고 있습니까? – Joel

+0

예, 이것은 뷰와 함께 작동하는 완전한 클래스이며 viewDidLoad를 사용하여 dummie 데이터가있는 뷰를 설정하고 다운로드 완료 후에 메인 스레드 (사용자가보고있는 내용)의 올바른 데이터로 모든 레이블을 업데이트합니다 –

+0

주 작업 대기열은 직렬 대기열입니다. 한 번에 하나의 작업 만 실행할 수 있습니다. 이 큐에 작업을 추가하는 유일한 장소입니까? 또는 다른 작업을 추가합니까? 그들 중 누구라도 오랜 시간 동안 실행되거나 완료하기 위해 다른 것을 기다리는 것을 차단합니까? –

답변

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)을 사용하지 않도록 설정할 수도 있습니다. 그러나 일부 사람들은이 빌드를 남겨두고 싶습니다. –

관련 문제