显示您正在关注的用户的帖子 - swift [英] Show posts from the users you are following - swift

查看:128
本文介绍了显示您正在关注的用户的帖子 - swift的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经做了以下功能,用户可以相互关注。问题在于它没有按照应有的方式使用。在用户撰写帖子的时候,它将被保存在我的Firebase数据库中:

FIRDatabase.database()。 ().child(feed-items)。childByAutoId()



feed-items是所有文章的地方。我改变了,但现在,所以当用户发布的东西,它将被保存在这里:


$ b

FIRDatabase.database()。reference() .child(Users)。child(UserID).child(Posts)。childByAutoId()
$ b

我这样做是因为它不知何故告诉我,只显示你在我的应用程序饲料中关注的人的帖子会更容易。



目前我正在获取我的Feed的所有帖子(来自feed-items)像这样:

pre $ func $ {
FIRDatabase.database()。reference() .child(feed-items)。
中的observeEventType(.Value,withBlock:{(snapshot:FIRDataSnapshot)var newUpdates = [Sweet]()



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


$ b self.updates = newUpdates.reverse()

print(error.description)
}




$ b $) $ b

然后在viewDidLoad中调用startObservingDB()。

 <$ c $  c> import Foundation 
导入FirebaseDatabase
导入FirebaseAuth
导入UIKit

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

$ b $ 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 :FIRDataSnapshot){
key = snapshot.key
itemRef = snapshot.ref
path = key
如果让theFeedContent = snapshot.value![content] as? String {
content = theFeedContent
} else {
content =
}

如果let feedUser = snapshot.value![addedByUser]如? String {
addedByUser = feedUser
} else {
addedByUser =
}

如果让feedPhoto = snapshot.value![profilePhoto]如?串! {
profilePhoto = feedPhoto
} else {
profilePhoto =
}



}

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

在我的TableViewController中,我使用这个在自定义单元格中显示名字等:

  var update = updates [indexPath.row] 

cell.nameLabel.text = update.addedByUser

等。
$ b

我的问题是:
我该如何改变,只显示来自我所关注的人的帖子?

解决方案

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

  user_followed_by:{
userID2:{
userID1:true,
userID5:true,
userID6:true,
userID12:true,
userID99:true,
}
}

用户:{
userID2:{
发布:{
postAutoID1:{...},
postAutoID2:{...},
。 ..



postsToShowToUser {

userID1:{
postAutoID1:true,//这是帖子的autoID所有用户
//你目前的用户在
po之后stAutoID5:true,
postAutoID3:true,
},

$ b}

/ * //如果您选择声明单独的数据库中的发布详情部分。
posts_Of_The_User:{

userID1:{

postAutoID1:{... // DETAILS},
postAutoID2:{... // DETAILS },
postAutoID3:{... // DETAILS},
....
},

} * /

这个想法是每当你当前用户所关注的用户发一个帖子。这个帖子的 autoID 附加在 postsToShowToUser / userID 中。
$ b 如果 userID2 发起了一个帖子,那么就会在所有用户 postsToShowToUser / userID 后跟随 userID2 添加该帖子的自动ID。



PS: - 我强烈建议您将您的帖子详情移出发布部分。使它成为一个单独的父节点,包含多个postAutoID作为键,并将数据作为值发布。它可能会在稍后派上用场,也会避免嵌套数据,这将有助于您浏览数据库。


I have made a following feature where users can follow each other. The problem is that it is not being used the way it should. At the moment when a user is writing a post it will be saved in my Firebase database under this reference:

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

The feed-items is where all posts are. I am changing that however now, so when a user is posting something it will be saved here:

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

I do that because it somehow tells me that would be easier to only show the posts of the people you follow in my apps feed.

At the moment I am getting all the posts for my feed (from feed-items) like this:

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)
        }
    }

And then I call startObservingDB() in the viewDidLoad.

If you want to see my Sweet struct here it is:

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!]
    }
}

And in my TableViewController I am using this to display name etc. in the custom cell:

var update = updates[indexPath.row]

cell.nameLabel.text = update.addedByUser

etc. etc.

My question is: How do I change that to only show posts from the people I am following?

Sorry for the long post

解决方案

Assuming that you are saving your followers list as an dictionary in other parent node like this :-

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},
      ....
           },

     }  */

The idea is that whenever a user whom your current user is following makes a post. That post's autoID gets appended in the postsToShowToUser/userID.

That is, if userID2 will make a post then a call will be made to append that post's autoID in all users postsToShowToUser/userID who are following the userID2.

PS:- I strongly suggest that you move your post details out of post section. Make it a separate parent node consisting of multiple postAutoID's as key and there post data as value. It might come in handy later on, also would avoid nesting data, which would help you navigate through your Database.

这篇关于显示您正在关注的用户的帖子 - swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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