Reputation: 117
I'm working on the firebase chat app and here is the first function insertConversation2 and this function creating a node of conversation array that contains all the data about chat. Now I want when user sending the message and code goes to success block, and at this point, I want to create another node of messages through function and pass the first function completion handler to the second function (finishCreatingConversation) completion handler but the issue is that it's not working fine and not creating the second node. See the firebase screenshot conversation node is creating but message-id not creating please check the code thanks. First function
func insertConversation2(with otherUserEmail: String,name:String,message:Message,completion:@escaping(Bool) -> Void){
let dformatter = DateFormatter()
dformatter.dateFormat = "dd/MM/yyy HH:mm"
let dateToString = dformatter.string(from: Date())
guard let email = UserDefaults.standard.value(forKey: "useremail") as? String else {
return}
var getSafeEmail = getUserEmail(currentEmail: email)
database.child("\(getSafeEmail)").observeSingleEvent(of: .value) { (snapshot) in
//if user is not preens to go to false block else go to furhter
guard var userNode = snapshot.value as? [String:Any] else {
completion(false)
print("user not found in insert time in networking manager")
return
}
var messageData = ""
switch message.kind{
case .text(let messageText):
messageData = messageText
default:break
}
let conversationId = "conversation_\(message.messageId)"
let newConversation:[String:Any] = [
"id":conversationId,
"other_user_email": otherUserEmail,
"name":name,
"latest_message":[
"date":dateToString,
"message":messageData,
"is_read":false
]
]
if var userConversation = userNode["conversation"] as? [[String:Any]]{
//conversation array is exiten append the conversation data
userConversation.append(newConversation)
userNode["conversation"] = userConversation
database.child("\(getSafeEmail)").setValue(userNode) { (error, ref) in
guard error != nil else {return}
self.finishCreatingConversation(conversationId: conversationId, message: message, completion: completion)
//completion(true)
}
}
else{
userNode["conversation"] = [
newConversation
]
database.child("\(getSafeEmail)").setValue(userNode) { (error, ref) in
guard error != nil else {return}
self.finishCreatingConversation(conversationId: conversationId, message: message, completion: completion) //second not working fine
//completion(true) //passing a refrence of completion in above function of insert method
}
}
}
}
Second function
func finishCreatingConversation(conversationId:String,message:Message,completion:@escaping(Bool) -> Void){
var messageData = ""
switch message.kind{
case .text(let messageText):
messageData = messageText
}
let dformatter = DateFormatter()
dformatter.dateFormat = "dd/MM/yyy HH:mm"
let dateToString = dformatter.string(from: Date())
guard let email = UserDefaults.standard.value(forKey: "useremail") as? String else {
return}
//var getUserEmail = getUserEmail(currentEmail: email)
let getUserEmailData = getUserEmail(currentEmail: email)
let collectionMessge: [String:Any] = [
"id":message.messageId,
"type":message.kind.messageKindString,
"content":messageData,
"date": dateToString,
"sender_email":getUserEmailData,
"is_read":false
]
database.child("\(conversationId)").setValue(collectionMessge) { (error, ref) in
guard error != nil else {return}
completion(true)
}
}
Upvotes: 0
Views: 605
Reputation: 100549
Replace
self.finishCreatingConversation(conversationId: conversationId, message: message, completion: completion)
with
self.finishCreatingConversation(conversationId: conversationId, message: message) { res in
completion(res)
}
Upvotes: 2