swift 弹出视图控制器后保持视图控制器代码运行

hpxqektj  于 2023-03-16  发布在  Swift
关注(0)|答案(1)|浏览(287)

我在XMPP的帮助下实现聊天功能,聊天工作正常。现在我正在上传图片,为此我上传图片到S3桶,上传图片到S3后,我使用XMPP方法将S3图片URL作为消息发送给其他用户。这也工作正常,但问题是它只工作,如果我留在同一屏幕上,即聊天屏幕时,图片上传。因此,如果我的图像仍在上传,我从视图控制器弹出,它被释放,我的完成处理程序没有得到调用后,上传图像到s3,因为我的sendMessage方法也没有得到调用。所以我怎么能做到这一点。我怎么能发送我的消息,并得到完成处理程序调用,即使我在不同的屏幕上,而图像上传。
这是我的代码挑选图像和上传图像

@IBAction func didPressMedia(_ sender: Any) {
        ImagePickerManager().pickImage(self) { \[weak self\] pickedImage in
        guard let self = self else { return }

        let compressedImage = pickedImage.resizedImage(newSize: CGSize(width: 120, height: 120))
        
        let messageId = XMPPHelper.shared.xmppStream.generateUUID

        let chat = ChatListModel()
        chat.roomId = ""
        chat.message = ""
        chat.messageId = messageId
        chat.time = String(Date().currentTimeMillis())
        chat.to = self.viewModel.otherUser
        chat.from = self.viewModel.myJid ?? ""
        chat.type = "chat/image"
        chat.imageLocalPath = messageId
        chat.msgDeliveryStatus = MsgDeliveryStatus.sending.rawValue
        chat.isSeenSync = true
        chat.isUploading = "pending"
        
        self.viewModel.setChatDataInRealm(item: chat, isAfterPickingImage: true)

        //This is completion handler which get called after image upload to s3.
        self.viewModel.didImageUploaded = { [weak self] isImgUploaded , imgUrl, imgMessageId in
            guard let self = self else { return }
            if isImgUploaded {
                print("success uploading image")
                self.viewModel.sendMessage(message: imgUrl, isImage: true, messageId: imgMessageId). // send image url as message to other user

            }else{
                print("error uploading image")
                self.viewModel.updateRealmAfterImageUpload(uploadStatus: "failed", uploadedUrl: "", messsageId: imgMessageId)
            }
        }

        self.viewModel.uploadImage(localPath: "\(Helper.shared.getDocumentDirectoryPath(filename: messageId)).png" ,image: compressedImage, messageId: messageId)
    }
}
afdcj2ne

afdcj2ne1#

试试这个代码

class ChatManager {
    
    static let shared = ChatManager()
    private init() {}
    
    
    func uploadImage(localPath: String, image: UIImage, messageId: String, didImageUploaded: @escaping (Bool, String?, String?) -> Void) {
        
        // Code to upload image to server...
        //Once image is uploaded you can continue the logic here. and after that call the closure `didImageUploaded`
        if isImgUploaded {
            print("success uploading image")
            self.sendMessage(message: imgUrl, isImage: true, messageId: imgMessageId) // send image url as message to other user
            
        }else{
            print("error uploading image")
            self.updateRealmAfterImageUpload(uploadStatus: "failed", uploadedUrl: "", messsageId: imgMessageId)
        }
        
        // Call didImageUploaded closure after image upload if needed.
        didImageUploaded(true, "imageUrl", "messageId")
        
    }
    
    func sendMessage(message: String, isImage: Bool, messageId: String) {
        // Code to send message to other user
    }
    
    func updateRealmAfterImageUpload(uploadStatus: String, uploadedUrl: String, messageId: String) {
        // Code to update Realm database after image upload
    }
}

如何致电

ChatManager.shared.uploadImage(localPath: "\(Helper.shared.getDocumentDirectoryPath(filename: messageId)).png", image: compressedImage, messageId: messageId) { [weak self] isImgUploaded, imgUrl, imgMessageId in
    //Refresh views if needed.
 }

相关问题