如何仅在方法完成后才执行一行代码?

How do I execute a line of code ONLY after a method has finished?

我有一个要在 viewDidLoad 中调用的方法 getSpotifyProductTypeWithSession:(SPTSession *)session,我希望它下面的代码仅在 getSpotifyProductTypeWithSession:(SPTSession *)session 完成后执行。我该怎么做呢?下面是我的代码。

-(void)viewDidLoad{

SPTAuth *auth = [SPTAuth defaultInstance];

    [self getSpotifyProductTypeWithSession:auth.session];


        BOOL hasPremium = [[NSUserDefaults standardUserDefaults] boolForKey:@"hasPremium"];

        if (hasPremium) {
            [FireStarter setSpotifyHasPremium];
        } else {
            [FireStarter setSpotifyHasFree];
        }

}

-(void)getSpotifyProductTypeWithSession:(SPTSession *)session{

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

    [SPTUser requestCurrentUserWithAccessToken:session.accessToken callback:^(NSError *error, SPTUser *user) {

        [defaults setBool:NO forKey:@"hasPremium"];
        [defaults synchronize];

        switch (user.product) {
            case SPTProductFree:
                [defaults setBool:NO forKey:@"hasPremium"];
                [defaults synchronize];
                break;
            case SPTProductPremium:
                [defaults setBool:YES forKey:@"hasPremium"];
                [defaults synchronize];
                break;
            case SPTProductUnlimited:
                [defaults setBool:YES forKey:@"hasPremium"];
                [defaults synchronize];
            default:

                break;
        }
    }];

    NSLog(@"%@",[defaults objectForKey:@"hasPremium"]);
}

为了很好地处理执行异步工作和获取完成块的方法,您应该在代码中采用相同的方法。所以你可以这样声明:

-(void)getSpotifyProductTypeWithSession:(SPTSession *)session completion:(void (^)(NSError *))completion {
    // ... OP code
    [SPTUser requestCurrentUserWithAccessToken:session.accessToken callback:^(NSError *error, SPTUser *user) {
        // if there's an error
        if (error) completion(error);
        // ....more OP code
        // at the end
        completion(nil);   // we're done with no error
    }];
}

现在您对此的调用遵循相同的模式...

-(void)viewDidLoad{
    SPTAuth *auth = [SPTAuth defaultInstance];
    [self getSpotifyProductTypeWithSession:auth.session completion:^(NSError *)error {
        BOOL hasPremium = [[NSUserDefaults standardUserDefaults] boolForKey:@"hasPremium"];
       // and so on...
    }];

您想向 getSpotifyProductTypeWithSession 方法添加完成处理程序参数。然后您可以将代码移到完成处理程序中。让完成处理程序告诉您结果也会有所帮助,这样您就不需要再次检查 NSUserDefaults

这是添加的参数:

-(void)getSpotifyProductTypeWithSession:(SPTSession *)session completion:(void (^)(BOOL hasPremium))completion {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

    [SPTUser requestCurrentUserWithAccessToken:session.accessToken callback:^(NSError *error, SPTUser *user) {
        BOOL hasPremium = NO;

        switch (user.product) {
            case SPTProductFree:
                hasPremium = NO;
                break;
            case SPTProductPremium:
            case SPTProductUnlimited:
                hasPremium = YES;
                break;
        }

        [defaults setBool:hasPremium forKey:@"hasPremium"];

        if (completion) {
            completion(hasPremium);
        }
    }];
}

你是这样称呼它的:

[self getSpotifyProductTypeWithSession:auth.session completion:^(BOOL hasPremium) {
    if (hasPremium) {
        [FireStarter setSpotifyHasPremium];
    } else {
        [FireStarter setSpotifyHasFree];
    }
}];

可能有一些错别字。此代码未经过编译和测试,但它给出了思路。