Rashmi Ranjan mallick
Rashmi Ranjan mallick

Reputation: 6620

iOS cordova plugin: How to send plugin result from ios plugin to javascript in background thread

I have a plugin class which is extending CDVPlugin. This plugin will invoked from a button click from HTML side. After that I am using UIImagePickerController to take two photos. Then I am trying to send those images to javascript callback function using pluginresult.

Here, as I am trying to send two images in one go, the UI is being stuck for some time. So, I want to send the result in a background thread. And also I should be receive that from javascript callback function.

Has someone done this before? Is there any way to achieve this so that UI navigation will be smoother....

Upvotes: 5

Views: 3703

Answers (1)

jcesarmobile
jcesarmobile

Reputation: 53301

Something like this:

    - (void)myPluginMethod:(CDVInvokedUrlCommand*)command
{
    // Check command.arguments here.
    [self.commandDelegate runInBackground:^{
        NSString* payload = nil;
        // Some blocking logic...
        CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:payload];
        // The sendPluginResult method is thread-safe.
        [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
    }];
}

Upvotes: 6

Related Questions