Fetch new comments from firebase using a query - swift

I have a handleRefresh() function that is called when the user refreshes the page. When the refresh happens new comments that were posted are loaded into the tableview.
The problem i have is that when the users refreshes the data in the tableview loads twice so i get the updated comments but with duplicates where it has reloaded the old comments again.
I am a bit stuck on how to fix this.
Here is the code.
var CommentsQuery: DatabaseQuery {
let postRef = Database.database().reference().child("posts")
let postKey = keyFound
let postCommentRef = postRef.child(postKey)
let lastComment = self.comments.last
var queryRef: DatabaseQuery
if lastComment == nil {
queryRef = postCommentRef.queryOrdered(byChild: "timestamp")
} else {
let lastTimestamp = lastComment!.createdAt.timeIntervalSince1970 * 1000
queryRef = postCommentRef.queryOrdered(byChild: "timestamp").queryEnding(atValue: lastTimestamp)
}
return queryRef
}
#objc func handleRefresh() {
CommentsQuery.queryLimited(toLast: 20).observeSingleEvent(of: .value) { snapshot in
var tempComments = [Comments]()
let commentsSnap = snapshot.childSnapshot(forPath: "comments")
let allComments = commentsSnap.children.allObjects as! [DataSnapshot]
for commentSnap in allComments {
let degree = commentSnap.childSnapshot(forPath: "reply degree").value as? String ?? ""
let name = commentSnap.childSnapshot(forPath: "reply name").value as? String ?? ""
let text = commentSnap.childSnapshot(forPath: "reply text").value as? String ?? ""
let university = commentSnap.childSnapshot(forPath: "reply university").value as? String ?? ""
let photoURL = commentSnap.childSnapshot(forPath: "reply url").value as? String ?? ""
let url = URL(string: photoURL)
let timestamp = commentSnap.childSnapshot(forPath: "timestamp").value as? Double
let lastComment = self.comments.last
if snapshot.key == lastComment?.id {
let newComments = Comments(id: snapshot.key, fullname: name, commentText: text, university: university, degree: degree, photoURL: photoURL, url: url!, timestamp: timestamp!)
tempComments.insert(newComments, at: 0)
print("fetchRefresh")
}
}
self.comments.insert(contentsOf: tempComments, at: 0)
self.fetchingMore = false
self.refreshControl.endRefreshing()
self.tableView.reloadData()
}
}

If the self.comments.last is persisted across page reloads, then it seems to be that the problem is that you use queryEnding(atValue: here:
queryRef = postCommentRef.queryOrdered(byChild: "timestamp").queryEnding(atValue: lastTimestamp)
Since timestamp values are incremental (higher values are newer), you want the node with a timestamp higher than the latest value, which you do with queryStarting(atValue: and not with queryEnding(atValue:.

Related

Fetched data from Firestore returns duplicates

I am trying to fetch data from firebase firestore. The problem i have is that my fetch is returning the results x4 times. For example when i do print(name) it print the users name x4 times.
I think there may be a loop that is not working correctly?
// fetches and returns all conversations for the user with passed in uid
public func getAllConversations(for uid: String, completion: #escaping(Result<[Conversation], Error>) -> Void) {
let CurrentUser = Auth.auth().currentUser?.uid
let db = Firestore.firestore()
let ConversationRef = db.collection("users").document(CurrentUser!).collection("conversations").document(
"jVymlfbpuAYQQ9Brf8SbUZ7KCGg1")
// get the otherUserUId TO DO
ConversationRef.getDocument { snapshot, error in
if error != nil {
print("Error connecting to database")
} else {
if let document = snapshot {
if document.exists {
let data = document.data()
print(data)
let conversations: [Conversation] = data!.compactMap ({ dictionary in
guard let conversationId = data!["id"] as? String,
let name = data!["name"] as? String,
let otherUserUid = data!["other_user-uid"] as? String,
let latestMessage = data!["latest-message"] as? [String:Any],
let date = latestMessage["date"] as? String,
let message = latestMessage["message"] as? String,
let isRead = latestMessage["is-read"] as? Bool
else {
return nil
}
print(name)
let latestMessageObject = LatestMessage(date: date, text: message, isRead: isRead)
return Conversation(id: conversationId, name: name, otherUserUid: otherUserUid, latestMessage: latestMessageObject)
})
completion(.success(conversations))
}
else {
completion(.failure(DatabaseError.failedToFetch))
return
}
}
}
}
}
Please note that ConversationRef.getDocument{..} will only Return One Specific Document, which you’re Referring here :
let ConversationRef = db.collection("users").document(CurrentUser!).collection("conversations").document("jVymlfbpuAYQQ9Brf8SbUZ7KCGg1”)
So the let data = document.data()
will be single [String:Any] object(in this case Single ‘Conversation’),
not the Array of Dictionaries(eg: [Conversations]).
Try doing it this way:
// fetches and returns all conversations for the user with passed in uid
public func getAllConversations(for uid: String, completion: #escaping(Result<Conversation, Error>) -> Void) {
let CurrentUser = Auth.auth().currentUser?.uid
let db = Firestore.firestore()
let ConversationRef = db.collection("users").document(CurrentUser!).collection("conversations").document(
"jVymlfbpuAYQQ9Brf8SbUZ7KCGg1")
// get the otherUserUId TO DO
ConversationRef.getDocument { snapshot, error in
if error != nil {
print("Error connecting to database")
} else {
if let document = snapshot {
if document.exists {
if let data = document.data() {
if let conversationId = data["id"] as? String,
let name = data["name"] as? String,
let otherUserUid = data["other_user-uid"] as? String,
let latestMessage = data["latest-message"] as? [String:Any],
let date = latestMessage["date"] as? String,
let message = latestMessage["message"] as? String,
let isRead = latestMessage["is-read"] as? Bool {
print(name)
let latestMessageObject = LatestMessage(date: date, text: message, isRead: isRead)
let conversations = Conversation(id: conversationId, name: name, otherUserUid: otherUserUid, latestMessage: latestMessageObject)
completion(.success(conversations))
}
}
}
else {
completion(.failure(DatabaseError.failedToFetch))
return
}
}
}
}
}
// fetches and returns all conversations for the user with passed in uid
public func getAllConversations(for uid: String, completion: #escaping(Result<[Conversation], Error>) -> Void) {
let CurrentUser = Auth.auth().currentUser?.uid
let db = Firestore.firestore()
let ConversationRef = db.collection("users").document(CurrentUser!).collection("conversations")
ConversationRef.addSnapshotListener { snapshot, error in
if error != nil {
print("Error connecting to database")
} else {
guard let snap = snapshot else {
completion(.failure(DatabaseError.failedToFetch))
return
}
for document in snap.documents {
let data = document.data()
print(data)
guard let conversationId = data["id"] as? String,
let name = data["name"] as? String,
let otherUserUid = data["other_user-uid"] as? String,
let latestMessage = data["latest-message"] as? [String:Any],
let date = latestMessage["date"] as? String,
let message = latestMessage["message"] as? String,
let isRead = latestMessage["is-read"] as? Bool else {
return
}
print(name)
let latestMessageObject = LatestMessage(date: date, text: message, isRead: isRead)
let conversations = [Conversation(id: conversationId, name: name, otherUserUid: otherUserUid, latestMessage: latestMessageObject)]
completion(.success(conversations))
}
}
}
}

Setting up Nested Structs and populating via FireStore [Swift]

I have two structs, one nested within the other:
struct User {
let uid: String
let name: String
var pack: [Doggo]
}
struct Doggo {
let dogUid: String
let dogName: String
let dogBreed: String
let dogBorn: String
let dogProfileImageURL: String
}
Is the following code the proper way to access the firebase data for each?
guard let userUid = Auth.auth().currentUser?.uid else { return }
let userRef = self.db.collection("users").document(userUid)
let packRef = self.db.collection("users").document(userUid).collection("pack")
userRef.getDocument { (document, error) in
if let document = document, document.exists {
let data = document.data()
let name = data?["name"] as? String ?? "Anonymous"
packRef.getDocuments(completion: { (snapshot, error) in
if let err = error {
debugPrint("Error fetchings docs: \(err)")
} else {
guard let snap = snapshot else { return }
for document in snap.documents {
let data = document.data()
let dogUid = data["dogUid"] as? String ?? "No dogUid"
let dogName = data["dogName"] as? String ?? "No dogName"
let dogBreed = data["dogBreed"] as? String ?? "No dogBreed"
let dogBorn = data["dogBorn"] as? String ?? "No dogBorn"
let dogProfileImageURL = data["dogProfileImageURL"] as? String ?? "No dogProfileImageURL"
let newDoggo = Doggo(dogUid: dogUid, dogName: dogName, dogBreed: dogBreed, dogBorn: dogBorn, dogProfileImageURL: dogProfileImageURL)
self.doggos.append(newDoggo)
print(newDoggo)
}
}
self.user = User(uid: userUid, name: name, pack: self.doggos)
print(self.user)
self.configureHomeController()
self.configureMenuController()
})
} else {
print("Document does not exist")
}
}
It appears to work the way I'd like it to, but I don't want to run into issues down the line as it's quite foundational to the rest of the app.

Is it possible to read from multiple child nodes?

I want to read all three data sourcing from "Arts & Humanities" and "Beauty & Style". Is this possible?
Let ref = Database.database().reference().child("posts")
//CODE A: Pulls 2 snapshot, but doesn't display anything
let ref = Database.database().reference().child("posts").child("Arts & Humanities")
//CODE B: only pulls up the two feeds but excludes beauty and style. Vice versa
//Below is the listener code I have. This works only works with CODE B above; but ideally id like to read the post under "Beauty & Style" as well.
postsRef.observeSingleEvent(of: .value, with: { snapshot in
var tempPosts = [PostModel]()
for child in snapshot.children {
print(snapshot.childrenCount)
if let childSnapshot = child as? DataSnapshot,
let dict = childSnapshot.value as? [String:Any],
let author = dict["author"] as? [String:Any],
let uid = author["uid"] as? String,
let username = author["username"] as? String,
let fullname = author["fullname"] as? String,
let patthToImage = author["patthToImage"] as? String,
let url = URL(string:patthToImage),
let pathToImage = dict["pathToImage"] as? String,
let likes = dict["likes"] as? Int,
let postID = dict["postID"] as? String,
let message = dict["message"] as? String,
let genre = dict["genre"] as? String,
let timestamp = dict["timestamp"] as? Double {
if childSnapshot.key != lastPost?.id {
let userProfile = UserProfile(uid: uid, fullname: fullname, username: username, patthToImage: url)
let post = PostModel(genre: genre, likes: likes, message: message, pathToImage: pathToImage, postID: postID, userID: pathToImage, timestamp: timestamp, id: childSnapshot.key, author: userProfile)
tempPosts.insert(post, at: 0)
if lastPost?.id != nil {
lastPostIdChecker = lastPost!.id
}
}
}
}
return completion(tempPosts)
})

Loading audio message takes too long

I'm using JSQMessageViewController for my real-time chatting app UI, and firebase for my chatting app database. For text message and image message, the loading still consider OK, but when it comes to audio message, it take very long time. Here is my code for retrieving audio message:
func observeMessages() {
let userMessagesRef = Database.database().reference().child("messages").child(Id).child(senderId).queryLimited(toLast: 50)
userMessagesRef.observe(.childAdded, with: { (snapshot) in
let messageId = snapshot.key
let messagesRef = Database.database().reference().child("messages").child(messageId)
messagesRef.observeSingleEvent(of: .value, with: { (snapshot) in
if (dictionary["fromId"] as? String ?? "" == self.userId ? dictionary["toId"] as? String ?? "" : dictionary["fromId"] as? String ?? "") == self.contactLists.first?.id{
let date = NSDate(timeIntervalSince1970: dictionary["timestamp"] as? Double ?? 0.0)
let textString = dictionary["text"] as? String ?? ""
let imgString = dictionary["imageUrl"] as? String ?? ""
let voiceString = dictionary["voiceUrl"] as? String ?? ""
if textString == "image"{
let imageView = AsyncPhotoMediaItem(withURL: URL(string: imgString)!)
let message = JSQMessage(senderId: dictionary["toId"] as? String ?? "", senderDisplayName: "", date: date as Date, media: imageView)
self.messages.append(message!)
}else if textString == "voice"{
print("voice msg")
let url = URL(string: voiceString)
let data = try? Data(contentsOf: url!)
let voiceData = data
let voice1 = JSQAudioMediaItem(data: voiceData)
let message = JSQMessage(senderId: dictionary["toId"] as? String ?? "", senderDisplayName: "", date: date as Date, media: voice1)
self.messages.append(message!)
}else{
print("text msg")
let message = JSQMessage(senderId: dictionary["toId"] as? String ?? "", senderDisplayName: "", date: date as Date, text: dictionary["text"] as? String ?? "")
self.messages.append(message!)
}
}; DispatchQueue.main.async {
self.collectionView?.reloadData()
}
self.finishReceivingMessage(animated: true)
}, withCancel: nil)
}, withCancel: nil)
}
Is there any other way to do it or enhance it?

Swift 4 Retrieving Data From Firebase Database

func retPost2(){
runNBPOST()
////////////////////////////////////1
var pic = [String]()
var p = Post(userId: "", postId: "", postType: "", description: "", Region: "", City: "", District: "", Street: "", Area: 0, View: "", TotalPrice: 0, Pictures: pic, StreetWidth: 0, PropertyType: "")
////// for to retrive all post
print(retNBPOST())
runNBPOST()
let nbpost = retNBPOST()
for i in 1..<nbpost{
postiiD = "Post(\(i))"
self._REF_BASE.child("Post").child(postiiD).observeSingleEvent(of: .value, with: { (snapshot) in
let value = snapshot.value as? NSDictionary
let UserID2 = value?["UserID"] as? String ?? ""
p.userId = UserID2
})
self._REF_BASE.child("Post").child("\("Post(\(i))")/Adress").observeSingleEvent(of: .value, with: { (snapshot) in
let value = snapshot.value as? NSDictionary
let Region = value?["Region"] as? String ?? ""
p.Region = Region
let City = value?["City"] as? String ?? ""
p.City = City
print(p.City)
let District = value?["District"] as? String ?? ""
p.District = District
let Street = value?["Street"] as? String ?? ""
p.Street = Street
let AdditionalNumber = value?["AdditionalNumber"] as? String ?? ""
p.AdditionalNumber = AdditionalNumber
let PostalCode = value?["PostalCode"] as? String ?? ""
p.PostalCode = PostalCode
let BuldingNo = value?["BuldingNo"] as? String ?? ""
p.BuldingNo = BuldingNo
})
self._REF_BASE.child("Post").child("\(postiiD)/PostInfo").observeSingleEvent(of: .value, with: { (snapshot) in
let value = snapshot.value as? NSDictionary
let postType = value?["PostType"] as? String ?? ""
p.postType = postType
print(p.postType)
let DateOfpost = value?["DateOfpost"] as? String ?? ""
p.dateOfpost = DateOfpost
let EndDateOfPost = value?["EndDateOfPost"] as? String ?? ""
p.endDateOfPost = EndDateOfPost
let NbOfShare = value?["NbOfShare"] as? String ?? ""
p.nbOfShare = Int(NbOfShare)!
let NbOfViews = value?["NbOfViews"] as? String ?? ""
p.nbOfViews = Int(NbOfViews)!
let LastUpdate = value?["LastUpdate"] as? String ?? ""
p.lastUpdate = LastUpdate
let Description = value?["Description"] as? String ?? ""
p.description = Description
let Publisher = value?["Publisher"] as? String ?? ""
p.publisher = Publisher
let ContactTime = value?["ContactTime"] as? String ?? ""
p.contactTime = ContactTime
let Payment = value?["Payment"] as? String ?? ""
p.payment = Payment
let TotalPrice = value?["TotalPrice"] as? String ?? ""
p.TotalPrice = Double(TotalPrice)!
let NearBy = value?["NearBy"] as? String ?? ""
p.NearBy = NearBy
let StreetWidth = value?["StreetWidth"] as? String ?? ""
p.StreetWidth = Double(StreetWidth)!
// let Discount = value?["Discount"] as? String ?? ""
// p.Discount =
})
self._REF_BASE.child("Post").child("\(postiiD)/Property").observeSingleEvent(of: .value, with: { (snapshot) in
let value = snapshot.value as? NSDictionary
let Area = value?["Area"] as? String ?? ""
p.Area = Double(Area)!
print(p.Area)
let View = value?["View"] as? String ?? ""
p.View = View
let FamilyOrSingle = value?["FamilyOrSingle"] as? String ?? ""
p.FamilyOrSingle = FamilyOrSingle
let Level = value?["Level"] as? String ?? ""
p.Level = Int(Level)!
let HouseAge = value?["HouseAge"] as? String ?? ""
p.HouseAge = Int(HouseAge)!
let LandType = value?["LandType"] as? String ?? ""
p.LandType = LandType
let MeterPrice = value?["MeterPrice"] as? String ?? ""
p.MeterPrice = Double(MeterPrice)!
let NbRoom = value?["NbRoom"] as? String ?? ""
p.NbRoom = Int(NbRoom)!
let NbGuestroom = value?["NbGuestroom"] as? String ?? ""
p.NbGuestroom = Int(NbGuestroom)!
let NbBathroom = value?["NbBathroom"] as? String ?? ""
p.NbBathroom = Int(NbBathroom)!
let NbBedroom = value?["NbBedroom"] as? String ?? ""
p.NbBedroom = Int(NbBedroom)!
let NbLivingroom = value?["NbLivingroom"] as? String ?? ""
p.NbLivingroom = Int(NbLivingroom)!
let NbKitchen = value?["NbKitchen"] as? String ?? ""
p.NbKitchen = Int(NbKitchen)!
let PropertyType = value?["PropertyType"] as? String ?? ""
p.PropertyType = PropertyType
let NbApartment = value?["NbApartment"] as? String ?? ""
p.NbApartment = Int(NbApartment)!
})
// complet
self._REF_BASE.child("Post").child("\(postiiD)/Amenities").observeSingleEvent(of: .value, with: { (snapshot) in
let value = snapshot.value as? NSDictionary
let LiftAvailability = value?["LiftAvailability"] as? String ?? ""
let KitchenAvailability = value?["KitchenAvailability"] as? String ?? ""
let FurnitureAvailability = value?["FurnitureAvailability"] as? String ?? ""
let CarageAvailability = value?["CarageAvailability"] as? String ?? ""
let SwimmingPoolAvailability = value?["SwimmingPoolAvailability"] as? String ?? ""
let ParkingAvailability = value?["ParkingAvailability"] as? String
let FiberOpticAvailability = value?["FiberOpticAvailability"] as? String
let FireplaceAvailability = value?["FireplaceAvailability"] as? String
let DiningroomAvailability = value?["DiningroomAvailability"] as? String
let LaundryAvailability = value?["LaundryAvailability"] as? String
let CentralAirAvailability = value?["CentralAirAvailability"] as? String
let BalconyAvailability = value?["BalconyAvailability"] as? String
let MaidRoomAvailability = value?["MaidRoomAvailability"] as? String
let DriverRoomAvailability = value?["DriverRoomAvailability"] as? String
let InternalStairAvailability = value?["InternalStairAvailability"] as? String
let BasementAvailability = value?["BasementAvailability"] as? String
})
arrpost.append(p)
}
}
func updateHomeView(post : Post){
totalPriceLb.text = "\(String(post.TotalPrice)) SR"
areaLb.text = String(post.Area)
AddressLb.text = "\(post.City) \(post.District) \(post.Street)"
imageName.image = UIImage(named: "HomePic.jpg")
if post.PropertyType == "Home" {
bathLb.text = String(post.NbBathroom)
BedLb.text = String(post.NbBedroom)
imageName.image = UIImage(named: "HomePic.jpg")
}else if post.PropertyType == "Apartment" {
bathLb.text = String(post.NbBathroom)
BedLb.text = String(post.NbBedroom)
imageName.image = UIImage(named: "ApartPic.jpg")
}else if post.PropertyType == "Land"{
bathLb.isHidden = true
BedLb.isHidden = true
imageName.image = UIImage(named: "LandPic.jpg")
}
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
DataService.instance.retPost2()
// tableView.reloadData()
return DataService.instance.arrpost.count
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(true)
}
func tableView
(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "PostCell") as! homecell
let post = DataService.instance.arrpost[indexPath.row]
// let post = DataServiceTemp.instance.getPosts()[indexPath.row]
cell.updateHomeView(post: post)
// tableView.reloadData()
return cell
}
I have a problem in my IOS App. I'm Using Firebase for saving & Retrieving data. All connections are good and the data is retrieved fine or sometimes incomplete.
And My Problem is when I run the app the views, labels, Pictures, etc are shown Empty at first when this components should show the data retrieved from firebase. I don't know what's the problem. Is it time or anything else? So the main problem is the components are shown empty before retrieving the data completely. I want to force the app to not show empty at first but showing the components with data.
I already use all method from google
https://firebase.google.com/docs/database/ios/read-and-write
Make hover UIView above the mainVC with a UIActivityIndicator until response comes from Firebase remove it