ios - 将线程返回的信息添加到数组中

标签 ios objective-c multithreading nsinvocationoperation

我对 Objective C 比较陌生,我正在尝试从 URL 读取信息,然后将其信息添加到我的 Name 对象中,然后将名称对象添加到数组中。我能够很好地从 URL 中读取信息,而且我成功地将它们的信息添加到数组中,但是我似乎无法成功地将名称添加到我的 nameArray 中。我在顶部声明数组并在 viewDidLoad 函数中初始化它,然后当我调用 readURLOne、readURLTwo 和 readURLThree 方法时,名称将被添加到数组中。但是,当我检查数组时,在 viewDidLoad 中调用这些方法后,它是空的。为什么会这样?

@interface ViewController () {
    Name *n1;
    Name *n2;
    Name *n3;
    NSMutableArray *nameArray; //this is the array I am having trouble with
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    nameArray = [[NSMutableArray alloc] init];
    NSOperationQueue *queue = [NSOperationQueue new];
    NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(readURLOne)object:nil];
    [queue addOperation:operation];

    operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(readURLTwo)object:nil];
    [queue addOperation:operation];

    operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(readURLThree) object:nil];
    [queue addOperation:operation];
    NSLog(@"NAME ARRAY: %lu", (unsigned long)[nameArray count]); //This is where I am trying to check if they are added to the array, the are not
} 

-(void) readURLOne {
    NSURL *dataURL = [NSURL URLWithString:@"http://example.org/data/1.txt"];
    NSArray *tmp = [NSArray arrayWithContentsOfURL:dataURL];
    for (NSString *str in tmp){ //add contents of URL successfully to tmp
        NSLog(@"%@", str);
    }
    n1 = [[Name alloc] init];
    [n1 setFirstName:tmp[0]];
    [n1 setLastName:tmp[1]];
    [n1 setNumber:tmp[2]];
    [nameArray addObject:n1];
 }

-(void) readURLTwo {
    NSURL *dataURL = [NSURL URLWithString:@"http://example.org/data/2.txt"];
    NSArray *tmp = [NSArray arrayWithContentsOfURL:dataURL];
    for (NSString *str in tmp){
        NSLog(@"%@", str);
    }
    n2 = [[Name alloc] init];
    [n2 setFirstName:tmp[0]];
    [n2 setLastName:tmp[1]];
    [n2 setNumber:tmp[2]];
    [nameArray addObject:n2];
}

-(void) readURLThree {
    NSURL *dataURL = [NSURL URLWithString:@"http://example.org/data/3.txt"];
    NSArray *tmp = [NSArray arrayWithContentsOfURL:dataURL];
    for (NSString *str in tmp){
        NSLog(@"%@", str);
    }
    n3 = [[Name alloc] init];
    [n3 setFirstName:tmp[0]];
    [n3 setLastName:tmp[1]];
    [n3 setNumber:tmp[2]];
    [nameArray addObject:n3];
}

-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return nameArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIdentifier = @"Cell";
    CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell) {
        cell = [[CustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }

    cell.firstName.text = [[nameArray objectAtIndex:indexPath.row] getFirstName];
    cell.lastName.text = [[nameArray objectAtIndex:indexPath.row] getLastName];
    return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

最佳答案

I declare the array at the top and initialize it in the viewDidLoad function then furthermore the names are meant to be added to the array when I call the methods readURLOne, readURLTwo and readURLThree.

如我所见,您没有调用 readURLOne、readURLTwo 和 readURLThree - 您正在将它们添加到 NSOperationQueue。在这种情况下,您的方法被异步调用,并且 nameArray 可能会在您在 viewDidLoad 结束时检查它后更新。

将您的方法更改为:

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    nameArray = [[NSMutableArray alloc] init];
    NSOperationQueue *queue = [NSOperationQueue new];
    queue.maxConcurrentOperationCount = 1; // It is needed to avoid synchronisation problems with nameArray
    NSInvocationOperation *operation1 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(readURLOne)object:nil];
    [queue addOperation:operation1];

    NSInvocationOperation *operation2 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(readURLTwo)object:nil];
    [queue addOperation:operation2];

    NSInvocationOperation *operation3 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(readURLThree) object:nil];
    [queue addOperation:operation3];


    NSOperation *finalOperation = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"NAME ARRAY: %lu", (unsigned long)[nameArray count]);
    }];
    [finalOperation addDependency:operation1];
    [finalOperation addDependency:operation2];
    [finalOperation addDependency:operation3];
    [queue addOperation:finalOperation];
} 

关于ios - 将线程返回的信息添加到数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28864807/

相关文章:

iphone - MKAnnotation 图像偏移与自定义引脚图像

c++ - openMP 的 SIMD 构造是否需要特定类型的硬件?

ios - UIImageView 在具有不同高度的自定义 UITableViewCell 内自动调整大小

ios - AFNetworking:我的类 NSURLResponse 中的 setRedirectResponseBlock 总是返回空值

iphone - 通过iOS无法保存核心数据对象

java - 关闭连接后 Socket 和 SwingWorker 不工作

java - 如何从actionEvent监听器通知主线程

ios - 我应该如何对这个复杂的网格进行编程?

ios - 尝试构建 DisplayCandy 时,终端给我这个错误

objective-c - 使用自定义类的 NSMutableArray 作为数据源设置 NSTableView