我为Firebase中的帖子和用户制作了简单的JSON结构:
posts
UID
postKey
content:
postId:
postImageStringUrl:
profileImageUrl:
timestamp:
username:
users
UID
email:
profileImageUrl:
reputation:
username:
我想知道可以吗?像这样我只知道如何通过UID查询帖子,但不知道如何查询不属于当前用户的所有帖子。这样可以看到其他用户个人资料吗?
像这样我应该只查询特定用户帖子:
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)
}
对于帖子我有这样的结构(最小):
class Post: NSObject {
var ref: FIRDatabaseReference?
var key: String!
init( key: String = "") {
self.ref = FIRDatabase.database().reference()
}
init(snapshot: FIRDataSnapshot){
self.key = snapshot.key
self.ref = snapshot.ref
}
func toAnyObject() -> [String: Any] {
return ["postImageStringUrl": postImageUrl as Any,"profileImageUrl": profileImageUrl as Any, "content": content as Any,"username": username as Any,"postId":postId as Any, "timestamp": timestamp as Any]
}
}
该应用的功能是什么?:
答案 0 :(得分:1)
就像我在其他帖子中告诉你的那样,你可以使用如下函数查询每个人的帖子:
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)
}
这样您就可以获得所有用户的所有帖子。
答案 1 :(得分:1)
我做了一个类似的应用程序,我使用的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 问题,我使用黑客将用户ID与时间戳集成,并将其设置为 postsId'小号强>
从 postsIdsToShowInMyFeed
检索数据时,从 postID 中提取uniqueID,然后访问该用户 postsCreatedByTheAuthenticatedUsers
node。
let timeStamp = Int(Date.timeIntervalSinceReferenceDate*1000)
let newPost = "\(FIRAuth.auth()!.currentUser!.uid)+" + "\(timeStamp)"
你的postId看起来像这样: -
WDfIyaJ***************2iw82+495671998919
其中 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)"
每当任何用户朋友或用户自己发帖时,帖子详细信息都会保存为: -
如果currentUser发帖,则会将其保存在这些节点中: -
postsCreatedByUser
- &gt;只发布密钥postsIdsToShowInFeed
- &gt;只发布密钥postsCreatedByTheAuthenticatedUsers/userId
- &gt; [帖子键:帖子详情] 如果当前用户的朋友发帖,则会将其保存在这些节点中: -
postsIdsToShowInFeed
- &gt;只发布密钥(你必须遍历每个friendsID并执行此操作)postsCreatedByTheAuthenticatedUsers/friendID
- &gt; [帖子键:帖子详情]