You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@chemistry.apache.org by lg...@apache.org on 2015/02/13 18:30:37 UTC

svn commit: r1659620 - in /chemistry/objectivecmis/trunk/ObjectiveCMIS: Bindings/Browser/CMISBrowserRepositoryService.m Utils/CMISHttpDownloadRequest.m Utils/CMISHttpRequest.h Utils/CMISHttpRequest.m Utils/CMISHttpUploadRequest.m

Author: lgross
Date: Fri Feb 13 17:30:37 2015
New Revision: 1659620

URL: http://svn.apache.org/r1659620
Log:
Execute callback blocks on original thread
This is required when ObjectiveCMIS Library is called from a background thread

Modified:
    chemistry/objectivecmis/trunk/ObjectiveCMIS/Bindings/Browser/CMISBrowserRepositoryService.m
    chemistry/objectivecmis/trunk/ObjectiveCMIS/Utils/CMISHttpDownloadRequest.m
    chemistry/objectivecmis/trunk/ObjectiveCMIS/Utils/CMISHttpRequest.h
    chemistry/objectivecmis/trunk/ObjectiveCMIS/Utils/CMISHttpRequest.m
    chemistry/objectivecmis/trunk/ObjectiveCMIS/Utils/CMISHttpUploadRequest.m

Modified: chemistry/objectivecmis/trunk/ObjectiveCMIS/Bindings/Browser/CMISBrowserRepositoryService.m
URL: http://svn.apache.org/viewvc/chemistry/objectivecmis/trunk/ObjectiveCMIS/Bindings/Browser/CMISBrowserRepositoryService.m?rev=1659620&r1=1659619&r2=1659620&view=diff
==============================================================================
--- chemistry/objectivecmis/trunk/ObjectiveCMIS/Bindings/Browser/CMISBrowserRepositoryService.m (original)
+++ chemistry/objectivecmis/trunk/ObjectiveCMIS/Bindings/Browser/CMISBrowserRepositoryService.m Fri Feb 13 17:30:37 2015
@@ -80,7 +80,7 @@
                                                completionBlock(nil);
                                            }
                                        } else {
-                                           completionBlock(error);
+                                            completionBlock(error);
                                        }
                                    }];
     

Modified: chemistry/objectivecmis/trunk/ObjectiveCMIS/Utils/CMISHttpDownloadRequest.m
URL: http://svn.apache.org/viewvc/chemistry/objectivecmis/trunk/ObjectiveCMIS/Utils/CMISHttpDownloadRequest.m?rev=1659620&r1=1659619&r2=1659620&view=diff
==============================================================================
--- chemistry/objectivecmis/trunk/ObjectiveCMIS/Utils/CMISHttpDownloadRequest.m (original)
+++ chemistry/objectivecmis/trunk/ObjectiveCMIS/Utils/CMISHttpDownloadRequest.m Fri Feb 13 17:30:37 2015
@@ -192,13 +192,9 @@
     // update statistics
     self.bytesDownloaded += data.length;
     
-    // pass progress to progressBlock, on the main thread
-    if (self.progressBlock) {
-        dispatch_async(dispatch_get_main_queue(), ^{
-            if (self.progressBlock) {
-                self.progressBlock(self.bytesDownloaded, self.bytesExpected);
-            }
-        });
+    // pass progress to progressBlock, on the original thread
+    if (self.originalThread) {
+        [self performSelector:@selector(executeProgressBlock:) onThread:self.originalThread withObject:@[@(self.bytesDownloaded), @(self.bytesExpected)] waitUntilDone:NO];
     }
 }
 
@@ -230,10 +226,10 @@
                 NSError *cmisError = [CMISErrors createCMISErrorWithCode:kCMISErrorCodeStorage
                                                      detailedDescription:@"Could not open output stream"];
                 
-                // call the completion block on the main thread
-                dispatch_async(dispatch_get_main_queue(), ^{
-                    self.completionBlock(nil, cmisError);
-                });
+                // call the completion block on the original thread
+                if (self.originalThread) {
+                    [self performSelector:@selector(executeCompletionBlockError:) onThread:self.originalThread withObject:cmisError waitUntilDone:NO];
+                }
             }
         }
     }
@@ -271,12 +267,16 @@
         if (totalBytesExpected == NSURLSessionTransferSizeUnknown && self.bytesExpected != 0) {
             totalBytesExpected = self.bytesExpected;
         }
-        
-        dispatch_async(dispatch_get_main_queue(), ^{
-            if (self.progressBlock) {
-                self.progressBlock(totalBytesWritten, totalBytesExpected);
-            }
-        });
+        // pass progress to progressBlock, on the original thread
+        if (self.originalThread) {
+            [self performSelector:@selector(executeProgressBlock:) onThread:self.originalThread withObject:@[@(totalBytesWritten), @(totalBytesExpected)] waitUntilDone:NO];
+        }
+    }
+}
+
+- (void)executeProgressBlock:(NSArray*)valueArray {
+    if (self.progressBlock) {
+        self.progressBlock([valueArray[0] unsignedLongLongValue], [valueArray[1] unsignedLongLongValue]);
     }
 }
 

Modified: chemistry/objectivecmis/trunk/ObjectiveCMIS/Utils/CMISHttpRequest.h
URL: http://svn.apache.org/viewvc/chemistry/objectivecmis/trunk/ObjectiveCMIS/Utils/CMISHttpRequest.h?rev=1659620&r1=1659619&r2=1659620&view=diff
==============================================================================
--- chemistry/objectivecmis/trunk/ObjectiveCMIS/Utils/CMISHttpRequest.h (original)
+++ chemistry/objectivecmis/trunk/ObjectiveCMIS/Utils/CMISHttpRequest.h Fri Feb 13 17:30:37 2015
@@ -34,6 +34,7 @@
 @property (nonatomic, strong) NSHTTPURLResponse *response;
 @property (nonatomic, strong) CMISBindingSession *session;
 @property (nonatomic, copy) void (^completionBlock)(CMISHttpResponse *httpResponse, NSError *error);
+@property (nonatomic, weak) NSThread *originalThread;
 
 /**
  * starts a URL request for given HTTP method 
@@ -61,4 +62,10 @@
 /// Creates an appropriate task for the given request object.
 - (NSURLSessionTask *)taskForRequest:(NSURLRequest *)request;
 
+/// Call completion block with response returned from server
+- (void)executeCompletionBlockResponse:(CMISHttpResponse*)response;
+
+/// Call completion block with error returned from server
+- (void)executeCompletionBlockError:(NSError*)error;
+
 @end

Modified: chemistry/objectivecmis/trunk/ObjectiveCMIS/Utils/CMISHttpRequest.m
URL: http://svn.apache.org/viewvc/chemistry/objectivecmis/trunk/ObjectiveCMIS/Utils/CMISHttpRequest.m?rev=1659620&r1=1659619&r2=1659620&view=diff
==============================================================================
--- chemistry/objectivecmis/trunk/ObjectiveCMIS/Utils/CMISHttpRequest.m (original)
+++ chemistry/objectivecmis/trunk/ObjectiveCMIS/Utils/CMISHttpRequest.m Fri Feb 13 17:30:37 2015
@@ -39,7 +39,6 @@ NSString * const kCMISExceptionStreamNot
 NSString * const kCMISExceptionUpdateConflict          = @"updateConflict";
 NSString * const kCMISExceptionVersioning              = @"versioning";
 
-
 @implementation CMISHttpRequest
 
 
@@ -69,6 +68,7 @@ NSString * const kCMISExceptionVersionin
 {
     self = [super init];
     if (self) {
+        _originalThread = [NSThread currentThread];
         _requestMethod = httpRequestMethod;
         _completionBlock = completionBlock;
     }
@@ -147,10 +147,7 @@ NSString * const kCMISExceptionVersionin
         if (self.completionBlock) {
             NSString *detailedDescription = [NSString stringWithFormat:@"Could not create network session for %@", urlRequest.URL];
             NSError *cmisError = [CMISErrors createCMISErrorWithCode:kCMISErrorCodeConnection detailedDescription:detailedDescription];
-            void (^completionBlock)(CMISHttpResponse *httpResponse, NSError *error);
-            completionBlock = self.completionBlock;
-            self.completionBlock = nil; // Prevent multiple execution if method on this request gets called inside completion block
-            completionBlock(nil, cmisError);
+            [self executeCompletionBlockResponse:nil error:cmisError];
         }
     }
     
@@ -212,14 +209,14 @@ NSString * const kCMISExceptionVersionin
                 httpResponse = nil;
             }
         }
-        
-        // call the completion block on the main thread
-        dispatch_async(dispatch_get_main_queue(), ^{
-            void (^completionBlock)(CMISHttpResponse *httpResponse, NSError *error);
-            completionBlock = self.completionBlock;
-            self.completionBlock = nil; // Prevent multiple execution if method on this request gets called inside completion block
-            completionBlock(httpResponse, cmisError);
-        });
+        // call the completion block on the original thread
+        if (self.originalThread) {
+            if(cmisError) {
+                [self performSelector:@selector(executeCompletionBlockError:) onThread:self.originalThread withObject:cmisError waitUntilDone:NO];
+            } else {
+                [self performSelector:@selector(executeCompletionBlockResponse:) onThread:self.originalThread withObject:httpResponse waitUntilDone:NO];
+            }
+        }
     }
     
     // clean up
@@ -333,4 +330,21 @@ NSString * const kCMISExceptionVersionin
     return YES;
 }
 
+- (void)executeCompletionBlockResponse:(CMISHttpResponse*)response {
+    [self executeCompletionBlockResponse:response error:nil];
+}
+
+- (void)executeCompletionBlockError:(NSError*)error {
+    [self executeCompletionBlockResponse:nil error:error];
+}
+
+- (void)executeCompletionBlockResponse:(CMISHttpResponse*)response error:(NSError*)error {
+    if (self.completionBlock) {
+        void (^completionBlock)(CMISHttpResponse *httpResponse, NSError *error);
+        completionBlock = self.completionBlock;
+        self.completionBlock = nil; // Prevent multiple execution if method on this request gets called inside completion block
+        completionBlock(response, error);
+    }
+}
+
 @end

Modified: chemistry/objectivecmis/trunk/ObjectiveCMIS/Utils/CMISHttpUploadRequest.m
URL: http://svn.apache.org/viewvc/chemistry/objectivecmis/trunk/ObjectiveCMIS/Utils/CMISHttpUploadRequest.m?rev=1659620&r1=1659619&r2=1659620&view=diff
==============================================================================
--- chemistry/objectivecmis/trunk/ObjectiveCMIS/Utils/CMISHttpUploadRequest.m (original)
+++ chemistry/objectivecmis/trunk/ObjectiveCMIS/Utils/CMISHttpUploadRequest.m Fri Feb 13 17:30:37 2015
@@ -266,12 +266,18 @@ const NSUInteger kRawBufferSize = 24576;
             if (totalBytesSent >= totalBytesExpectedToSend) {
                 self.transferCompleted = YES;
             }
-            self.progressBlock((unsigned long long)totalBytesSent, (unsigned long long)totalBytesExpectedToSend);
+            // pass progress to progressBlock, on the original thread
+            if (self.originalThread) {
+                [self performSelector:@selector(executeProgressBlock:) onThread:self.originalThread withObject:@[@(totalBytesSent), @(totalBytesExpectedToSend)] waitUntilDone:NO];
+            }
         } else {
             if (totalBytesSent >= self.bytesExpected) {
                 self.transferCompleted = YES;
             }
-            self.progressBlock((unsigned long long)totalBytesSent, self.bytesExpected);
+            // pass progress to progressBlock, on the original thread
+            if (self.originalThread) {
+                [self performSelector:@selector(executeProgressBlock:) onThread:self.originalThread withObject:@[@(totalBytesSent), @(self.bytesExpected)] waitUntilDone:NO];
+            }
         }
     }
 }
@@ -504,5 +510,10 @@ const NSUInteger kRawBufferSize = 24576;
     self.streamStartData = nil;
 }
 
+- (void)executeProgressBlock:(NSArray*)valueArray {
+    if (self.progressBlock) {
+        self.progressBlock([valueArray[0] unsignedLongLongValue], [valueArray[1] unsignedLongLongValue]);
+    }
+}
 
 @end