I am working with a package called DSBridge to connect a mobile application (iOS code in this case) to javascript code containing the main logic of my application. This involves running the JavaScript on an invisible WKWebView.
My JavaScript code needs to call a method in the native iOS application which needs to execute asynchronously (as to not block the UI), to do this I am using callbacks in JavaScript and a completionHandler in native.
My Objective C function reads:
- (void)read:(NSDictionary *) args :(JSCallback) completionHandler{
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void){
NSLog(@"In Read");
while(self->connection != NULL) {
if([self->connection isMessageAvailable]) {
[self->connection messageRetrieved];
NSLog(@"Message Received in ViewController");
completionHandler([self->connection getMessage], YES);
}
}
});
}
Which is called from my JavaScript here:
function read() {
data = "reading";
dsBridge.call("read", data, function (read_result) {
console.log("read_result = ", read_result)
return read_result;
});
}
The issue lies in the JavaScript, I need the JavaScript function 'read' to return the value "read_result" once the Native application returns the message. Right now my code is calling the objective C, which is executing properly and returning the required value to the callback function in the JavaScript but the JavaScript function 'read' already completed without a return value.
Is there a way for me to stall the JavaScript until the callback is complete?