2016-08-18 7 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と、現在のユーザーを保存する必要がありますされています

私は私の質問のarrises行をマークしましたか?このメソッドは、ネストされたブロックと上記のものよりもはるかにエレガントで正しいようだ:

/** 
    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) 
      } 
     } 
    } 

しかし、それはいくつかのランダムなエッジの場合には、バックグラウンドでユーザーに保存することを私に関する完全ではないでしょう。その後、このGroupオブジェクトがデータベースにぶら下がってしまい、次回にアプリケーションを読み込んだときにそのオブジェクトが表示されなくなります。

ここで私の恐怖は間違っていますか、私の本来の方法はこの機能を構築する正しい方法ですか?

答えて

0

あなたのオリジナルの方法は正しい方法だと思います。 ともう1つ、バックグラウンドで保存している間にactivityindicatorを表示する必要があります。

+0

これらのメソッドはネットワーキングクラスにあり、すべてのUIはこれらの関数とは別に実行されます。 – random

関連する問題