我在同一个函数中有两个方法。
我只想在有第一种方法的响应的情况下才开始第二种方法。
我该怎么做?

     - (void)share:(UIImage*)immagine
    [FBRequestConnection startWithGraphPath:@"/me/albums"
                                 parameters:nil
                                 HTTPMethod:@"GET"
                          completionHandler:^(
                                              FBRequestConnection *connection,
                                              id result,
                                              NSError *error
                                              ) {

                             if (!error) {
                               //Here I'm getting a var but it need time
                                     }

                          }];


     NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
        [params setObject:UIImagePNGRepresentation(immagine) forKey:@"picture"];

        //that's the var I need

    [FBRequestConnection startWithGraphPath:[NSString stringWithFormat:@"/%@/photos", result]
            parameters:params
            HTTPMethod:@"POST"
            completionHandler:^(FBRequestConnection *connection,result,NSError *error)
                                                   {

                                                       if (error==nil)
                                                       {

                                                    }

                                  }];

最佳答案

您的代码异步执行:它不是从上到下执行。

注意执行顺序:

1)方法称为:

  - (void)share:(UIImage*)immagine


2)startWithGraphPath is called

[FBRequestConnection startWithGraphPath:@"/me/albums"
                             parameters:nil
                             HTTPMethod:@"GET"
                      completionHandler:^(
                                          FBRequestConnection *connection,
                                          id result,
                                          NSError *error
                                          ) {

                         if (!error) {
                           //Here I'm getting a var but it need time
                                 }

                      }];


3)该代码的其余部分立即执行:

 NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
    [params setObject:UIImagePNGRepresentation(immagine) forKey:@"picture"];

    //that's the var I need

[FBRequestConnection startWithGraphPath:[NSString stringWithFormat:@"/%@/photos", result]
        parameters:params
        HTTPMethod:@"POST"
        completionHandler:^(FBRequestConnection *connection,result,NSError *error)
                                               {

                                                   if (error==nil)
                                                   {

                                                }

                              }];


4)一旦网络请求返回,就调用此位(即使您必须向上滚动才能看到它):

completionHandler:^(
                                      FBRequestConnection *connection,
                                      id result,
                                      NSError *error
                                      ) {

                     if (!error) {
                       //Here I'm getting a var but it need time
                             }

                  }];


将步骤3)中的代码基本上放入completionHandler:直到该代码被击中,数据才可用。

10-06 10:50
查看更多