Reputation: 1053
I have a method getSpotifyProductTypeWithSession:(SPTSession *)session
that I'm calling in viewDidLoad and I want the code below it to only be executed once getSpotifyProductTypeWithSession:(SPTSession *)session
has finished. How do I do this? Here's my code below.
-(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"]);
}
Upvotes: 0
Views: 85
Reputation: 318934
You want to add a completion handler parameter to your getSpotifyProductTypeWithSession
method. Then you can move your code inside the completion handler. It would also help to have the completion handler tell you the result so you don't need to check NSUserDefaults
again.
Here's the added parameter:
-(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);
}
}];
}
And here is how you call it:
[self getSpotifyProductTypeWithSession:auth.session completion:^(BOOL hasPremium) {
if (hasPremium) {
[FireStarter setSpotifyHasPremium];
} else {
[FireStarter setSpotifyHasFree];
}
}];
There may be some typos. This code was not compiled and tested but it gives the idea.
Upvotes: 0
Reputation: 62686
In order to play nice with methods that do async work and take completion blocks, you should adopt the same approach in your code. So you might declare something like:
-(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
}];
}
Now your call to this follows the same pattern...
-(void)viewDidLoad{
SPTAuth *auth = [SPTAuth defaultInstance];
[self getSpotifyProductTypeWithSession:auth.session completion:^(NSError *)error {
BOOL hasPremium = [[NSUserDefaults standardUserDefaults] boolForKey:@"hasPremium"];
// and so on...
}];
Upvotes: 1