2017-08-13 17 views
3

私はfirebaseデータベースからデータを取得し、その結果を辞書の配列に格納しようとしています。辞書の配列へのFirebaseデータ。 iOS、Swift

[[郵便番号:郵便番号、緯度:87.28764862、経度:-23.94679475694、登録:登録]、[郵便番号:郵便番号、緯度:87.28764862、経度:-23.94679475694]このデータをこの方法で保存します。登録:登録]、[郵便番号:郵便番号、緯度:87.28764862、経度:-23.94679475694、登録:登録]]

は私が取得していた結果は...

トニーです:追記ポストの前に[] Tony:投稿[appname.Post]

どこが間違っていますか?

var places = [String]() 
var postsNew = [Post]() 
var posts = [String: Any]() 

DataService.ds.REF_POSTS.observe(.value, with: { (snapshot) in 
     if snapshot.hasChild("postCodes") { 
      let refToUser = DataService.ds.REF_POSTS 
      refToUser.child("postCodes").observe(.value, with: { snapshot in 

       let value = snapshot.value as? NSDictionary 
       let postsIds = value?.allKeys as! [String] 
       for postId in postsIds { 
        let refToPost = Database.database().reference(withPath: "posts/" + "postCodes/" + postId) 
        refToPost.observe(.value, with: { snapshot in 
         if snapshot.exists() { 

          let postDict = snapshot.value as? Dictionary<String, AnyObject> 
          let pCodeData = snapshot.key 
          let post = Post(postKey: pCodeData, postData: postDict!) 


          self.configureCell(post: post) 
          print("Tony: before append post \(self.postsNew)") 
          self.postsNew.append(post) 
          print("Tony: post \(self.postsNew)") 


       }else { 
          print("Tony: Couldn't get the data") 
         } 
        }) 
       } 
      }) 
     }else { 
      print("Tony: No Favs added, couldn't get the data") 
     } 

    }) 


func configureCell(post: Post) { 
    self.postData = post 

    self.latitude = post.latitude 
    self.longitude = post.longitude 
    self.postCodeDB = post.postCode 
    self.registration = post.other2 

} 

と私Postクラスで..

init(postKey: String, postData: Dictionary<String, AnyObject>) { 
    self._postKey = postKey 

私firebaseデータ... Firebase data

私はfirebaseに別のものを追加した場合、私はこの結果を得る...

答えて

1

私のコードを見直して答えを見つけました。

DataService.ds.REF_POSTS.child("postCodes").observe(.value, with: { (snapshot) in 

       let value = snapshot.value as? NSDictionary 
       let postsIds = value?.allKeys as! [String] 
       for postId in postsIds { 
        let refToPost = Database.database().reference(withPath: "posts/" + "postCodes/" + postId) 
        refToPost.observe(.value, with: { snapshot in 
         if snapshot.exists() { 

          let postDict = snapshot.value as? [String: AnyObject] 

          print("Tony: before append post \(self.posts)") 
          self.posts.append(postDict!) 
          print("Tony: post \(self.posts)") 
          self.showSightingsOnMap() 

       }else { 
          print("Tony: Couldn't get the data") 
         } 
        }) 
       } 
      }) 
関連する問題