【问题标题】:Find out when all processes in (void) is done?找出 (void) 中的所有进程何时完成?
【发布时间】:2010-06-09 15:07:03
【问题描述】:

如果可能的话,我需要知道如何找出来自- (void) 的所有进程(加载)何时完成。

为什么?我正在为 UITableView 加载数据,我需要知道何时可以将 Loading... 视图替换为 UITableView,以及何时可以开始创建单元格。

这是我的代码:

- (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];
}

【问题讨论】:

  • 我从来没有这样做过,只是推测:您是否尝试过在最后返回一个 BOOL 以便reloadData 函数在到达该点时返回 TRUE?我假设(可能不正确)该设备一次处理一个任务,所以试一试。
  • 当然,你是对的。谢谢:)

标签: iphone objective-c memory-management process


【解决方案1】:

就像在后台运行的选择器底部返回一个布尔值一样简单,然后重新加载UITableView

感谢@iWasRobbed:

我从未这样做过,只是推测:您是否尝试过在最后返回一个 BOOL 以便 reloadData 函数在到达该点时返回 TRUE?我假设(可能是错误的)该设备一次处理一个任务,所以试一试。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-01-26
    • 2015-05-27
    • 2022-01-23
    • 1970-01-01
    • 1970-01-01
    • 2021-03-30
    • 2011-06-26
    • 1970-01-01
    相关资源
    最近更新 更多