AFNetworking的异步get怎么传出数据? 10C

AFNetworking没有同步get,如果我要把获取的数据传出get函数应该怎么做?

异步的话还没等到查询结束就执行下一条语句了。

6个回答

AFHTTPRequestOperationManager*mananger = [AFHTTPRequestOperationManagermanager];
mananger.responseSerializer= [AFHTTPResponseSerializerserializer];
[manangerPOST:@“http://xxxxx?uid=123"parameters:nilsuccess:^(AFHTTPRequestOperation*operation,idresponseObject) {
NSDictionary*dic = [NSJSONSerializationJSONObjectWithData:responseObjectoptions:NSJSONReadingMutableContainerserror:nil];
saveDic= dic;
model= [[AccoundModelalloc]init];
[modelsetValuesForKeysWithDictionary:dic];
NSLog(@"%@",model);

    [selfinitData:model];


}failure:^(AFHTTPRequestOperation*operation,NSError*error) {

}];

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

AFHTTPRequestOperationManager*manager = [AFHTTPRequestOperationManagermanager];
manager.responseSerializer= [AFHTTPResponseSerializerserializer];

//@"uid" @"totalAmount" @"singleAmount" @"validityTime" @"miniDueTime" @"maxDueTime" @"miniRatePy" @"maxRatePy"

[saveDicsetObject:@“123"forKey:@"uid"];
[managerPOST:@“http://xxxx"parameters:saveDicsuccess:^(AFHTTPRequestOperation*operation,idresponseObject) {
   NSDictionary*dic = [NSJSONSerializationJSONObjectWithData:responseObjectoptions:NSJSONReadingMutableContainerserror:nil];

   NSLog(@"%@",dic);

}failure:^(AFHTTPRequestOperation*operation,NSError*error) {

}];

在get的异步回调函数中,可以通过闭包拿到self,然后通过self调用自定义函数

[saveDicsetObject:@“123"forKey:@"uid"];
[managerPOST:@“http://xxxx"parameters:saveDicsuccess:^(AFHTTPRequestOperation*operation,idresponseObject) {
NSDictionary*dic = [NSJSONSerializationJSONObjectWithData:responseObjectoptions:NSJSONReadingMutableContainerserror:nil];

NSLog(@"%@",dic);

}failure:^(AFHTTPRequestOperation*operation,NSError*error) {

}];

业务上有需要的话写个阻塞 直到获取到需要的值后 在下一步调用

可以用信号量去控制,可以尝试一下

使用代码块或者闭包进行数据返回就解决了!
/// 超时时间设置
lazy var sessionManager:Alamofire.SessionManager = {
let configuration = URLSessionConfiguration.default
configuration.timeoutIntervalForRequest = 5
return Alamofire.SessionManager(configuration: configuration)
}()

func getListData(apiStr:String, finshedBlock:@escaping (_ resoult:Any) -> Void) -> Void {
    self.sessionManager.request(apiStr, method: .get).responseJSON { (resoult) in
        let json = resoult.result.value
        finshedBlock(json ?? "error")
    }
}
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐