迅速显示来自所有Firebase用户的信息 [英] swift showing posts from all firebase users

查看:173
本文介绍了迅速显示来自所有Firebase用户的信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用firebase作为后端来创建一个UISearchController。我目前在Firebase中有两个用户。一个用户发了一个帖子,另一个发了四个帖子。我希望能够搜索我的数据库中的所有图书(五个)的标题。不过,截至目前,我只能搜索当前用户上传的图书。以下是我的数据库外观和屏幕截图。

5ikgS.pngrel =nofollow noreferrer>


$ b $ pre $ databaseRef.child(posts)。child(userID!)。observe(.childAdded,with:{(snapshot )in


let key = snapshot.key
let snapshot = snapshot.value as?NSDictionary
snapshot?.setValue(key,forKey:uid)

if(key == self.loggedUser?.uid)
{
print(与登录用户相同)
}
else
$ b self.usersArray.append(snapshot)
self.followUsersTableView.insertRows(at:[IndexPath(row:self.usersArray.count-1,section:0)],与:UITableViewRowAnimation .automatic)
}



)){(error)in
print(error.localizedDescription)
}
$


$ div

解决方案

您将需要一个不同的父节点所谓的书: - $ /

现在你的JSON是这样的: - $ / $>

 帖子:{
userID1:{
BOOK_11_ID:{.....}
};

userID2:{
BOOK_21_ID:{.....},
BOOK_22_ID:{.....},
BOOK_23_ID:{.... 。,
BOOK_24_ID:{.....},
}
}

您必须修改您的数据库JSON结构:

 文章:{

Users_books:{
userID1:{
BOOK_11:true // Value'true'表示这个用户
//已经订阅或购买了这本书或者任何
};

userID2:{
BOOK_21:true,
BOOK_22:true,
BOOK_23:true,
BOOK_24:true,
}
$,

书籍:{
BOOK_11_ID:{/ Book details /};
BOOK_21_ID:{/ Book details /};
BOOK_22_ID:{/ Book details /};
BOOK_23_ID:{/ Book details /};
BOOK_24_ID:{/ Book details /};


$ b

修改您的安全规则,所有经过验证的用户都可以看到书。

  {
rules:{

Users_books:{

.write:auth!= null&& auth.uid === $ uid,//只允许用户制作
.read:auth!= null&& auth.uid === $ uid

},
Books:{
.write:auth!= null,//将允许所有通过应用程序认证的用户访问每一本书的详细信息。
.read:auth!= null
}
}
}

现在你要做的是: -
$ b $ 1)如果它的用户正在创建这本书;然后将图书详细信息存储在书籍 uid 下的父节点Books中,并将book_ID的值设置为true。



2)如果数据库是所有后端,即你输入它;无论何时用户订阅或购买书籍,只需获取该书的uid,并在该用户的部分中将其设置为true即可。



PS :总是扇出你的数据库;它更容易搜索它。虽然我很确定你想在不久的将来存储用户的一些个人细节,所以只是做另一个节点。将它命名为'用户',安全规则将与'Users_books'以及'用户'父节点下的每个用户的详细信息他们的 uid 作为他们的关键。


I need to create a UISearchController using firebase as the backend. I currently have two users in firebase. One user has made one post and the other has made four posts. I will like to be able to search for the title of all of the books in my database (which is five). However, as of now, I can only search for the books the current signed in user uploaded. Below is a screenshot of what my database looks like and what my code currently looks like.

databaseRef.child("posts").child(userID!).observe(.childAdded, with: { (snapshot) in


    let key = snapshot.key
    let snapshot = snapshot.value as? NSDictionary
    snapshot?.setValue(key, forKey: "uid")

    if(key == self.loggedUser?.uid)
    {
        print("same as logged in user")
    }
    else
    {
    self.usersArray.append(snapshot)
         self.followUsersTableView.insertRows(at: [IndexPath(row:self.usersArray.count-1,section:0)], with: UITableViewRowAnimation.automatic)
    }



    }) { (error) in
        print(error.localizedDescription)
    }

}

解决方案

You will need a different parent node called Books :-

Right now your JSON is something like this :-

posts : {
    userID1 : {
       BOOK_11_ID : {.....}
       };

    userID2 : {
       BOOK_21_ID : {.....},
       BOOK_22_ID : {.....},
       BOOK_23_ID : {.....},
       BOOK_24_ID : {.....},
       }
 }

You gotta modify your database JSON structure to :-

 posts : {

   Users_books :{
    userID1 : {
       BOOK_11 : true  // Value 'true' means that this user 
                      // has subscribed or bought this book or whatever
       };

    userID2 : {
       BOOK_21 : true,
       BOOK_22 : true,
       BOOK_23 : true,
       BOOK_24 : true,
       }
      },

   Books:{
       BOOK_11_ID : {/Book details/};
       BOOK_21_ID : {/Book details/};
       BOOK_22_ID : {/Book details/};
       BOOK_23_ID : {/Book details/};
       BOOK_24_ID : {/Book details/};
       }

 }

Modify your security rules for section 'Book' to make is visible to all the authenticated users.

{
 "rules" :{

  "Users_books" : {

     ".write" : "auth != null && auth.uid === $uid",  // Will allow only the user to make any change within its node and no-one else
     ".read": "auth != null && auth.uid === $uid"

       },
  "Books" : {
      ".write" : "auth != null",  // Will allow all the users that are authenticated to your app to access every books detail.
      ".read" : "auth != null"
       }
     } 
  }

Now what you gotta do is :-

1) If its the user that is creating the book ; Then store the book details in the parent node 'Books' under the books uid and set the value of book_ID to true in the users node.

2) If the database is all backend i.e you input it; Whenever the user subscribe's or buys a books just fetch the uid of that book and set it to true in the section of that user.

PS : Always fan out your database ; its much easier to search from it .Although i am pretty sure you are gonna wanna store some personal details of the user in the near future so for that just make another node . Name it 'Users', security rules will be same as 'Users_books' and under 'Users' parent node append details of every which user with their uid as their key.

这篇关于迅速显示来自所有Firebase用户的信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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