2

私には2つの方法があります。最初のタスクが終わったら実行したいです。これどうやってするの?

4

2 に答える 2

7

シンプルな完了ブロック ソリューションを探していると思いますので、これで十分です。

-(void)method1:(void (^ __nullable)(void))completion {
    NSLog(@"method1 started");
    //Do some stuff, then completion
    completion();
    NSLog(@"method1 ended");
}
-(void)method2{
    NSLog(@"method2 called");
}

このように使って、

- (void)viewDidLoad{
    [super viewDidLoad];
    [self method1:^{   //After method1 completion, method2 will be called
        [self method2];
    }];
}
于 2017-01-05T11:51:48.167 に答える
1

次のようなことができます。

    [[manager POST:url parameters:parameters success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {

            NSLog(@"This is success!!!");

       //this is first method's completion blcok!

               // this is another method from completion of first
                    [self saveImages:^(BOOL isDone) {

                        // this is second method's completion

                    }];

               } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

            NSLog(@"failure : Error is : %@",error.localizedDescription);

          // this is completion of first method but with failure

        }]resume];

これは、それを管理する方法の簡単な例です!! ここではAFNEtworking の方法を使用しているので、混同しないでください。

于 2017-01-05T11:33:28.443 に答える