块内的AFNetworking和异步数据

时间:2012-07-08 14:14:14

标签: objective-c ios afnetworking

我的代码:

-(void)addressLocation:(NSArray *)inputData {
NSString *plusAddress = [[inputData objectAtIndex:0] stringByReplacingOccurrencesOfString:@" " withString:@"+"];
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/geocode/json?address=%@,%@&sensor=false&region=pl", plusAddress, [inputData objectAtIndex:1]]];

__block CLLocationCoordinate2D newRecord;
NSURLRequest *request = [NSURLRequest requestWithURL:url];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
    NSArray *results = [JSON valueForKey:@"results"];
    NSNumber *latNumber = [[results objectAtIndex:0] valueForKeyPath:@"geometry.location.lat"];
    NSNumber *lngNumber = [[results objectAtIndex:0] valueForKeyPath:@"geometry.location.lng"];
    CGFloat lat = [latNumber floatValue];
    CGFloat lng = [lngNumber floatValue];
    newRecord = CLLocationCoordinate2DMake(lat, lng);
}  failure:nil];
[operation start];
[operation waitUntilFinished];

KantorPoint *newPoint = [[KantorPoint alloc] init];
newPoint.coordinate = newRecord;
newPoint.name = [inputData objectAtIndex:1];
newPoint.address = [inputData objectAtIndex:0];
[self.pointersArray addObject:newPoint];
}

我正在尝试从this回答中的操作块传输newRecord。但是newPoint.coordinatenull,我不能在阻止内部执行类似return newRecord的操作(获取错误)。有办法解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

我不确定你的目标,但我会以这种方式重写代码。

- (void)addressLocation:(NSArray *)inputData {

    NSString *plusAddress = [[inputData objectAtIndex:0] stringByReplacingOccurrencesOfString:@" " withString:@"+"];
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/geocode/json?address=%@,%@&sensor=false&region=pl", plusAddress, [inputData objectAtIndex:1]]];

    __block YourClass *selfBlock = self; // __weak YourClass *selfBlock = self; if you use ARC

    NSURLRequest *request = [NSURLRequest requestWithURL:url];

    AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {

        NSArray *results = [JSON valueForKey:@"results"];
        NSNumber *latNumber = [[results objectAtIndex:0] valueForKeyPath:@"geometry.location.lat"];
        NSNumber *lngNumber = [[results objectAtIndex:0] valueForKeyPath:@"geometry.location.lng"];
        CGFloat lat = [latNumber floatValue];
        CGFloat lng = [lngNumber floatValue];
        CLLocationCoordinate2D newRecord = CLLocationCoordinate2DMake(lat, lng);

        KantorPoint *newPoint = [[KantorPoint alloc] init];
        newPoint.coordinate = newRecord;
        newPoint.name = [inputData objectAtIndex:1];
        newPoint.address = [inputData objectAtIndex:0];
        [selfBlock.pointersArray addObject:newPoint];

    }  failure:nil];

    // the queue needs to be allocated somewhere...
    [[self operationQueue] addOperation:operation];
}

如果使用ARC,请注意:__block不会阻止保留对象。请改用__weak

修改

基于 phix23 ,您需要在某处保留您的操作。这是一种简单的方法,可以创建类型为NSOperationQueue的属性,并将操作添加到该队列。将其添加到队列时,您无需在操作上执行start

希望有所帮助。