【问题标题】:How i can pass the completion handler to another function swift ios我如何将完成处理程序传递给另一个函数 swift ios
【发布时间】:2021-11-09 02:35:03
【问题描述】:

我正在开发 firebase 聊天应用程序,这是第一个函数 insertConversation2,该函数创建了一个包含所有聊天数据的对话数组节点。 现在我想当用户发送消息和代码进入成功块,此时,我想通过函数创建另一个消息节点并传递第一个函数 完成处理程序到第二个函数(finishCreatingConversation)完成处理程序,但问题是它不能正常工作并且没有创建第二个节点。请参阅 firebase 屏幕截图对话节点正在创建但 message-id 未创建请检查代码谢谢。 第一个函数

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
            }
        }
    }
    
}

第二个功能

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)
    }
}

Firebase 图片:

【问题讨论】:

    标签: ios swift firebase


    【解决方案1】:

    替换

    self.finishCreatingConversation(conversationId: conversationId, message: message, completion: completion)
    

    self.finishCreatingConversation(conversationId: conversationId, message: message) { res in
         completion(res)
    }
    

    【讨论】:

    • 相同结果不创建消息ID节点但创建会话节点不起作用
    • 在每个位置添加断点以跟踪您的代码执行还在database.child("\(conversationId)").setV 中打印错误可能是一个权限
    • 第一个函数 insertConversation2() 的问题,最后一个 else 块 database.child("(getSafeEmail)").setValue(userNode) { (error, ref) 在错误中是 nil 并且没有到达secondfunction 和执行正在终止,但在 firebase 中它创建了对话节点
    猜你喜欢
    • 2023-03-18
    • 1970-01-01
    • 2020-09-19
    • 2016-06-05
    • 2018-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-01
    相关资源
    最近更新 更多