2010-06-09 4 views
0

J'ai besoin de savoir comment vous pouvez savoir quand tous les processus (chargés) à partir d'un - (void) sont terminés, si c'est possible.Savoir quand tous les processus dans (vide) est fait?

Pourquoi? Je charge des données pour un UITableView, et j'ai besoin de savoir quand une vue Loading... peut être remplacée par UITableView, et quand je peux commencer à créer les cellules.

Ceci est mon code:

- (void) reloadData { 
    NSAutoreleasePool *releasePool = [[NSAutoreleasePool alloc] init]; 

    NSLog(@"Reloading data."); 
    NSURL *urlPosts = [NSURL URLWithString:[NSString stringWithFormat:@"%@", URL]]; 
    NSError *lookupError = nil; 
    NSString *data = [[NSString alloc] initWithContentsOfURL:urlPosts encoding:NSUTF8StringEncoding error:&lookupError]; 
    postsData = [data componentsSeparatedByString:@"~"]; 
    [data release], data = nil; 
    urlPosts = nil; 

    self.numberOfPosts = [[postsData objectAtIndex:0] intValue]; 
    self.postsArrayID = [[postsData objectAtIndex:1] componentsSeparatedByString:@"#"]; 
    self.postsArrayDate = [[postsData objectAtIndex:2] componentsSeparatedByString:@"#"]; 
    self.postsArrayTitle = [[postsData objectAtIndex:3] componentsSeparatedByString:@"#"]; 
    self.postsArrayComments = [[postsData objectAtIndex:4] componentsSeparatedByString:@"#"]; 
    self.postsArrayImgSrc = [[postsData objectAtIndex:5] componentsSeparatedByString:@"#"]; 


    NSMutableArray *writeToPlist = [NSMutableArray array]; 
    NSMutableArray *writeToNoImagePlist = [NSMutableArray array]; 
    NSMutableArray *imagesStored = [NSMutableArray arrayWithContentsOfFile:[rootPath stringByAppendingPathComponent:@"imagesStored.plist"]]; 
    int loop = 0; 
    for (NSString *postID in postsArrayID) { 
     if ([imagesStored containsObject:[NSString stringWithFormat:@"%@.png", postID]]){ 
      NSLog(@"Allready stored, jump to next. ID: %@", postID); 
      continue; 
     } 
     NSLog(@"%@.png", postID); 

     NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:[postsArrayImgSrc objectAtIndex:loop]]]; 

     // If image contains anything, set cellImage to image. If image is empty, try one more time or use noImage.png, set in IB 
     if (imageData == nil){ 
      NSLog(@"imageData is empty before trying .jpeg"); 

      // If image == nil, try to replace .jpg with .jpeg, and if that worked, set cellImage to that image. If that is also nil, use noImage.png, set in IB. 
      imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:[[postsArrayImgSrc objectAtIndex:loop] stringByReplacingOccurrencesOfString:@".jpg" withString:@".jpeg"]]]; 

     } 
     if (imageData != nil){ 
      NSLog(@"imageData is NOT empty when creating file"); 
      [fileManager createFileAtPath:[rootPath stringByAppendingPathComponent:[NSString stringWithFormat:@"images/%@.png", postID]] contents:imageData attributes:nil]; 
      [writeToPlist addObject:[NSString stringWithFormat:@"%@.png", postID]]; 
     } else { 
      [writeToNoImagePlist addObject:[NSString stringWithFormat:@"%@", postID]]; 

     } 
     imageData = nil; 

     loop++; 

     NSLog(@"imagePlist: %@\nnoImagePlist: %@", writeToPlist, writeToNoImagePlist); 
    } 
    NSMutableArray *writeToAllPlist = [NSMutableArray arrayWithArray:writeToPlist]; 

    [writeToPlist addObjectsFromArray:[NSArray arrayWithContentsOfFile:nowPlist]]; 

    [writeToAllPlist addObjectsFromArray:[NSArray arrayWithContentsOfFile:[rootPath stringByAppendingPathComponent:@"imagesStored.plist"]]]; 

    [writeToNoImagePlist addObjectsFromArray:[NSArray arrayWithContentsOfFile:[rootPath stringByAppendingPathComponent:@"noImage.plist"]]]; 

    [writeToPlist writeToFile:nowPlist atomically:YES]; 
    [writeToAllPlist writeToFile:[rootPath stringByAppendingPathComponent:@"imagesStored.plist"] atomically:YES]; 
    [writeToNoImagePlist writeToFile:[rootPath stringByAppendingPathComponent:@"noImage.plist"] atomically:YES]; 
    [releasePool release]; 
} 
+1

Je n'ai jamais fait cela, mais juste spéculer: avez-vous essayé de retourner un BOOL à la toute fin pour que la fonction 'reloadData' retourne TRUE quand elle arrive à ce point? Je suppose (peut-être incorrectement) que l'appareil gère en série les tâches une à la fois, alors donnez-lui un coup de feu. – iwasrobbed

+0

Bien sûr, vous avez raison. Merci :) – Emil

Répondre

0

Il est aussi simple que le retour d'un bool au bas du sélecteur en cours d'exécution en arrière-plan, et la reaload UITableView.

Merci à @iWasRobbed:

Je n'ai jamais fait, mais juste spéculant: avez-vous essayé de retourner un BOOL à la fin de sorte que la fonction reloadData retourne TRUE quand il arrive à ce moment-là? Je suppose (peut-être incorrectement) que l'appareil gère en série les tâches une à la fois, alors donnez-lui un coup de feu.

Questions connexes