2016-08-18 83 views
1

我最近努力了我的代碼可讀性和清潔度,這是我遇到很多東西。下面的方法運行良好,並做我需要的一切。解析保存在背景最好的技術

我已標記在我的問題棱行:

/** 
    Creates a new `Group` and adds the currently logged in user as the `groupOwner`. 

    - parameter name:  The name of the group 
    - parameter completion: Called when the save request has completed passing back the newly saved `Group`. If an error occured it will be returned and `group` will be `nil`. 
    */ 
    static func createNewGroup(name: String, completion:(group: Group?, error: NSError?) ->()) { 

     // Instantiate our new `Group` 
     let newGroup = Group() 
     newGroup.groupName = name 
     newGroup.groupOwner = User.currentUser()! 

     newGroup.saveInBackgroundWithBlock { (success, error) in 

      // There was some problem saving the newGroup so return the error 
      if let err = error { 
       completion(group: nil, error: err) 
      } 

      // The newGroup was saved OK. 
      else { 

       // Now, since we've successfully saved the newGroup, we can append that to our current user. 
       User.currentUser()!.ownedGroups.append(newGroup) 

       // 
       // HERE IS MY QUESTION 
       // 
       // Next up is the save the current user since he/she has been modified. 
       User.currentUser()?.saveInBackgroundWithBlock({ (success, error) in 

        // If there was some error saving the user, then we should delete the newGroup so it isn't hanging around in the db. 
        if let err = error { 
         newGroup.deleteInBackground() 
         completion(group: nil, error: err) 
        } 

        // Everything went OK some return our recently saved newGroup 
        else { 
         completion(group: newGroup, error: nil) 
        } 
       }) 
      } 
     } 
    } 

我的問題是我應該保存與saveInBackgroundWithBlock當前用戶?這種方法似乎比嵌套塊上面的一個更優雅和正確的:

/** 
    Creates a new `Group` and adds the currently logged in user as the `groupOwner`. 

    - parameter name:  The name of the group 
    - parameter completion: Called when the save request has completed passing back the newly saved `Group`. If an error occured it will be returned and `group` will be `nil`. 
    */ 
    static func createNewGroup(name: String, completion:(group: Group?, error: NSError?) ->()) { 

     // Instantiate our new `Group` 
     let newGroup = Group() 
     newGroup.groupName = name 
     newGroup.groupOwner = User.currentUser()! 

     newGroup.saveInBackgroundWithBlock { (success, error) in 

      // There was some problem saving the newGroup so return the error 
      if let err = error { 
       completion(group: nil, error: err) 
      } 

      // The newGroup was saved OK. 
      else { 

       // Now, since we've successfully saved the newGroup, we can append that to our current user. 
       User.currentUser()!.ownedGroups.append(newGroup) 

       // Save and return 
       User.currentUser()?.saveInBackground() 
       completion(group: newGroup, error: nil) 
      } 
     } 
    } 

但是,對我而言,在一些隨機邊緣的情況下,保存到用戶在後臺將無法完成。然後我會將這個對象掛在我的數據庫中,並且用戶下次加載應用程序時也不會看到它。

我的恐懼在這裏是虛假的還是我的原始方法去構建此功能的正確方法?

回答

0

我認爲你的原始方法是正確的方法。 還有一件事,你需要在保存背景時顯示一個activityindicator。

+0

我有這些方法在網絡類,所以所有的用戶界面是從這些功能分開完成:) – random