swiftasynchronousasync-awaitinputbaraccessoryview

How to use async and await on a default function?


I am having a problem when I am trying to add async and await to a default function.

The function I am trying to add it to is:

func inputBar(_ inputBar: InputBarAccessoryView, didPressSendButtonWith text: String)

But it looks like when I add async the function will no longer get called when I press the button because it apparently is a different function. How can I fix this?


Solution

  • You can't change Apple's function but you can incorporate.

    var task: Task<Void, Never>? = nil // Put this at class level.
    
    func inputBar(_ inputBar: InputBarAccessoryView, didPressSendButtonWith text: String){
    
        task?.cancel() // Cancels previous operations
        task = Task{
             //Do something here
        }
    }