Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Executing synchronous queries to Google Cloud Endpoints on iOS

I guess this is really a feature request to Google, but I'm curious if anyone knows a work around. I'd like to execute a synchronous query request to a GAE Endpoints api.

In Android executing a request is synchronous. Then you put it into an AsyncTask to make it work in the background.

In iOS executing a request is asynchronous. You simply pass in a callback block.

I'm converting an Android app into an iOS app and it'd be really nice if they used the same mechanism. For example there are times when I WANT a synchronous query. It just makes my code easier and I know to put it on a background thread.

So my question is this... is there any way (hacky or not) to block until the iOS query completes?

like image 461
Dave Fisher Avatar asked Oct 10 '13 19:10

Dave Fisher


1 Answers

You can wait on the call to finish with code with a timeout using code similar to this. Obviously you wouldn't want to do this on a UI thread but this would ensure your completion handlers run in serial.

NSLog(@"Before API Call");
GTLServiceTicket *apiCall = [apiService executeQuery:query completionHandler:^(GTLServiceTicket *ticket,
                                                   GTLHelloworldHelloGreeting *object,
                                                   NSError *error) {
    NSLog(@"Starting completion handler");
    NSArray *greetings = [NSArray arrayWithObjects: object, nil];
    greetingsRetrievedFromAPI = greetings;
    [self performSegueWithIdentifier: @"DisplayGreetings" sender: self];
    NSLog(@"Ending completion handler");
}];
[apiService waitForTicket:apiCall timeout:100000 fetchedObject:nil error:nil];
NSLog(@"After completion handler");
like image 64
PaulR Avatar answered Sep 28 '22 22:09

PaulR