Find out when all processes in (void) is done?

Posted by Emil on Stack Overflow See other posts from Stack Overflow or by Emil
Published on 2010-06-09T15:07:03Z Indexed on 2010/06/09 15:12 UTC
Read the original article Hit count: 179

Hey.

I need to know how you can find out when all processes (loaded) from a - (void) are done, if it's possible.

Why? I'm loading in data for a UITableView, and I need to know when a Loading... view can be replaced with the UITableView, and when I can start creating the cells.

This is my 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];
}

© Stack Overflow or respective owner

Related posts about iphone

Related posts about objective-c