【问题标题】:Show posts from the users you are following - swift显示您关注的用户的帖子 - 快速
【发布时间】:2017-04-02 13:44:30
【问题描述】:

我做了一个关注功能,用户可以互相关注。问题是它没有按应有的方式使用。在用户写帖子的那一刻,它会保存在我的 Firebase 数据库中,参考如下:

FIRDatabase.database().reference().child("feed-items").childByAutoId()

提要项目是所有帖子所在的位置。但是我现在正在更改它,因此当用户发布某些内容时,它将被保存在这里:

FIRDatabase.database().reference().child("Users").child(UserID).child("Posts").childByAutoId()

我这样做是因为它以某种方式告诉我,在我的应用程序源中仅显示您关注的人的帖子会更容易。

目前,我正在获取我的提要(来自提要项)的所有帖子,如下所示:

func startObersvingDB() {
        FIRDatabase.database().reference().child("feed-items").observeEventType(.Value, withBlock: { (snapshot: FIRDataSnapshot) in
            var newUpdates = [Sweet]()



            for update in snapshot.children {
                let updateObject = Sweet(snapshot: update as! FIRDataSnapshot)
                newUpdates.append(updateObject)

            }

            self.updates = newUpdates.reverse()
            self.tableView.reloadData()


        }) { (error: NSError) in
            print(error.description)
        }
    }

然后我在 viewDidLoad 中调用 startObservingDB()。

如果你想在这里查看我的 Sweet 结构:

import Foundation
import FirebaseDatabase
import FirebaseAuth
import UIKit

struct Sweet {
    let key: String!
    let content: String!
    let addedByUser: String!
    let profilePhoto: String!
    let itemRef: FIRDatabaseReference?


    init (content: String, addedByUser: String, profilePhoto: String!, key: String = "") {
        self.key = key
        self.content = content
        self.addedByUser = addedByUser
        self.profilePhoto = profilePhoto
        self.itemRef = nil

    }

    init (snapshot: FIRDataSnapshot) {
        key = snapshot.key
        itemRef = snapshot.ref
        path  = key
        if let theFeedContent = snapshot.value!["content"] as? String {
            content = theFeedContent
        } else {
            content = ""
        }

        if let feedUser = snapshot.value!["addedByUser"] as? String {
            addedByUser = feedUser
        } else {
            addedByUser = ""
        }

        if let feedPhoto = snapshot.value!["profilePhoto"] as? String! {
            profilePhoto = feedPhoto
        } else {
            profilePhoto = ""
        }



    }

    func toAnyObject() -> AnyObject {
        return ["content":content, "addedByUser":addedByUser, "profilePhoto":profilePhoto!]
    }
}

在我的 TableViewController 中,我使用它在自定义单元格中显示名称等:

var update = updates[indexPath.row]

cell.nameLabel.text = update.addedByUser

等等。等等

我的问题是: 如何将其更改为仅显示我关注的人的帖子?

抱歉,帖子太长了

【问题讨论】:

    标签: ios swift uitableview firebase firebase-realtime-database


    【解决方案1】:

    假设您将关注者列表保存为其他父节点中的字典,如下所示:-

    user_followed_by :{
      userID2 : {
          userID1 : true,
          userID5 : true,
          userID6 : true,
          userID12 : true,
          userID99 : true, 
         } 
        }
    
     users :{
      userID2 :{
          post :{
                 postAutoID1 : {...},
                 postAutoID2 : {...},
                 ...
                  }
              }   
        }
      postsToShowToUser :{
    
         userID1 : {
             postAutoID1 : true,  //These are the post's autoID's of all the users whom
                                  // your current user is following
             postAutoID5 : true,
             postAutoID3 : true,
         },
    
    
       }
    
    /*    // If you choose to declare a separate section of the post Details in your Database.
      posts_Of_The_User :{
    
        userID1 : {
    
           postAutoID1 : {...//DETAILS},
           postAutoID2 : {...//DETAILS},
           postAutoID3 : {...//DETAILS},
          ....
               },
    
         }  */
    

    这个想法是,每当您当前用户关注的用户发布帖子时。该帖子的 autoID 会附加在 postsToShowToUser/userID 中。

    也就是说,如果 userID2 将发布一个帖子,那么将进行调用以将该帖子的 autoID 附加到所有关注该 的用户 postsToShowToUser/userID用户 ID2.

    PS:- 我强烈建议您将帖子详细信息移出post 部分。使其成为一个单独的父节点,由多个 postAutoID 作为键组成,并将数据作为值。它稍后可能会派上用场,也可以避免嵌套数据,这将有助于您在数据库中导航。

    【讨论】:

    • 我不太确定我是否理解;您是说每当用户发布帖子时,它应该检查谁在关注该用户,然后将该帖子添加到关注该用户的所有用户中? :-) 不能这样做吗?当用户发布内容时,它会出现在他们的个人资料中 - 那么所有当前用户的关注者都会进入一个数组并检查帖子?我真的不知道,但是在多个地方添加用户帖子会占用一些空间 - 对吧?
    • 基本上允许一个用户访问另一个用户的个人资料并不是一个好主意。阅读我在 PS 中的建议。此外,在 postsToShowToUser 中,您可以提供发布用户的用户 ID,而不是 true。一旦你有了帖子的 postID 和 userID。现在您可以在数据库的 post 部分中导航 postID 的帖子详细信息,而无需授予用户访问其他用户帐户的权限。我已相应地更新了我的答案。
    • 至于在每个关注者中附加 postID,您的用户可以只发布一个帖子,然后您必须遍历每个 users_Folwers,然后通过他们的 postID,您可以从中获取 postDetails最近的每个帖子。这不是一个坏主意,但是您将允许随机关注者访问您的其他用户的个人详细信息,并且加载所有详细信息并循环访问需要很长时间,您的应用用户可能不想等待那么久阅读他们朋友的帖子...!
    • 此外,您不必担心在发布帖子或网络线程将 postID 附加到关注者节点时网络连接丢失。由于这些调用是异步的并首先加载到本地缓存中,因此当您的用户重新联机时,那些剩余的附加 ID 将开始附加。 :) 快乐编码
    • 不幸的是没有:(我尝试将 currentUser 关注的所有用户都放入一个数组中。然后我遍历该数组以获取该数组中用户的所有帖子。起初它似乎工作得很好 - 它显示了它需要的帖子,但是每当 currentUser 点击由(比如说......)John Ryan 创建的帖子的点赞按钮时,John Ryan 的所有帖子都是重复的?我不知道为什么:(
    猜你喜欢
    • 2017-04-05
    • 2021-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-11
    • 1970-01-01
    • 1970-01-01
    • 2019-02-10
    相关资源
    最近更新 更多