带块的NURLSession批处理请求。发布参数已更改

时间:2015-07-09 10:22:49

标签: ios objective-c iphone nsurlsession nsurlsessiondatatask

- (void)someMethodThatIsCalledTwice{

    NSURLSession *session = [NSURLSession sharedSession];
            NSString *noteDataString = [NSString stringWithFormat:@"album_id=%@&pic_id=%@", albumId,photoId];
            NSURL *url = [NSURL URLWithString:stringUrl];
            NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
            NSLog(@"PARAMS: %@", noteDataString);
            request.HTTPBody = [noteDataString dataUsingEncoding:NSUTF8StringEncoding];
            request.HTTPMethod = @"POST";

            NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {

            }];
}

这个问题是,如果我两次通过不同的 request.HTTPBody 调用此方法,则 dataTask 会从第二次调用中获取 noteDataString 第一次和第二次通话。

1 个答案:

答案 0 :(得分:0)

albumIdphotoId作为参数传递给someMethodThatIsCalledTwice:,例如

- (void) someMethodThatIsCalledTwiceForAlbumId:(NSString *)albumId photId:(NSString *)pahotId {

     //do the stuff
}