I’ve two features right here one for itemizing all chat created by person and one for delete chat after someday. so, when i attempted to load person all chat it would test chat time is ended than it would deleted whereas loading. when i am going for customers chat it would carry out all issues which i discussed and what i get web page getting reload each 0.5 seconds.
Fetch Messages Perform
///Fetches and returns all of the conversations for the person with handed in e mail
public func getAllConversations(for e mail:String, completion: @escaping(End result<[Conversation], Error>)->Void){
database.youngster("(e mail)/conversations").observe(.worth, with: {snapshot in
guard let worth = snapshot.worth as? [[String: Any]] else{
completion(.failure(DatabaseError.failedToFetch))
return
}
let conversations: [Conversation] = worth.compactMap { dictionary in
guard let conversationId = dictionary["id"] as? String,
let identify = dictionary["name"] as? String,
let otherUserEmail = dictionary["other_user_email"] as? String,
let latestMessage = dictionary["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
}
let latestMessageObject = LatestMessage(date: date,
textual content: message,
isRead: isRead)
return Dialog(id: conversationId,
identify: identify,
otherUserEmail: otherUserEmail,
latestMessage: latestMessageObject)
}
completion(.success(conversations))
})
}
Checking For Expiration Chat
public func listenForMessages(id:String, completion: @escaping (Bool, String, Date) -> Void) {
DatabaseManager.shared.getAllMessagesForConversation(with: id, completion: { [weak self] end in
guard let _ = self else {return}
swap outcome {
case .success(let messages):
print("success in getting messages:(messages)")
guard !messages.isEmpty else{
print("messages are empty")
return
}
print(messages)
guard let date = messages.first?.sentDate else {
return
}
let tempCalendar = Calendar.present
let oneHourAfter = tempCalendar.date(byAdding: .hour, worth: 1, to: date)
let (val,isNeedToDelete) = CommonClass().getDiffrenceBetweenDates(date: oneHourAfter!.timeIntervalSince1970)
print("============REMAIN TIME DB============",oneHourAfter)
print("============REMAIN DATE DB============",val)
if isNeedToDelete {
DispatchQueue.world(qos: .background).async {
self?.deleteOldChats(id: id, completion: { isDeleted in
print(isDeleted)
completion(true,String(),date)
})
}
}else {
completion(false,val,oneHourAfter ?? Date())
}
case .failure(let error):
print("didn't get messages:(error)")
}
})
}
Delete Messages Perform
non-public func deleteOldChats(id:String, completion: @escaping (Bool) -> Void) {
DatabaseManager.shared.deleteConversation(conversationId: id, completion: { [weak self] success in
guard let strongSelf = self else {return}
if success{
print("Deleted Succesfully")
completion(true)
}else {
print("Not capable of delete")
completion(false)
}
})
}
As per my understanding firebase use sockets and people are up to date each time we carry out CRUD Operation that’s the problem over right here.
i need to cease reload knowledge each time and knowledge must be deleted correctly with out affecting important thread.