社交应用程序的Firebase JSON结构
我为Firebase中的帖子和用户制作了简单的JSON结构:社交应用程序的Firebase JSON结构,json,swift,firebase,firebase-realtime-database,Json,Swift,Firebase,Firebase Realtime Database,我为Firebase中的帖子和用户制作了简单的JSON结构: posts UID postKey content: postId: postImageStringUrl: profileImageUrl: timestamp: username: users UID email: profileImageUrl: reputation: usern
posts
UID
postKey
content:
postId:
postImageStringUrl:
profileImageUrl:
timestamp:
username:
users
UID
email:
profileImageUrl:
reputation:
username:
我想知道这样做可以吗?像这样,我只知道如何通过UID查询帖子,但不知道如何查询不属于当前用户的所有帖子。是否可以像这样查看其他用户配置文件
像这样,我应该只查询SPCIC用户帖子:
databaseRef.child("posts").child(currentUser.generalDetails.uid).observe(.value, with: { (posts) in
var newPostsArray = [Post]()
for post in posts.children {
let newPost = Post(snapshot: post as! FIRDataSnapshot)
newPostsArray.insert(newPost, at: 0)
}
self.postsArray = newPostsArray
self.tableView.reloadData()
}) { (error) in
print(error.localizedDescription)
}
对于帖子,我有如下结构(最小):
应用程序的功能是什么
正如我在另一个线程中告诉您的,您可以使用如下函数查询每个人的帖子:
func loadPosts(userID: String) {
let usersRef = firebase.child("Posts").child(userID)
usersRef.observeEventType(.Value, withBlock: { snapshot in
if snapshot.exists() {
self.tweets.removeAll()
let sorted = (snapshot.value!.allValues as NSArray).sortedArrayUsingDescriptors([NSSortDescriptor(key: "date",ascending: false)])
for element in sorted {
let message = element.valueForKey("message")! as? String
let userID = element.valueForKey("backendlessObjectID")! as? String
let name = element.valueForKey("name")! as? String
let date = element.valueForKey("date")! as? String
let t = Tweet(message: message!, userID: userID!, name: name!, date: date!)
self.tweets.append(t)
}
}
self.tableView.reloadData()
})
}
然后使用以下任一方法调用函数:
loadPosts(currentUserID)
或者,您首先收集所有实际发布帖子的用户,将他们的ID存储到一个包含var userIDs:[String]=[]
的数组中,然后循环遍历该数组
for user in userIDs {
loadPosts(user)
}
这样,您就可以获得所有用户的所有帖子。我曾经制作过一个类似的应用程序,我使用的JSON树结构是:-
myApp :{
Users :{
userID1 :{
userName : userName1,
userLocation : New York,
},
FriendID1 : {....},
FriendID2 : {....},
..
},
friendsListOfUsers :{
userID1 : {
friendID1 : true,
friendID2 : true,
friendID3 : true,
...
},
..
},
postsCreatedByUser : {
userID1 :{
myPostID1 : true,
myPostID2 : true,
},
FriendID1 :{
myFriends1PostID1 : true
},
FriendID2 :{
myFriends2PostID1 : true
},
FriendID3 :{
myFriends3PostID1 : true
},
},
postsIdsToShowInFeed : {
userID1 :{
myFriends1PostID1 : true, //Posts created by this user's friends
myFriends2PostID1 : true,
myFriends3PostID1 : true,
myPostID1 : true, //Posts created by the user itself
myPostID2 : true
},
FriendsID1 : {...},
},
postsCreatedByTheAuthenticatedUsers : {
myFriend1PostID1 : {Posts Details....}, //Posts created by this user's friends
myFriend2PostID1 : {Posts Details....}, //Posts created by this user's friends
myFriend3PostID1 : {Posts Details....}, //Posts created by this user's friends
myPostID1 : {Posts Details....}, //Posts created by the user itself
myPostID2 : {Posts Details....},
}
}
为了解决用户朋友唯一ID的问题,我使用了一个hack将用户ID与时间戳集成,并将其设置为postsId的
在从postsIdsToShowInMyFeed
检索数据时,从postID提取唯一ID,然后访问由经过身份验证的用户创建的用户postscreated
节点
张贴
您的postId将如下所示:-
WDfIyaJ***************2iw82+495671998919
其中495671998919是您的时间戳
检索后
let feedFriendID=“\(postKey[postKey.startIndex..你能解释一下你的应用程序结构,以及你的应用程序都是关于什么的吗..在知道之前很难评论...)我添加了一些细节:)这里不是有太多的嵌套吗?这是社交应用程序可以获得的最简单的JSON。此外,我正在从PostScreatedByTheuthenticatedUsers
节点中删除uniqueID,但是你必须操纵你的安全规则,以便当前用户只能访问其帖子中的那些密钥IDSThowInfeed节点
let timeStamp = Int(Date.timeIntervalSinceReferenceDate*1000)
let newPost = "\(FIRAuth.auth()!.currentUser!.uid)+" + "\(timeStamp)"
WDfIyaJ***************2iw82+495671998919
let feedFriendID = "\(postKey[postKey.startIndex..<postKey.characters.index(postKey.startIndex, offsetBy: postKey.characters.count - 13)])" // 13 because `+495671998919` is of 13 character length
let friendPath = "postsCreatedByTheAuthenticatedUsers/\(feedFriendID)"