2017-08-15 15 views
-3

沒有人有建議如何在將新元素髮布到Firebase數據庫時擺脫用戶責任?我想創建開放式Feed,但這隻適用於特定用戶。 謝謝!打開Firebase數據庫供稿

功能啓動,如:

func fetchPosts(){ 

    let ref = Database.database().reference() 
    ref.child("users").queryOrderedByKey().observeSingleEvent(of: .value, with: { snapshot inlet users = snapshot.value as! [String : AnyObject] 

主要的問題,我在此看到:

 for (_,value) in users { 
      if let uid = value["uid"] as? String { 
       if uid == Auth.auth().currentUser?.uid { 
        if let followingUsers = value["following"] as? [String : String]{ 
         for (_,user) in followingUsers{ 
          self.following.append(user) 
         } 
        } 
        self.following.append(Auth.auth().currentUser!.uid) 

        ref.child("posts").queryOrderedByKey().observeSingleEvent(of: .value, with: { (snap) in 


         let postsSnap = snap.value as! [String : AnyObject] 

         for (_,post) in postsSnap { 
          if let userID = post["userID"] as? String { 
           for each in self.following { 
            if each == userID { 

我想這將是相同的:

         let posst = Post() 
             if let author = post["author"] as? String, let likes = post["likes"] as? Int, let pathToImage = post["pathToImage"] as? String, let postID = post["postID"] as? String { 

              posst.author = author 
              posst.likes = likes 
              posst.pathToImage = pathToImage 
              posst.postID = postID 
              posst.userID = userID 
              if let people = post["peopleWhoLike"] as? [String : AnyObject] { 
               for (_,person) in people { 
                posst.peopleWhoLike.append(person as! String) 
               } 
              } 

              self.posts.append(posst) 
             } 
            } 
           } 

           self.collectionview.reloadData() 
          } 
         } 
        }) 
       } 
      } 
     } 

    })} 
+0

目前還不清楚你在問什麼;您是否想要讓任何人能夠訪問您的Firebase?如果是這樣,那麼將讀寫規則設置爲true,並且您很好。目前還不清楚你在代碼中發現了什麼*主要問題*。如果你能澄清你的問題,我們會看看。 – Jay

+0

此代碼最初來自以下視頻:(https://www.youtube.com/watch?v=fw7ySRFtX_M&t=1920s)。作者正在創建像app一樣的Instagram,並顯示Feed,他正在使用上面編寫的代碼。我想要使​​用相同的示例,但對所有人開放。換句話說,上面的代碼應該是重新創建與用戶UID和Auth函數無關的Feed的一部分。 –

回答

0

OK,所以如果我我正確理解你的問題,你希望任何人登錄後能夠看到所有的帖子,而不僅僅是用戶的帖子埃正在跟隨。

如果確實如此,那麼您不需要檢查用戶是誰。你只是想加載所有的職位。更改

for (_,post) in postsSnap { 
    if let userID = post["userID"] as? String { 
      for each in self.following { 
       if each == userID { 

self.ref.child("posts").queryOrderedByKey().observeSingleEvent(of: .value, with: { snap in 
         if let postsSnap = snap.value as? [String: AnyObject] { 
          for(_, post) in postsSnap { 
           if let userID = post["userID"] as? String { 
            for each in self.following { 
             if each == userID { 
              let posst = Post() 

              if let author = post["author"] as? String, let likes = post["likes"] as? Int, let pathToImage = post["pathToImage"] as? String, let postID = post["postID"] as? String, let postDescription = post["postDescription"] as? String, let timestamp = post["timestamp"] as? Double, let category = post["category"] as? String, let group = post["group"] as? Int { 

               posst.author = author 
               posst.likes = likes 
               posst.pathToImage = pathToImage 
               posst.postID = postID 
               posst.userID = userID 
               posst.fancyPostDescription = self.createAttributedString(author: author, postText: postDescription) 
               posst.postDescription = author + ": " + postDescription 
               posst.timestamp = timestamp 
               posst.group = group 
               posst.category = category 
               posst.userWhoPostedLabel = self.createAttributedPostLabel(username: author, table: group, category: category) 

               if let people = post["peopleWhoLike"] as? [String: AnyObject] { 
                for(_, person) in people { 
                 posst.peopleWhoLike.append(person as! String) 
                } 
               } 
               self.posts.append(posst) 
              } // end if let 
             } 
            } 
            self.tableView.reloadData() 
           } 
          } 
         } 
        }) 
        ref.removeAllObservers() 

如果你希望其他人能夠讀取/寫入到數據庫,你必須更新在火力地堡控制檯的規則。你可以通過點擊數據庫和規則來實現。默認的規則是這樣的:

{ 
    "rules": { 
    ".read": "auth != null", 
    ".write": "auth != null" 
    } 
} 

改變他們

{ 
    "rules": { 
    ".read": "true", 
    ".write": "true" 
    } 
} 

我看了視頻你以下,他做的很多錯誤。真的,當您從Firebase請求數據時,您應該進行分頁,即加載一定數量的帖子,然後根據滾動位置加載更多。

+0

我很抱歉,但此代碼仍包含用戶標識。刪除前四行代碼後,它仍顯示警告。 –

+0

對不起,很煩人,但沒有寫入如何在沒有登錄用戶的情況下從Firebase讀取數據,以及在Tableview中使用UIViewController。 –

+0

我只是將用戶標識保存在發佈對象中,我沒有使用它來過濾出像您在代碼中一樣的帖子。如果您希望用戶能夠在未經過身份驗證的情況下讀取/寫入數據庫,則必須更改數據庫規則。我會更新我的帖子。 – DoesData