0

I'm using the popular class calle SSZipArchive to unzip a file, concretely this method:

+ (BOOL)unzipFileAtPath:(NSString *)path toDestination:(NSString *)destination progressHandler:(void (^)(NSString *entry, unz_file_info zipInfo, long entryNumber, long total))progressHandler completionHandler:(void (^)(NSString *path, BOOL succeeded, NSError *error))completionHandler

And I'm updating two uilabels text in the blocks inside, both of them are allocated, both of their texts are changed before, when I print the content of the uilabels the text is updated but the screen doesn't update it, I have to say that this method is loaded when I finish the download of a zip into a delegate method.

My code:

- (void)downloadManager:(id)sender finishedDownload:(ANDownload *)download {  
    if ( [[NSFileManager defaultManager] fileExistsAtPath:download.storeFile] ) {
      NSLog(@"Download exists");
      [self.labelStep setText:CustomLocalizedString(@"ZIP_DECOMPRESSING_MSG", nil)];

      [SSZipArchive unzipFileAtPath:download.storeFile toDestination:self.saveFolderPath progressHandler:^(NSString *entry, unz_file_info zipInfo, long entryNumber, long total) {
          //Your main thread code goes in here
          NSString * labelProgressText = [NSString stringWithFormat:@"%ld / %ld", entryNumber, total];
          self.labelProgress.text = labelProgressText;
       } completionHandler:^(NSString *path, BOOL succeeded, NSError *error) {
         NSLog(@"Succeeded %d in path: %@", succeeded, path);
         if(succeeded){
         .....
Cœur
  • 37,241
  • 25
  • 195
  • 267
peig
  • 418
  • 3
  • 11
  • Show your code, not `unzipFileAtPath:...` method signature. How do you use it? Post as much related code as possible ... – zrzka Jul 24 '15 at 08:26
  • Do you update the label in `if (succeed) {...}`? – sbarow Jul 24 '15 at 09:11
  • No, in the suceed I do some stuff and present another view controller... I update labelStep text and labelProgress text where you can see it... – peig Jul 24 '15 at 09:28

1 Answers1

1

You are probably not on the main thread, you can try this :

dispatch_async(dispatch_get_main_queue(), ^{
    //Your main thread code goes in here
    yourLabel.text = @"new text";       
});

EDIT

Since you are on the main thread, and you want to update your labels right away you would need this :

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    dispatch_async(dispatch_get_main_queue(), ^{
        //Your main thread code goes in here
        yourLabel.text = @"new text";       
    });
}

However, i don't understand why it is not updated at the end of your method in your example. There might be a better solution.

streem
  • 9,044
  • 5
  • 30
  • 41
  • Ok, you are right, I'm probably not in the main thread, but if I use your way it updates the text just when the method finish and it is not what I'm looking for... – peig Jul 24 '15 at 08:39
  • Hmm that is weird, this behavior suggest that you are actually in the main thread, can you check what `[NSThread isMainThread];` returns ? – streem Jul 24 '15 at 08:49
  • @peig i've edited my answer so that your label will update right away but i believe there might be a better solution. – streem Jul 24 '15 at 09:40
  • Yeah, it is updated at the end of the method but what I want is to see the progressive updates of progressHandler – peig Jul 24 '15 at 10:03