usergrid-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From g...@apache.org
Subject [05/40] usergrid git commit: Updates.
Date Thu, 19 May 2016 17:18:38 GMT
Updates.

Moved more of the request response handling to background threads.

Updated asset upload/download completion blocks to be more consistent with other completion
blocks.

Updated ReadMe to reflect changes to asset upload completion blocks.

Added UsegridEntity.isEqualToEntity() function.

UsergridUser.reauthenticate() now functions properly when the entity isEqualTo() the UsergridClient.currentUser.


Project: http://git-wip-us.apache.org/repos/asf/usergrid/repo
Commit: http://git-wip-us.apache.org/repos/asf/usergrid/commit/9f53f585
Tree: http://git-wip-us.apache.org/repos/asf/usergrid/tree/9f53f585
Diff: http://git-wip-us.apache.org/repos/asf/usergrid/diff/9f53f585

Branch: refs/heads/master
Commit: 9f53f585ad29361405a5ce3091e25fb6bd355778
Parents: f2a4a10
Author: Robert Walsh <rjwalsh1985@gmail.com>
Authored: Tue Feb 16 15:58:52 2016 -0600
Committer: Robert Walsh <rjwalsh1985@gmail.com>
Committed: Tue Feb 16 15:58:52 2016 -0600

----------------------------------------------------------------------
 sdks/swift/README.md                           |  2 +-
 sdks/swift/Source/UsergridAsset.swift          |  4 +-
 sdks/swift/Source/UsergridClient.swift         |  8 +--
 sdks/swift/Source/UsergridEntity.swift         | 18 +++++++
 sdks/swift/Source/UsergridRequestManager.swift | 60 ++++++++++++++-------
 sdks/swift/Source/UsergridUser.swift           |  2 +-
 6 files changed, 66 insertions(+), 28 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/usergrid/blob/9f53f585/sdks/swift/README.md
----------------------------------------------------------------------
diff --git a/sdks/swift/README.md b/sdks/swift/README.md
index be8b6f6..14f93ea 100644
--- a/sdks/swift/README.md
+++ b/sdks/swift/README.md
@@ -742,7 +742,7 @@ Usergrid.uploadAsset(entity,
                      progress: { bytesFinished, bytesExpected in
                         // Monitor the upload progress
                      },
-                     completion: { response, asset, error in
+                     completion: { asset, response in
                         // The asset is now uploaded to Usergrid and entity.asset == asset
 })
 ```

http://git-wip-us.apache.org/repos/asf/usergrid/blob/9f53f585/sdks/swift/Source/UsergridAsset.swift
----------------------------------------------------------------------
diff --git a/sdks/swift/Source/UsergridAsset.swift b/sdks/swift/Source/UsergridAsset.swift
index 85f9ab6..73f0bd3 100644
--- a/sdks/swift/Source/UsergridAsset.swift
+++ b/sdks/swift/Source/UsergridAsset.swift
@@ -35,10 +35,10 @@ import MobileCoreServices
 public typealias UsergridAssetRequestProgress = (bytesFinished:Int64, bytesExpected: Int64)
-> Void
 
 /// The completion block used in `UsergridAsset` are finished uploading.
-public typealias UsergridAssetUploadCompletion = (response:UsergridResponse,asset:UsergridAsset?,
error: String?) -> Void
+public typealias UsergridAssetUploadCompletion = (asset:UsergridAsset?, response: UsergridResponse)
-> Void
 
 /// The completion block used in `UsergridAsset` are finished downloading.
-public typealias UsergridAssetDownloadCompletion = (asset:UsergridAsset?, error: String?)
-> Void
+public typealias UsergridAssetDownloadCompletion = (asset:UsergridAsset?, error: UsergridResponseError?)
-> Void
 
 /**
 As Usergrid supports storing binary assets, the SDKs are designed to make uploading assets
easier and more robust. Attaching, uploading, and downloading assets is handled by the `UsergridEntity`
class.

http://git-wip-us.apache.org/repos/asf/usergrid/blob/9f53f585/sdks/swift/Source/UsergridClient.swift
----------------------------------------------------------------------
diff --git a/sdks/swift/Source/UsergridClient.swift b/sdks/swift/Source/UsergridClient.swift
index a0306a3..46310c6 100644
--- a/sdks/swift/Source/UsergridClient.swift
+++ b/sdks/swift/Source/UsergridClient.swift
@@ -872,9 +872,9 @@ public class UsergridClient: NSObject, NSCoding {
                                                       auth: self.authForRequests(),
                                                       asset: asset)
 
-        _requestManager.performAssetUpload(assetRequest, progress: progress) { [weak entity]
(response, asset, error) -> Void in
-            entity?.asset = asset
-            completion?(response: response, asset: asset, error: error)
+        _requestManager.performAssetUpload(assetRequest, progress: progress) { asset, response
in
+            entity.asset = asset
+            completion?(asset: asset, response: response)
         }
     }
 
@@ -889,7 +889,7 @@ public class UsergridClient: NSObject, NSCoding {
     public func downloadAsset(entity:UsergridEntity, contentType:String, progress:UsergridAssetRequestProgress?
= nil, completion:UsergridAssetDownloadCompletion? = nil) {
         guard entity.hasAsset
         else {
-            completion?(asset: nil, error: "Entity does not have an asset attached.")
+            completion?(asset: nil, error: UsergridResponseError(errorName: "Download asset
failed.", errorDescription: "Entity does not have an asset attached."))
             return
         }
 

http://git-wip-us.apache.org/repos/asf/usergrid/blob/9f53f585/sdks/swift/Source/UsergridEntity.swift
----------------------------------------------------------------------
diff --git a/sdks/swift/Source/UsergridEntity.swift b/sdks/swift/Source/UsergridEntity.swift
index 4e15683..322ef29 100644
--- a/sdks/swift/Source/UsergridEntity.swift
+++ b/sdks/swift/Source/UsergridEntity.swift
@@ -608,4 +608,22 @@ public class UsergridEntity: NSObject, NSCoding {
     public func getConnections(client:UsergridClient, direction:UsergridDirection, relationship:String,
query:UsergridQuery? = nil, completion:UsergridResponseCompletion? = nil) {
         client.getConnections(direction, entity: self, relationship: relationship, query:query,
completion: completion)
     }
+
+    // MARK: - Helper methods -
+
+    /**
+     Determines if the two `UsergridEntity` objects are equal.  i.e. they have the same non
nil uuidOrName.
+
+     - parameter entity: The entity to check.
+
+     - returns: If the two `UsergridEntity` objects are equal.  i.e. they have the same non
nil uuidOrName.
+     */
+    public func isEqualToEntity(entity: UsergridEntity?) -> Bool {
+        guard let selfUUID = self.uuidOrName,
+              let entityUUID = entity?.uuidOrName
+            else {
+                return false
+        }
+        return selfUUID == entityUUID
+    }
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/usergrid/blob/9f53f585/sdks/swift/Source/UsergridRequestManager.swift
----------------------------------------------------------------------
diff --git a/sdks/swift/Source/UsergridRequestManager.swift b/sdks/swift/Source/UsergridRequestManager.swift
index ccd41e5..65fde19 100644
--- a/sdks/swift/Source/UsergridRequestManager.swift
+++ b/sdks/swift/Source/UsergridRequestManager.swift
@@ -53,7 +53,7 @@ final class UsergridRequestManager {
 
         self.session = NSURLSession(configuration:  config,
                                     delegate:       UsergridSessionDelegate(),
-                                    delegateQueue:  NSOperationQueue.mainQueue())
+                                    delegateQueue:  nil)
     }
 
     deinit {
@@ -62,7 +62,10 @@ final class UsergridRequestManager {
 
     func performRequest(request:UsergridRequest, completion:UsergridResponseCompletion?)
{
         session.dataTaskWithRequest(request.buildNSURLRequest()) { [weak self] (data, response,
error) -> Void in
-            completion?(response: UsergridResponse(client:self?.client, data: data, response:
response as? NSHTTPURLResponse, error: error))
+            let response = UsergridResponse(client:self?.client, data: data, response: response
as? NSHTTPURLResponse, error: error)
+            dispatch_async(dispatch_get_main_queue()) {
+                completion?(response: response)
+            }
         }.resume()
     }
 }
@@ -87,27 +90,30 @@ extension UsergridRequestManager {
     func performUserAuthRequest(userAuth:UsergridUserAuth, request:UsergridRequest, completion:UsergridUserAuthCompletionBlock?)
{
         session.dataTaskWithRequest(request.buildNSURLRequest()) { (data, response, error)
-> Void in
             let dataAsJSON = try! NSJSONSerialization.JSONObjectWithData(data!, options:
NSJSONReadingOptions.MutableContainers)
+
+            var createdUser: UsergridUser? = nil
+            var responseError: UsergridResponseError? = nil
+
             if let jsonDict = dataAsJSON as? [String:AnyObject] {
                 let tokenAndExpiry = UsergridRequestManager.getTokenAndExpiryFromResponseJSON(jsonDict)
                 userAuth.accessToken = tokenAndExpiry.0
                 userAuth.expiry = tokenAndExpiry.1
 
-                var user: UsergridUser?
                 if let userDict = jsonDict[UsergridUser.USER_ENTITY_TYPE] as? [String:AnyObject]
{
-                    if let createdUser = UsergridEntity.entity(jsonDict: userDict) as? UsergridUser
{
-                        createdUser.auth = userAuth
-                        user = createdUser
+                    if let newUser = UsergridEntity.entity(jsonDict: userDict) as? UsergridUser
{
+                        newUser.auth = userAuth
+                        createdUser = newUser
                     }
                 }
-                if let createdUser = user {
-                    completion?(auth: userAuth, user:createdUser, error: nil)
-                } else {
-                    let error = UsergridResponseError(jsonDictionary: jsonDict) ?? UsergridResponseError(errorName:
"Auth Failed.", errorDescription: "Error Description: \(error?.localizedDescription).")
-                    completion?(auth: userAuth, user:nil, error:error)
+                if createdUser == nil {
+                    responseError = UsergridResponseError(jsonDictionary: jsonDict) ?? UsergridResponseError(errorName:
"Auth Failed.", errorDescription: "Error Description: \(error?.localizedDescription).")
                 }
             } else {
-                let error = UsergridResponseError(errorName: "Auth Failed.", errorDescription:
"Error Description: \(error?.localizedDescription).")
-                completion?(auth: userAuth, user:nil, error: error)
+                responseError = UsergridResponseError(errorName: "Auth Failed.", errorDescription:
"Error Description: \(error?.localizedDescription).")
+            }
+
+            dispatch_async(dispatch_get_main_queue()) {
+                completion?(auth: userAuth, user: createdUser, error: responseError)
             }
         }.resume()
     }
@@ -115,14 +121,19 @@ extension UsergridRequestManager {
     func performAppAuthRequest(appAuth: UsergridAppAuth, request: UsergridRequest, completion:
UsergridAppAuthCompletionBlock?) {
         session.dataTaskWithRequest(request.buildNSURLRequest()) { (data, response, error)
-> Void in
             let dataAsJSON = try! NSJSONSerialization.JSONObjectWithData(data!, options:
NSJSONReadingOptions.MutableContainers)
+
+            var responseError: UsergridResponseError? = nil
+
             if let jsonDict = dataAsJSON as? [String:AnyObject] {
                 let tokenAndExpiry = UsergridRequestManager.getTokenAndExpiryFromResponseJSON(jsonDict)
                 appAuth.accessToken = tokenAndExpiry.0
                 appAuth.expiry = tokenAndExpiry.1
-                completion?(auth: appAuth, error: nil)
             } else {
-                let error = UsergridResponseError(errorName: "Auth Failed.", errorDescription:
"Error Description: \(error?.localizedDescription).")
-                completion?(auth: nil, error: error)
+                responseError = UsergridResponseError(errorName: "Auth Failed.", errorDescription:
"Error Description: \(error?.localizedDescription).")
+            }
+
+            dispatch_async(dispatch_get_main_queue()) {
+                completion?(auth: appAuth, error: responseError)
             }
         }.resume()
     }
@@ -134,11 +145,17 @@ extension UsergridRequestManager {
     func performAssetDownload(contentType:String, usergridRequest:UsergridRequest, progress:
UsergridAssetRequestProgress? = nil, completion:UsergridAssetDownloadCompletion? = nil) {
         let downloadTask = session.downloadTaskWithRequest(usergridRequest.buildNSURLRequest())
         let requestWrapper = UsergridAssetRequestWrapper(session: self.session, sessionTask:
downloadTask, progress: progress)  { (request) -> Void in
+            var asset: UsergridAsset? = nil
+            var responseError: UsergridResponseError? = nil
+
             if let assetData = request.responseData where assetData.length > 0 {
-                let asset = UsergridAsset(data: assetData, contentType: contentType)
-                completion?(asset: asset, error:nil)
+                asset = UsergridAsset(data: assetData, contentType: contentType)
             } else {
-                completion?(asset: nil, error: "Downloading asset failed.  No data was recieved.")
+                responseError = UsergridResponseError(errorName: "Download Failed.", errorDescription:
"Downloading asset failed.  No data was recieved.")
+            }
+
+            dispatch_async(dispatch_get_main_queue()) {
+                completion?(asset: asset, error: responseError)
             }
         }
         self.sessionDelegate.addRequestDelegate(requestWrapper.sessionTask, requestWrapper:requestWrapper)
@@ -148,7 +165,10 @@ extension UsergridRequestManager {
     func performAssetUpload(usergridRequest:UsergridAssetUploadRequest, progress:UsergridAssetRequestProgress?
= nil, completion: UsergridAssetUploadCompletion? = nil) {
         let uploadTask = session.uploadTaskWithRequest(usergridRequest.buildNSURLRequest(),
fromData: usergridRequest.multiPartHTTPBody)
         let requestWrapper = UsergridAssetRequestWrapper(session: self.session, sessionTask:
uploadTask, progress: progress)  { [weak self] (request) -> Void in
-            completion?(response: UsergridResponse(client: self?.client, data: request.responseData,
response: request.response as? NSHTTPURLResponse, error: request.error),asset:usergridRequest.asset,error:nil)
+            let response = UsergridResponse(client: self?.client, data: request.responseData,
response: request.response as? NSHTTPURLResponse, error: request.error)
+            dispatch_async(dispatch_get_main_queue()) {
+                completion?(asset: usergridRequest.asset, response: response)
+            }
         }
         self.sessionDelegate.addRequestDelegate(requestWrapper.sessionTask, requestWrapper:requestWrapper)
         requestWrapper.sessionTask.resume()

http://git-wip-us.apache.org/repos/asf/usergrid/blob/9f53f585/sdks/swift/Source/UsergridUser.swift
----------------------------------------------------------------------
diff --git a/sdks/swift/Source/UsergridUser.swift b/sdks/swift/Source/UsergridUser.swift
index 7c70142..17415ba 100644
--- a/sdks/swift/Source/UsergridUser.swift
+++ b/sdks/swift/Source/UsergridUser.swift
@@ -375,7 +375,7 @@ public class UsergridUser : UsergridEntity {
                 return
         }
 
-        client.authenticateUser(userAuth, setAsCurrentUser:(self == client.currentUser),
completion: completion)
+        client.authenticateUser(userAuth, setAsCurrentUser:self.isEqualToEntity(client.currentUser),
completion: completion)
     }
 
     /**


Mime
View raw message