I've got that architectural problem.
My app before doing operations need to check if the user is logged. Here I have 3 scenarios:
- User is logged: no problem
- User is not logged but I have username and pwd stored
- User is not logged, username and pwd are not stored and I need to show the login form
What I'd like to do is to suspend the current operation until the user is logged and later restart where I've stopped.
Here a practical example: The user press on an audio stream content, but this content requires login. I'd like to:
- Show the login form
- Process the async login call
- If the login has completed successfully start the audio content
I was thinking to use NSBlockOperation as a wrapper between the two operations and add a dependency between them but the login call is method that just accept a completion block that is passed to AFNetworking manager that creates the login connection. Thus, even if I create a dependency like that:
NSBlockOperation * operation1 = [NSBlockOperation blockOperationWithBlock:^{
[NetworkController loginUser:^{
NSLog(@"USerLogged");
}]
}];
NSBlockOperation * operation2 = [NSBlockOperation blockOperationWithBlock:^{
[weakSelf.player play];
}];
[operation2 addDependency:operation1];
[self.opQueue addOperations:@[operation1,operation2] waitUntilFinished:NO];
The operation1 will be completed as soon as loginUser is completed, but I'd like that this would be done only when the loginUser method is finished with login process result.
Which is the best pattern to tackle this problem?
I also thought about a dispatch_group, but this would require the injection of dispatch_enter/leave in particular parts of code.