2016-10-05 6 views
1

Firebase observeEventTypeオブザーバを再読み込みすることに関する他の質問を読んだ後、私はかなり混乱します。私は、オブザーバー辞書をコントローラー全体でアクセス可能な変数に追加しています。私はその値をデータソース辞書に割り当て、以前に追加されたすべての子を読み込みます。Firebaseの子がリアルタイムで追加されてコレクションビューで更新されない

しかし、いったん別のシミュレータから新しい値を追加しようとしたり、バックエンド内で手動で入力すると、グローバル辞書は新しい値で更新されますが、データソース変数は更新されません。一度コントローラを離れると、最終的には更新されますが、Firebaseを使用する目的は無効になります。

オープンオブザーバは、viewWillAppearにあるはずですが、オンラインソースがviewDidLoadにあるようです。

セグメント化されたコントロールを使用して各カスタムクラスを実行しているため、問題が発生している可能性があります。設定は1つのコレクションビューコントローラで、そのセルはセルでもあるカスタムコレクションビューです。

 override func viewWillAppear(animated: Bool) { 
      super.viewWillAppear(animated) 
      self.loadFireData() 
     } 

    func loadFireData() { 
    if let locationId = location.locationId { 

     let postQuery = ref.child("Posts").child(selectedRegion).child(locationId).queryOrderedByChild("descTime") 
     postQuery.observeEventType(.ChildAdded, withBlock: { (snap) in 


      if snap.exists() { 
       let postQuery = snap.value! as! [String: AnyObject] 

       let feedPost = FeedModel() 

       feedPost.value = postQuery["value"] as? String 

       feedPost.key = postQuery["key"] as? Int 
       feedPost.balance = postQuery["balance"] as? Double 
       self.post.append(feedPost) 

       dispatch_async(dispatch_get_main_queue(), { 
        self.collectionView!.reloadData() 
       }) 
      } 

      } 

     }) 
    } 

} 

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 

    if indexPath.section == 1 { 
     let cell = collectionView.dequeueReusableCellWithReuseIdentifier(feedCellId, forIndexPath: indexPath) as! LocationFeedCell 
     cell.location = location 
     //this doesn't seem to be updating here 
     cell.posts = posts 

     return cell 
    } 

すべてのヘルプは非常にあなただけのポスト配列にfeetpostを追加している

答えて

0

を理解されるであろう。だから、リロードcollectionview後、カスタムセルのラベルにして最新のfeetpostを追加する必要があります

if indexPath.section == 1 { 
      let cell = collectionView.dequeueReusableCellWithReuseIdentifier(feedCellId, forIndexPath: indexPath) as! LocationFeedCell 
      cell.location = location 
      //Replace this below line: 
      cell.posts = post[indexpath.row] as! String 

      return cell 
     } 
+0

をindexpath.rowに従って申し訳ありませんが、私は十分に早く明確ではなかったが、それはただの鍵の一つの例であることを意味しました。ポストディクショナリの値のペア。それで、これは、そのコードで遊んでいることに基づいて辞書に1つの値しか割り当てないので、まだ動作しません – SethG

関連する問題