响应iOs后启动方法

时间:2015-02-10 13:18:37

标签: ios objective-c

我在同一个功能中使用了两种方法。 我想只有在我有第一种方法的响应时才启动第二种方法。 我怎么能这样做?

     - (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 个答案:

答案 0 :(得分:1)

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

请注意执行顺序:

调用

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:直到该代码被点击,数据才可用。