【问题标题】:Implementing comments on feed post in swift using firebase使用 firebase 在 swift 中实现对提要帖子的评论
【发布时间】:2019-03-05 10:57:49
【问题描述】:

我正在尝试使用 swift 和 firebase 在我的应用程序中的每个提要帖子上实现评论部分,但在获取 cmets 的代码时遇到了问题。在我的函数中,它返回一个空数组messageComments,但我不知道我做错了什么。如果我希望我的 firebase 数据库结构看起来像图片中的那样,我该如何实现将这些 cmets 下载到数组中的代码?

func getFeedMessages(handler: @escaping (_ feedMessages:[FeedMessages]) -> ()){
        var feedMessagesArray = [FeedMessages]()
        var commentArray = [messageComments]()
        REF_FEEDMESSAGES.observeSingleEvent(of: .value) { (feedMessagesSnapshot) in
            guard let feedMessagesSnapshot = feedMessagesSnapshot.children.allObjects as? [DataSnapshot] else {return}

            for messages in feedMessagesSnapshot {
                let content = messages.childSnapshot(forPath: "content").value as? String ?? "Joe Flacco is an elite QB"
                let icon = messages.childSnapshot(forPath: "icon").value as? String ?? "none"
                let color = messages.childSnapshot(forPath: "color").value as? String ?? "bop"


                self.REF_FEEDCOMMENTS.observeSingleEvent(of: .value, with: { (feedCommentsSnapshot) in
                    guard let feedCommentsSnapshot = feedCommentsSnapshot.children.allObjects as? [DataSnapshot] else {return}

                    for comments in feedCommentsSnapshot {
                        commentArray.append((comments.childSnapshot(forPath: "comments").value as? messageComments!)!)
                    }
                })



                print("             comment:        ")
                print(commentArray)
                let messages = FeedMessages(content: content, color: color, icon: icon, comments: commentArray)
                feedMessagesArray.append(messages)
            }
            handler(feedMessagesArray)
        }
    }

【问题讨论】:

    标签: arrays swift firebase object firebase-realtime-database


    【解决方案1】:

    如果您也有相同的数据结构,则无需再次请求 cmets,因为它们嵌套在提要消息中。这只需要一些简单的解析,通过一些扩展就可以更容易阅读和理解。

    extension DataSnapshot {
        var string: String? {
            return value as? String
        }
        var childSnapshots: [DataSnapshot] {
            return children.allObjects as? [DataSnapshot] ?? []
        }
        func child(_ path: String) -> DataSnapshot {
            return childSnapshot(forPath: path)
        }
    }
    

    这两个扩展负责初始化对象所需的快照操作。

    extension MessageComments {
        convenience init(snapshot: DataSnapshot) {
            self.comments = snapshot.childSnapshots.map { $0.string }
        }
    }
    
    extension FeedMessages {
        convenience init(snapshot: DataSnapshot) {
            self.color = snapshot.child("color").string ?? "bop",
            self.comments = MessageComments(snapshot: snapshot.child("comments"))
            self.content = snapshot.child("content").string ?? "Joe Flacco is an elite QB",
            self.icon = snapshot.child("icon").string ?? "none",
        }
    }
    

    只需映射子快照以将每个快照初始化为FeedMessages 对象。

    func getFeedMessages(handler: @escaping (_ feedMessages: [FeedMessages]) -> ()) {
        REF_FEEDMESSAGES.observeSingleEvent(of: .value) {
            handler($0.childSnapshots.map { FeedMessages(snapshot: $0) })
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-05-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多