我是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
答案 0 :(得分:1)
我在顶部声明数组并在viewDidLoad函数中初始化它,然后当我调用方法readURLOne,readURLTwo和readURLThree时,名称将被添加到数组中。
我可以看到你没有调用readURLOne,readURLTwo和readURLThree - 你将它们添加到NSOperationQueue。在这种情况下,您的方法被异步调用,并且在viewDidLoad结束时检查它后,nameArray可能会更新。
将您的方法更改为:
- (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];
}
答案 1 :(得分:0)
NSOperationQueue
以异步方式运作,因此,当您访问nameArray
时,无法保证它们完整。
您可以在NSLog
:
[queue waitUntilAllOperationsAreFinished];
但实际上,我认为您应该直接调用函数而不使用NSOperationQueue
。
在进一步检查您正在呼叫的方法时,您似乎正在执行网络操作。这绝对应该在你已经完成的不同线程上完成,所以你真的只需要为完成添加一个回调。您可以通过添加finish
操作来完成此操作:
NSInvocationOperation *completionOperation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(urlDataRead) object:nil];
// ...
[completionOperation addDependency:operation];
// ...
[completionOperation addDependency:operation];
// ...
[completionOperation addDependency:operation];
[queue addOperation:completionOperation];
}
- (void)urlDataRead {
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
}