2017-09-16 6 views
0

Firebaseデータベースの作業を開始したばかりですが、データベースをどのように構造化するのかちょっと混乱しています。次の例では、私はユーザーオブジェクトとグループオブジェクトを持っています。各ユーザーは複数のグループに属し、各グループは複数のユーザーを持つことができます。提案されたデータベース構造は、 "Structure Your Database"に従って以下の通りです。Firebaseのデータとリレーションシップのリスト

{ 
    "users": { 
    "alovelace": { 
     "name": "Ada Lovelace", 
     "groups": { 
     "techpioneers": true, 
     "womentechmakers": true 
     } 
    } 
    }, 
    "groups": { 
    "techpioneers": { 
     "name": "Historical Tech Pioneers", 
     "startDate": "24-04-1820", 
     "members": { 
     "alovelace": true, 
     "ghopper": true, 
     "eclarke": true 
     } 
    } 
    } 
} 

のは、私は、グループ名と開始日を、私のアプリでは、リスト内のすべてのグループを表示したいとしましょう。どのようにしてそのデータベースコールを作成しますか?ユーザーオブジェクトにはグループのIDだけが含まれているので、グループの名前と開始日を見つけるために、グループごとにデータベースを別々に呼び出す必要がありますか?リストに多数のグループがある場合、それは多くの呼び出しになります。私のグループには他の多くの情報も含まれている可能性があります。 1回の呼び出しで、ユーザーのグループリスト内のすべてのグループを取得できますか?


一つ私が持っていたのに名前が含まれており、ユーザーの下のオブジェクトのグループで日付を開始することでした:

"users": { 
    "alovelace": { 
     "name": "Ada Lovelace", 
     "groups": { 
     "techpioneers":{ 
      "name": "Historical Tech Pioneers", 
      "startDate": "24-04-1820" 
      }, 
     "womentechmakers":{ 
      "name": "Women in Technology", 
      "startDate": "13-10-1823" 
      } 
     } 
    } 
    } 
} 

が、このソリューションは、重複データの多くを追加しているようです。また、名前を更新したい場合は、複数の場所でそれを行う必要があります。グループを含むスポンサー組織オブジェクトを追加して、それらをリストしたいとします。その後、情報を更新する3つの場所があります。どうすればこの問題を解決できますか?

答えて

0

次に、2つの可能性があります.1つは、必要なデータ(複製)を各ユーザーのグループノードに格納することです。

私はあなたの最初の観察者内observeSingleEvent(of: .value)を追加することです、ほとんどのをお勧めします一つである、他の(つまりはobserve(.value)observe(.childAdded)または何でもすることができます)。

var groupMembers = [AppUser]() 

は、新しいメンバーは、例えば、グループに追加されるたびに、あなたを検出する:

は、アプリ内のユーザーを表し、すべてのグループメンバーの配列、およびAPPUSERという名前のオブジェクトがあると例えば.childAddedオブザーバを使用することができます

func groupWasAddedObserver(completion: @escaping() ->()) { 

     // Add a .childAdded observer to the group's members node (groupId should be defined somewhere) : 

     groupsRef.child(groupId).child("members").observe(.childAdded, with: { [weak self] (snapshot) in 

       // When you get the snapshot, store its key, which represents the member id : 

       let memberId = snapshot.key 

       // fetch this member's profile information using its id : 

       self?.getUser(memberId, completion: { (groupMember) in 

        // Once you got the profile information of this member, add it to an array of all your members for example : 

        self?.groupMembers.append(groupMember) 

        // Call the completion handler so that you can update the UI or reload a table view somewhere maybe depending on your needs : 

        completion() 

       }) 
     }) 
} 

そして、彼または彼女のIDを知っているユーザー・データをフェッチする第二の方法:

func getUser(_ userId: String, completion: @escaping (AppUser) ->()) { 

    // Add the observerSingleEvent observer : 

    usersRef.child(userId).observeSingleEvent(of: .value, with: { (snapshot) in 

      // Get the data you need using the snapshot you get : 

      guard let email = snapshot.childSnapshot(forPath: "email").value as? String else { return } 
      guard let name = snapshot.childSnapshot(forPath: "name").value as? String else { return } 
      guard let picUrl = snapshot.childSnapshot(forPath: "picUrl").value as? String else { return } 

      // Call the completion handler to return your user/member : 

      completion(AppUser(id: snapshot.key, email: email, name: name, picUrl: picUrl)) 

     }) 
} 

スナップショットキーを使用して各ユーザーのmemberIdを取得し、このmemberIdを使用してこの特定のユーザーデータを取得します。

関連する問題