Swift -Firebase如何观察tableView单元格中的多个帖子 [英] Swift -Firebase how to observe multiple posts in tableView cells

查看:58
本文介绍了Swift -Firebase如何观察tableView单元格中的多个帖子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个tableView,每个帖子都有许多单元格.我需要观察对几个不同属性( availability && hours )的更改,我想在它们所关联的特定单元格中对其进行更新.这是我想出的,但是我认为这不是正确的方法,因为我只需要听 availability && hours 属性.

I have a tableView that has a number of cells for each post. I need to observe changes to a couple of different properties (availability && hours) and I would like to update them in the specific cells that they relate to. This is what I came up with but I don't think it is the correct way to do it because I only need to listen for changes to the availability && hours properties.

struct Post {
    var postId: String
    var uid: String
    var availability: Bool // this can change
    var hours: String // this can change
}

var arrOfPosts = [Post]() // 1 - n posts

override viewDidLoad() {
   super.viewDidLoad()

   for post in arrOfPosts {

      let postId = post.postId

      Database....child(postId).observe( .childChanged, with: { (snapshot) in

          let updatedPostId = snapshot.key

          if let indexOfItem = self.arrOfPosts.firstIndex(where: { $0.postId == updatedPostId }) {

              // update post in cell via batchUpdate
          }
      })

      Database....child(postId).observe( .removed, with: { (snapshot) in

          let deletedPostId = snapshot.key

          if let indexOfItem = self.arrOfPosts.firstIndex(where: { $0.postId == deletedPostId }) {

              // remove cell via batchUpdate
          }
      })
    }
}

推荐答案

是正确的,但是您需要在路径上明确说明以最小化响应负载,因为Firebase会增加

It's correct but you need to be specific in path to minimize response load that firebase adds more cost for

Database....child("\(postId)/availability")

相同时间小时

这篇关于Swift -Firebase如何观察tableView单元格中的多个帖子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆