Menu

SDK’s Method Calls Anatomy

Developers will see bunch of method calls for a single API end-point that interacting with Playbasis platform. This is due to a design that the SDK provides to give developers flexibility in integrating the SDK with their own application.

When to Use What?

In iOS SDK, developers will see the following method calls declared in Playbasis.h file.

-(PBRequestUnit *)loginPlayer:(NSString *)playerId withDelegate:(id<PBResultStatus_ResponseHandler>)delegate;
                    

Use this when developers prefer delegate, and want to block a call until it finishes before continues to do other tasks.

-(PBRequestUnit *)loginPlayer:(NSString *)playerId withBlock:(PBResultStatus_ResponseBlock)block;
                      

Use this when developers prefer block, and want to block a call until it finishes before continues to do other tasks.

-(PBRequestUnit *)loginPlayerAsync:(NSString *)playerId withDelegate:(id<PBResultStatus_ResponseHandler>)delegate;
                      

Use this when developers prefer delegate, and want to immediately continue doing other tasks. Calling this method will return immediately, thus gives a flexibility for developers to quickly continue their code flow.

-(PBRequestUnit *)loginPlayerAsync:(NSString *)playerId withBlock:(PBResultStatus_ResponseBlock)block;
                      

Use this when developers prefer block, and want to immediately continue doing other tasks. Calling this method will return immediately, thus gives a flexibility for developers to quickly continue their code flow.

-(PBRequestUnit *)loginPlayerAsync_:(NSString *)playerId withBlock:(PBAsyncURLRequestResponseBlock)block;
                      

Use this when developers want to make a request to Playbasis platform with asynchronous request. It’s the same as loginPlayerAsync but it does it in asynchronous request. There will be no data responded. SDK decided to only use block as calling this we don’t want to hook up or add more code ie. delegate. Note “_” is appended at the end of method call.

Combine With dispatch_async()

Developers can combine the power of dispatch_async() along with our exposed set of method calls to achieve a certain desire outcome.

Consider the following examples.

1. Using synchronous method call
[[Playbasis sharedPB] loginPlayer:USER withBlock:^(PBResultStatus_Response *result, NSURL *url, NSError *error) {
    if(!error)
    {
        NSLog(@"Logged in successfully.");
    }
    else
    {
        NSLog(@"Failed to log in.");
    }
}];
NSLog(@"Passed to this line");
                
Result:

Successful case
Logged in successfully.
Passed to this line.

Failed case
Failed to log in.
Passed to this line.

2. Using asynchronous method call
[[Playbasis sharedPB] loginPlayerAsync:USER withBlock:^(PBResultStatus_Response *result, NSURL *url, NSError *error) {
    if(!error)
    {
        NSLog(@"Logged in successfully.");
    }
    else
    {
        NSLog(@"Failed to log in.");
    }
}];
NSLog(@"Passed to this line");
                                
Result:

Successful case
Passed to this line.
Logged in successfully.

Failed case
Passed to this line.
Failed to log in.

3. Using synchronous method calls with dispatch_async()

This case we will spawn a new thread to login player, and do an action for player (rule) in sequentially waiting for one another to finish first then do next.

...
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[[Playbasis sharedPB] loginPlayer:USER withBlock:^(PBResultStatus_Response *result, NSURL *url, NSError *error) {
    if(!error)
    {
        NSLog(@"Logged in successfully.");
    }
}];
[[Playbasis sharedPB] ruleForPlayer:USER action:@"like" withBlock:^(PBRule_Response *response, NSURL *url, NSError *error) {
    if(!error)
    {
        NSLog(@"Like for player successfully");
    }
}, nil];
});
NSLog(@"Passed to this line");
...
                                                
Result (assume success):

Passed to this line.
Logged in successfully.
Like for player successfully.

4. Using asynchronous method calls with dispatch_async()

There’re a couple of possibilities there when developers call asynchronous methods. After calling asynchronous method, the method will return immediately, but the response of that method might come in for next few seconds, or more.

...
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[[Playbasis sharedPB] loginPlayerAsync:USER withBlock:^(PBResultStatus_Response *result, NSURL *url, NSError *error) {
    if(!error)
    {
        NSLog(@"Logged in successfully.");
    }
}];
[[Playbasis sharedPB] ruleForPlayerAsync:USER action:@"like" withBlock:^(PBRule_Response *response, NSURL *url, NSError *error) {
    if(!error)
    {
        NSLog(@"Like for player successfully");
    }
}, nil];
});
NSLog(@"Passed to this line");
...
                          
Result (assume success):

Possibility 1
Passed to this line.
Logged in successfully.
Like for player successfully.

Possibility 2
Passed to this line.
Like for player successfully.
Logged in successfully.

5. Using asynchronous url request
...
[[Playbasis sharedPB] ruleForPlayerAsync_:USER action:@"like" withBlock:^(PBManualSetResultStatus_Response *status, NSURL *url, NSError *error) {
    if(!error)
    {
        NSLog(@"Do like for player successfully.");
    }
    else
    {
        NSLog(@"Failed to like for player.");
    }
}];
NSLog(@”Passed to this line”);
…
                                          
Result:

Successful case
Passed to this line.
Do like for player successfully.

Failed case
Passed to this line.
Failed to like for player.