2016-11-18 22 views
3

我已經做了以下功能,用戶可以關注對方。問題在於它沒有按照應有的方式使用。在當用戶寫帖子,將這個標記下被保存在我的火力地堡數據庫中的時刻:顯示你正在關注的用戶的帖子 - swift

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 

等。等等

我的問題是: 我該如何改變,只顯示我所關注的人的帖子?

很抱歉的長期職位

回答

0

假設你保存你的關注者列表中的其他父節點像這樣的解釋: -

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

    } */ 

的想法是,只要用戶爲之當前的用戶正在關注發帖。該帖子的自動編號獲取後附於postsToShowToUser/userID

也就是說,如果userID2將使後則調用將進行追加該職位的自動識別中的所有用戶postsToShowToUser /用戶ID誰是繼userID2

PS: -我強烈建議您將您的帖子詳細信息移出post部分。使其成爲一個單獨的父節點,由多個postAutoID作爲鍵組成,並將數據作爲值發佈。它稍後可能會派上用場,也會避免嵌套數據,這將幫助您瀏覽數據庫。

+0

我不太清楚我的理解;你是說當用戶發佈帖子時,它應該檢查誰跟隨該用戶,然後將該帖子添加到所有跟隨該用戶的用戶?:-)是不可能做到這一點的;當一個用戶發佈一些東西進入他們的個人資料 - 那麼所有當前的用戶追隨者進入一個數組並檢查帖子?我真的沒有線索,但添加用戶帖子多個地方將需要一些空間 - 對嗎? –

+0

基本上允許用戶訪問其他用戶配置文件不是一個好主意。閱讀我在PS中建議的內容。而不是* postsToShowToUser *中的true,您可以提供發佈用戶的用戶標識。一旦你有帖子的帖子ID和用戶ID。現在,您可以在數據庫的* post *部分導航postID的帖子詳細信息,而不會授予用戶訪問其他用戶帳戶的權限。我已經相應地更新了我的答案。 – Dravidian

+0

至於追加每個追隨者的postID,你的用戶可以只發一個帖子,然後你將不得不遍歷每個users_Follwers,然後通過他們的postID,從那裏你將能夠獲得每個最近的帖子postDetails。這不是一個壞主意,但是您會授權隨機訪問者訪問您的其他用戶的個人詳細信息,並且需要很長時間才能加載所有詳細信息並循環播放,您的應用用戶可能不希望等待很長時間閱讀他們的朋友發佈的帖子...! – Dravidian

相關問題