2

问题是我调用 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 正在显示实际发生的记录是在调用 do response 之前打印 NSLOG 。如何解决这个问题.s

4

3 回答 3

2

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数组加载表

于 2013-05-03T13:09:02.627 回答
1

首先分配 NSMutableArray 并确保 [Magento.service renewSession] 实际上返回一个数组。

NSMutableArray *list = [NSMutableArray array];
list=[Magento.service renewSession];
//[Magento.service getCallForPriceData];
NSLog(@"Record is:%@",list);
于 2013-05-03T13:10:37.610 回答
0

我不知道 Magneto API,但我之前遇到过这种行为。这是一种异步方法。在 startSession 中,它将返回空列表,并进行异步调用(在不同的线程上)。在响应中,您对结果不做任何事情。(块存储本地引用,这就是为什么你可以添加一些东西)。尝试定义一个

startSessionWithCompletionBlock:(void(^)(NSArray* list))completionBlock; 

函数并将响应处理程序更改为:

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

并打电话

[Magento.service startSessionWithCompletionBlock:^(NSArray* list){
   NSLog(@"Record is:%@",list);
}];

这只会将第一个对象放在列表中并返回(只是为了显示它实际上返回了一些东西)。您还需要定义一些逻辑来计算异步下载的项目。

于 2013-05-03T14:11:53.497 回答