【发布时间】:2020-09-19 07:41:56
【问题描述】:
我有一个用于我的 TableView 单元的数组,类似于 Facebook 提要。我成功地让我的应用程序版本的 Feed 成功显示了用户发帖的日期、显示创建帖子的用户电子邮件、显示随帖子上传的照片,但我似乎不知道如何为他们的用户个人资料制作一个数组。所有信息均来自 Firebase。
您可以在下面看到我的数据库结构的屏幕截图。
在这里你可以看到我存储帖子信息的帖子结构。
Posts > DocumentID > PostInformation(日期,上传的图片 url 为字符串,海报电子邮件,类似计数为 Int)
表格视图
我正在尝试从另一个名为 Public 的集合中提取用户的个人资料。
Public > UserEmail (test@gmail.com) > 用户信息(姓名、日期、头像 url)
我通过获取 Firestore 数据库的信息来获取帖子信息,并将所有内容放入数组中。
然后我将所有内容加载到 tableview 单元格中。
//发布变量 var userCommentArray = 字符串
var likeArray = [Int]()
var userPostImageArray = [String]()
var userProfilePhotoArray = [String]()
var userPostDescription = [String]()
var PosterEmail = [String]()
var postDate = [String]()
var documentIDArray = [String]()
以上是我的帖子变量
下面是我的单元格和用于加载其信息的数组。
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let Cell = tableView.dequeueReusableCell(withIdentifier: "VidaFeed", for: indexPath) as! FeedCell
print("Posteremail count = \(PosterEmail.count)")
>> //Cell.userProfilePhotoFeedCellLabel.sd_setImage(with: URL(string: self.userProfilePhotoArray[indexPath.row]))
Cell.FeedCellUserNameLabel.text = PosterEmail[indexPath.row]
Cell.dateFeedCellLabel.text = "test"
Cell.likeCountFeedCellLabel.text = String(likeArray[indexPath.row])
Cell.postImageFeedCellLabel.sd_setImage(with: URL(string: self.userPostImageArray[indexPath.row]))
Cell.postDescriptionLabel.text = userPostDescription[indexPath.row]
if userPostImageArray[indexPath.row] == ""{
Cell.postImageFeedCellLabel.isHidden = true
}
if userPostImageArray[indexPath.row] != "" {
Cell.postImageFeedCellLabel.isHidden = false
}
return Cell
//Cell.userProfilePhotoFeedCellLabel.sd_setImage(with: URL(string: self.userProfilePhotoArray[indexPath.row]))
如果我只注释掉配置文件单元格,那么我的应用程序会成功运行,但每当我添加它时它就会崩溃。
您可以在下面看到我试图确保我的 var userProfilePhotoArray 不会超出我的索引范围,因为我将它设置为等于一个变量以限制超出数组范围。
if self.profilecount <= self.PosterEmail.count{
self.profilecount = self.profilecount + 1
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return PosterEmail.count
}
func getDatFromFirestore() {
let firestoreDatabase = Firestore.firestore()
firestoreDatabase.collection("Posts").order(by: "Date" , descending : true).addSnapshotListener { (snapshot, error) in
if error != nil {
print(error?.localizedDescription ?? "Connection Error")
} else {
self.userPostImageArray.removeAll(keepingCapacity: false)
self.userCommentArray.removeAll(keepingCapacity: false)
self.userCommentArray.removeAll(keepingCapacity: false)
self.likeArray.removeAll(keepingCapacity: false)
self.PosterEmail.removeAll(keepingCapacity: false)
self.userProfilePhotoArray.removeAll(keepingCapacity: false)
for document in snapshot!.documents {
self.dbread = self.dbread + 1
print(self.dbread)
let documentID = document.documentID
self.documentIDArray.append(documentID)
self.getUserProfilePhoto()
if let postedBy = document.get("postedBy") as? String {
self.PosterEmail.append(postedBy)
let db = Firestore.firestore()
let docRef = db.collection("Public").document(postedBy)
docRef.getDocument { (document, error) in
if self.profilecount <= self.PosterEmail.count{
self.profilecount = self.profilecount + 1
print("Profile count is \(self.profilecount)")
if let document = document, document.exists {
print(document)
if let userProfile = document.get("imageUrl") as? String {
self.userProfilePhotoArray.append(userProfile)
print("time = \(Date.self), \(self.userProfilePhotoArray).")
}
}
}
}
if let postDescription = document.get("PostDescription") as? String {
self.userPostDescription.append(postDescription)
}
if let imageUrl = document.get("imageUrl") as? String {
self.userPostImageArray.append(imageUrl)
}
if let PostLikes = document.get("Likes") as? Int {
self.likeArray.append(PostLikes)
}
if let timeStamp = document.get("Date") as? Date {
let formatter = DateFormatter()
formatter.dateFormat = "MM/dd/yyyy"
let dateString = formatter.string(from: timeStamp)
let timeStampAsString = dateString
self.postDate.append(timeStampAsString)
}
}
self.VidaFeed.reloadData()
}
}
}
下面是输出,我可以清楚地看到该数组等于我的 PosterEmail 数组计数,它是否没有结束,但是每当为用户的个人资料图像重新添加单元格时,应用程序就会崩溃。
Posteremail count = 1
Profile count is 1
<FIRDocumentSnapshot: 0x6000001a2850>
time = 2020-06-01 03:00:28 +0000, "https://firebasestorage.googleapis.com/v0/b/vida-clock-11690.appspot.com/o/UserProfilePhotos%2F8C2ADEC6-C108-4C70-8CDA-ADBFCC56B4E2?alt=media&token=1c2178b8-b35b-4e2a-bbc0-c7747843b673"].
我只是卡住了,到目前为止,我已经厌倦了。有什么建议?
【问题讨论】:
标签: arrays swift firebase google-cloud-firestore