gpt4 book ai didi

iphone - 调用 API 后不等待响应

转载 作者:塔克拉玛干 更新时间:2023-11-02 10:10:19 27 4
gpt4 key购买 nike

问题是我正在调用 megnto api,但是当 api 返回对象之前,所有其他语句都已执行。

NSMutableArray *list=[Magento.service startSession];

NSLog(@"Record is:%@",list);

我修改的startSession方法是:

- (NSMutableArray *)startSession
{
NSString *_sessionID;
NSNumber *nsPage =[NSNumber numberWithInt:1];
NSNumber *nsData =[NSNumber numberWithInt:10];
NSMutableArray *listOfName = [[NSMutableArray alloc] init];
NSArray *args = [NSArray arrayWithObjects:nsPage,nsData,nil ];
@synchronized(self) {
_sessionID = sessionID;
}
if (_sessionID != FAILED_SESSION)
dispatch_group_enter(session_group);
[client postPath:@"login" parameters:@{@"username": MAGENTO_USERNAME, @"apiKey": MAGENTO_API_KEY} success:^(AFHTTPRequestOperation *operation, id responseObject) {
sessionID = responseObject;



dispatch_group_leave(session_group);

[client postPath:@"call" parameters:@{@"sessionId":sessionID, @"resourcePath":@"callforprice_collection.getCallForPriceCollection",
@"args":args
} success:^(AFHTTPRequestOperation *operationData, id responseData) {

NSLog(@"Response : %@",responseData);
[listOfName addObject:[responseData valueForKey:@"name"]];

} failure:^(AFHTTPRequestOperation *operationData, NSError *error) {
NSLog(@"Response is not get");
sessionID = FAILED_SESSION;
}];


NSLog(@"got session %@", sessionID);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
sessionID = FAILED_SESSION;
}];
return listOfName;

}

o/p 是

2013-05-03 06:27:16.272 CallForPrice[3120:c07] Record is: ()
2013-05-03 06:27:19.836 CallForPrice[3120:c07] got session 88ef34087a12809334ae3a0c839f85b6
** response that i have printed in method**
2013-05-03 06:27:22.048 CallForPrice[3120:c07] Response : (
{
"callforprice_id" = 13;
country = Austria;
"created_time" = "2013-05-03 12:03:05";
emailid = "indies.tester@gmail.com";
message = hfhftrfygjh;
name = "Richard Conover";
"phone_no" = 8977896;
"product_name" = Ottoman;
"product_options" = "";
"update_time" = "0000-00-00 00:00:00";
},
{
"callforprice_id" = 12;
country = "Antigua and Barbuda";
"created_time" = "2013-05-03 12:01:24";
emailid = "indies.tester@gmail.com";
message = sdfsdfsdfwe;
name = "Richard Conover";
"phone_no" = 5645;
"product_name" = Ottoman;
"product_options" = "";
"update_time" = "0000-00-00 00:00:00";
},
{
"callforprice_id" = 11;
country = Angola;
"created_time" = "2013-05-03 11:58:51";
emailid = "indies.tester@gmail.com";
message = 6tyrtyryrty;
name = "Kaitlyn Matheson";
"phone_no" = 564564;
"product_name" = Ottoman;
"product_options" = "";
"update_time" = "0000-00-00 00:00:00";
},

startSession 正在显示记录,实际发生的是 NSLOG 在调用 do response 之前打印。如何解决这个问题.s

最佳答案

1) 在 Magento.h 中的所有导入之后添加这一行

typedef void(^completion)(NSArray *list);

2) 更改.h 和.m 中的方法签名

- (void)startSession:(completion) completion

3) 在 Magento.m 中,用这个修改你的方法

- (void)startSession:(completion) completion
{

NSString *_sessionID;
NSNumber *nsPage =[NSNumber numberWithInt:1];
NSNumber *nsData =[NSNumber numberWithInt:10];
NSMutableArray *listOfName = [[NSMutableArray alloc] init];
NSArray *args = [NSArray arrayWithObjects:nsPage,nsData,nil ];
@synchronized(self) {
_sessionID = sessionID;
}
if (_sessionID != FAILED_SESSION)
dispatch_group_enter(session_group);
[client postPath:@"login" parameters:@{@"username": MAGENTO_USERNAME, @"apiKey": MAGENTO_API_KEY} success:^(AFHTTPRequestOperation *operation, id responseObject) {
sessionID = responseObject;



dispatch_group_leave(session_group);

[client postPath:@"call" parameters:@{@"sessionId":sessionID, @"resourcePath":@"callforprice_collection.getCallForPriceCollection",
@"args":args
} success:^(AFHTTPRequestOperation *operationData, id responseData) {

NSLog(@"Response : %@",responseData);
[listOfName addObject:[responseData valueForKey:@"name"]];

completion(listOfName);

} failure:^(AFHTTPRequestOperation *operationData, NSError *error) {
NSLog(@"Response is not get");
sessionID = FAILED_SESSION;
completion(nil);
}];


NSLog(@"got session %@", sessionID);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
sessionID = FAILED_SESSION;
completion(nil);
}];

}

4)在你的 viewController.h 中,声明一个全局变量来保存返回的名称,

@property(nonatomic, strong) NSArray *nameList;

// and in viewDidLoad method,
self.nameList = [[NSArray alloc] init];

5) 把这个代码片段,放在 viewDidLoad 或

[Magento.service startSession:^(NSArray *list) {
if(list){
self.nameList = list;
[self.tableView reloadData];
}else self.nameList = [[NSArray alloc] init];
}];

6) 使用self.nameList数组加载表格

关于iphone - 调用 API 后不等待响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16359473/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com