From 9429adb0347a52dd97d346ba4b8389776cd78985 Mon Sep 17 00:00:00 2001 From: aaa Date: Fri, 10 Oct 2014 15:08:58 +0100 Subject: [PATCH] All blocking calls in ios plugin now runs in background thread. --- .../SocketsForCordova/Classes/SocketPlugin.m | 80 ++++++++++--------- 1 file changed, 43 insertions(+), 37 deletions(-) diff --git a/src/ios/SocketsForCordova/Classes/SocketPlugin.m b/src/ios/SocketsForCordova/Classes/SocketPlugin.m index 495cc99..1182586 100644 --- a/src/ios/SocketsForCordova/Classes/SocketPlugin.m +++ b/src/ios/SocketsForCordova/Classes/SocketPlugin.m @@ -66,37 +66,41 @@ SocketAdapter *socket = [self getSocketAdapter:socketKey]; - @try { - [socket connect:host port:port]; - [self.commandDelegate - sendPluginResult:[CDVPluginResult resultWithStatus:CDVCommandStatus_OK] - callbackId:command.callbackId]; - } - @catch (NSException *e) { - [self.commandDelegate - sendPluginResult:[CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:e.reason] - callbackId:command.callbackId]; - } + [self.commandDelegate runInBackground:^{ + @try { + [socket connect:host port:port]; + [self.commandDelegate + sendPluginResult:[CDVPluginResult resultWithStatus:CDVCommandStatus_OK] + callbackId:command.callbackId]; + } + @catch (NSException *e) { + [self.commandDelegate + sendPluginResult:[CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:e.reason] + callbackId:command.callbackId]; + } + }]; } - (void) write:(CDVInvokedUrlCommand *) command { - NSString* socketKey = [command.arguments objectAtIndex:0]; - NSArray *data = [command.arguments objectAtIndex:1]; + NSString* socketKey = [command.arguments objectAtIndex:0]; + NSArray *data = [command.arguments objectAtIndex:1]; - SocketAdapter *socket = [self getSocketAdapter:socketKey]; - - @try { - [socket write:data]; - [self.commandDelegate - sendPluginResult:[CDVPluginResult resultWithStatus:CDVCommandStatus_OK] - callbackId:command.callbackId]; - } - @catch (NSException *e) { - [self.commandDelegate - sendPluginResult:[CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:e.reason] - callbackId:command.callbackId]; - } + SocketAdapter *socket = [self getSocketAdapter:socketKey]; + + [self.commandDelegate runInBackground:^{ + @try { + [socket write:data]; + [self.commandDelegate + sendPluginResult:[CDVPluginResult resultWithStatus:CDVCommandStatus_OK] + callbackId:command.callbackId]; + } + @catch (NSException *e) { + [self.commandDelegate + sendPluginResult:[CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:e.reason] + callbackId:command.callbackId]; + } + }]; } - (void) close:(CDVInvokedUrlCommand *) command { @@ -105,17 +109,19 @@ SocketAdapter *socket = [self getSocketAdapter:socketKey]; - @try { - [socket close]; - [self.commandDelegate - sendPluginResult:[CDVPluginResult resultWithStatus:CDVCommandStatus_OK] - callbackId:command.callbackId]; - } - @catch (NSException *e) { - [self.commandDelegate - sendPluginResult:[CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:e.reason] - callbackId:command.callbackId]; - } + [self.commandDelegate runInBackground:^{ + @try { + [socket close]; + [self.commandDelegate + sendPluginResult:[CDVPluginResult resultWithStatus:CDVCommandStatus_OK] + callbackId:command.callbackId]; + } + @catch (NSException *e) { + [self.commandDelegate + sendPluginResult:[CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:e.reason] + callbackId:command.callbackId]; + } + }]; } - (void) setOptions: (CDVInvokedUrlCommand *) command {