androidkotlinpepper

Does Pepper QISDK sync calls ensure a result?


From the QiSDK docs it is understandable, that async() calls of Future<T> has result statuses.

e.x.

var goto = GoToBuilder..
var operation = goto.async().run().thenConsume {    
//if(it.isSuccess or it.hasError()) etc. statuses are available
}
//These statuses are also available in the $operation variable afterwards
if(operation.hasError()) {...}

But when we call operations sync()

e.x. just

var goto = GoToBuilder...
goto.run()

Does it ensures that the operation will be completed with the success?

Will it also lock the current Thread or what should we know about sync() calls? Because the docs say, that the best practice is to always use async(), but what if I don't want Pepper to create a new Thread for an action and process them alongside with the code.


Solution

  • Does it ensures that the operation will be completed with the success?

    No, using async() or not has no effect on chances of success; what changes is that if an error occurs:

    Will it also lock the current Thread or what should we know about sync() calls?

    Yes, a sync call will block the current thread until the action is finished (succeeds, is cancelled or fails), which means you should never do that in the android main thread (the UI thread), or you will get a NetworkOnUIThread exception.

    Note that not all calls finish on their own; for example chat.run() on a Chat object will run "forever", that is, until it's cancelled or an error is raised. That's another case where .sync() is useful, because you will get a future that you can cancel in order to stop the chat.