Add a method to CDVCommandDelegate for executing on a background thread.

Project: http://git-wip-us.apache.org/repos/asf/incubator-cordova-ios/repo
Commit: 
http://git-wip-us.apache.org/repos/asf/incubator-cordova-ios/commit/e68e9d4e
Tree: http://git-wip-us.apache.org/repos/asf/incubator-cordova-ios/tree/e68e9d4e
Diff: http://git-wip-us.apache.org/repos/asf/incubator-cordova-ios/diff/e68e9d4e

Branch: refs/heads/master
Commit: e68e9d4e16dbdd95485727c5261846444c5a062d
Parents: 20cdaf9
Author: Andrew Grieve <agri...@chromium.org>
Authored: Fri Oct 5 22:44:55 2012 -0400
Committer: Andrew Grieve <agri...@chromium.org>
Committed: Tue Oct 9 15:49:34 2012 -0400

----------------------------------------------------------------------
 CordovaLib/CDVCommandDelegateImpl.m     |    4 ++++
 CordovaLib/Classes/CDVCommandDelegate.h |    2 ++
 CordovaLib/Classes/CDVContacts.m        |   24 ++++--------------------
 CordovaLib/Classes/CDVFileTransfer.m    |    4 ++--
 CordovaLib/Classes/CDVLocalStorage.m    |    4 ++--
 5 files changed, 14 insertions(+), 24 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-cordova-ios/blob/e68e9d4e/CordovaLib/CDVCommandDelegateImpl.m
----------------------------------------------------------------------
diff --git a/CordovaLib/CDVCommandDelegateImpl.m 
b/CordovaLib/CDVCommandDelegateImpl.m
index 342ea79..f35705a 100644
--- a/CordovaLib/CDVCommandDelegateImpl.m
+++ b/CordovaLib/CDVCommandDelegateImpl.m
@@ -110,4 +110,8 @@
     [_viewController registerPlugin:plugin withClassName:className];
 }
 
+- (void)runInBackground:(void (^) ())block {
+    dispatch_async (dispatch_get_global_queue 
(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), block);
+}
+
 @end

http://git-wip-us.apache.org/repos/asf/incubator-cordova-ios/blob/e68e9d4e/CordovaLib/Classes/CDVCommandDelegate.h
----------------------------------------------------------------------
diff --git a/CordovaLib/Classes/CDVCommandDelegate.h 
b/CordovaLib/Classes/CDVCommandDelegate.h
index c17a5d5..bd793b5 100644
--- a/CordovaLib/Classes/CDVCommandDelegate.h
+++ b/CordovaLib/Classes/CDVCommandDelegate.h
@@ -37,5 +37,7 @@
 - (void)sendPluginResult:(CDVPluginResult*)result 
callbackId:(NSString*)callbackId;
 // Evaluates the given JS.
 - (void)evalJs:(NSString*)js;
+// Runs the given block on a background thread.
+- (void)runInBackground:(void (^)())block;
 
 @end

http://git-wip-us.apache.org/repos/asf/incubator-cordova-ios/blob/e68e9d4e/CordovaLib/Classes/CDVContacts.m
----------------------------------------------------------------------
diff --git a/CordovaLib/Classes/CDVContacts.m b/CordovaLib/Classes/CDVContacts.m
index 8d7ce30..de79254 100644
--- a/CordovaLib/Classes/CDVContacts.m
+++ b/CordovaLib/Classes/CDVContacts.m
@@ -39,8 +39,6 @@
 
 @implementation CDVContacts
 
-dispatch_queue_t workQueue = nil;
-
 // no longer used since code gets AddressBook for each operation.
 // If address book changes during save or remove operation, may get error but 
not much we can do about it
 // If address book changes during UI creation, display or edit, we don't 
control any saves so no need for callback
@@ -52,15 +50,6 @@ dispatch_queue_t workQueue = nil;
     Contacts* contacts = (Contacts*)context;
     [contacts addressBookDirty];
 }*/
-+ (void)initialize
-{
-    workQueue = dispatch_queue_create("contacts work queue", 
DISPATCH_QUEUE_SERIAL);
-}
-
-+ (dispatch_queue_t)getWorkQueue
-{
-    return workQueue;
-}
 
 - (CDVPlugin*)initWithWebView:(UIWebView*)theWebView
 {
@@ -80,11 +69,6 @@ dispatch_queue_t workQueue = nil;
     // NSLog(@"Contacts::onAppTerminate");
 }
 
-- (void)dealloc
-{
-    dispatch_release(workQueue);
-}
-
 // iPhone only method to create a new contact through the GUI
 - (void)newContact:(CDVInvokedUrlCommand*)command
 {
@@ -296,7 +280,7 @@ dispatch_queue_t workQueue = nil;
     NSArray* fields = [command.arguments objectAtIndex:0];
     NSDictionary* findOptions = [command.arguments objectAtIndex:1 
withDefault:[NSNull null]];
 
-    dispatch_async([CDVContacts getWorkQueue], ^{
+    [self.commandDelegate runInBackground:^{
             // from Apple:  Important You must ensure that an instance of 
ABAddressBookRef is used by only one thread.
             // which is why address book is created within the dispatch queue.
             // more details here: http: 
//blog.byadrian.net/2012/05/05/ios-addressbook-framework-and-gcd/
@@ -385,7 +369,7 @@ dispatch_queue_t workQueue = nil;
                         CFRelease (addrBook);
                     }
                 }];
-        }); // end of workQueue block
+        }]; // end of workQueue block
 
     return;
 }
@@ -395,7 +379,7 @@ dispatch_queue_t workQueue = nil;
     NSString* callbackId = command.callbackId;
     NSDictionary* contactDict = [command.arguments objectAtIndex:0];
 
-    dispatch_async([CDVContacts getWorkQueue], ^{
+    [self.commandDelegate runInBackground:^{
             CDVAddressBookHelper* abHelper = [[CDVAddressBookHelper alloc] 
init];
             CDVContacts* __unsafe_unretained weakSelf = self; // play it safe 
to avoid retain cycles
 
@@ -459,7 +443,7 @@ dispatch_queue_t workQueue = nil;
                         [weakSelf.commandDelegate sendPluginResult:result 
callbackId:callbackId];
                     }
                 }];
-        }); // end of  queue
+        }]; // end of  queue
 }
 
 - (void)remove:(CDVInvokedUrlCommand*)command

http://git-wip-us.apache.org/repos/asf/incubator-cordova-ios/blob/e68e9d4e/CordovaLib/Classes/CDVFileTransfer.m
----------------------------------------------------------------------
diff --git a/CordovaLib/Classes/CDVFileTransfer.m 
b/CordovaLib/Classes/CDVFileTransfer.m
index c2139fb..5fd3fb5 100644
--- a/CordovaLib/Classes/CDVFileTransfer.m
+++ b/CordovaLib/Classes/CDVFileTransfer.m
@@ -216,7 +216,7 @@ static CFIndex WriteDataToStream(NSData* data, 
CFWriteStreamRef stream)
         CFStreamCreateBoundPair(NULL, &readStream, &writeStream, 
kStreamBufferSize);
         [req setHTTPBodyStream:CFBridgingRelease(readStream)];
 
-        
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
+        [self.commandDelegate runInBackground:^{
                 if (CFWriteStreamOpen (writeStream)) {
                     NSData* chunks[] = {postBodyBeforeFile, fileData, 
postBodyAfterFile};
                     int numChunks = sizeof (chunks) / sizeof (chunks[0]);
@@ -232,7 +232,7 @@ static CFIndex WriteDataToStream(NSData* data, 
CFWriteStreamRef stream)
                 }
                 CFWriteStreamClose (writeStream);
                 CFRelease (writeStream);
-            });
+            }];
     } else {
         [postBodyBeforeFile appendData:fileData];
         [postBodyBeforeFile appendData:postBodyAfterFile];

http://git-wip-us.apache.org/repos/asf/incubator-cordova-ios/blob/e68e9d4e/CordovaLib/Classes/CDVLocalStorage.m
----------------------------------------------------------------------
diff --git a/CordovaLib/Classes/CDVLocalStorage.m 
b/CordovaLib/Classes/CDVLocalStorage.m
index 6486a4e..7d39052 100644
--- a/CordovaLib/Classes/CDVLocalStorage.m
+++ b/CordovaLib/Classes/CDVLocalStorage.m
@@ -368,12 +368,12 @@
                 NSLog (@"Background task to backup WebSQL/LocalStorage 
expired.");
             }];
         CDVLocalStorage __unsafe_unretained* weakSelf = self;
-        
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
+        [self.commandDelegate runInBackground:^{
                 [weakSelf backup:nil];
 
                 [[UIApplication sharedApplication] 
endBackgroundTask:backgroundTaskID];
                 backgroundTaskID = UIBackgroundTaskInvalid;
-            });
+            }];
     }
 }
 

Reply via email to