0

I was trying to send message from watch to phone. If phone is not connected, to send the message at a later time. However, when testing on actual watch, it seems like error handler not called. My sentFlag is not false.

func sendMessageToPhone(paraMessage: Dictionary<String, Any>) -> Bool
{       
    var sentFlag : Bool

    sentFlag = true

    session.sendMessage(paraMessage, replyHandler: nil,
                        errorHandler: { (_ error: Error) -> Void in
        sentFlag =  false

    })

    return sentFlag
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579

1 Answers1

0

The handlers are called asynchronously, when the corresponding event occurs. The function, on the other hand, will return right away, after the sendMessage method is called. So, your function will always return true.

You either need to implement scheduling a new message in the error handler, or if there is already a periodic background task that you schedule elsewhere, implement sentFlag as your class property, not local variable.

Eugr
  • 76
  • 2