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?


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");
链接地址: http://www.djcxy.com/p/74884.html

上一篇: 自动布局可视化编程语言,用于一系列视图

下一篇: 在iOS上执行对Google云端点的同步查询