Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 对多个呼叫使用NSURLConnection didReceiveData_Ios_Objective C_Nsurlconnection - Fatal编程技术网

Ios 对多个呼叫使用NSURLConnection didReceiveData

Ios 对多个呼叫使用NSURLConnection didReceiveData,ios,objective-c,nsurlconnection,Ios,Objective C,Nsurlconnection,我想在这里得到一些设计/代码建议 我有一个应用程序需要调用web服务的3个API。这些发生在不同的视图中,但我需要将这3个API中的所有信息放在一个对象中。因此,我将一个对象从视图控制器传递到另一个视图控制器,并在对web服务进行https调用时向其添加数据 我创建了一个名为PostToServer的函数,该函数根据“类型”调用webapi,并选择适当的URL用于发布。但只会有一个didReceiveData调用。我将api的“类型”存储在对象中的一个变量中,以便在didReceiveData中

我想在这里得到一些设计/代码建议

我有一个应用程序需要调用web服务的3个API。这些发生在不同的视图中,但我需要将这3个API中的所有信息放在一个对象中。因此,我将一个对象从视图控制器传递到另一个视图控制器,并在对web服务进行https调用时向其添加数据

我创建了一个名为PostToServer的函数,该函数根据“类型”调用webapi,并选择适当的URL用于发布。但只会有一个didReceiveData调用。我将api的“类型”存储在对象中的一个变量中,以便在didReceiveData中可以适当地解析响应


是否有更好的方法使用相同的NSURLConnection代码来处理多个webapi调用?我是obj-c新手,因此希望确保正确使用语言结构。

他们是NSURLConnection类的一个委托“-(void)connection:(NSURLConnection*)connection didReceiverResponse:(NSURResponse*)response”,其中他们是参数响应,此响应具有属性“URL”通过它,您可以知道此响应是针对哪个URL请求的。
“didReceiveResponse”方法被调用一次,然后“didReceiveData”被调用,因此您可以进行相应的检查和逻辑。

我使用AFNetworking而不是NSURLConnection。如果需要3个请求来构建一个对象,则可以将该对象从一个连接传递到另一个连接并添加缺少的数据

    NSString *urlString = ...

    NSDictionary *parameters = @{@"username": username,
                                 @"password": password,
                                 };

    AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] init];
    [manager.requestSerializer setTimeoutInterval:10.0];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/json"];

    [manager POST:urlString parameters:parameters success:^(NSURLSessionDataTask *task, id responseObject) {
        Person *person = [Person new];
        person.name = [responseObject valueForKey:@"name"];
        person.city = [responseObject valueForKey:@"city"];

       [self callSecondRequest:person];


    } failure:^(NSURLSessionDataTask *task, NSError *error) {


    }];

}
方法
callSecondRequest:
将从另一个请求向对象添加数据

另一个选项是同时运行3个共享同一对象的请求