Completion Callback to Caller

Dec 14, 2015 at 7:53 PM
We are considering using cpprestsdk to share common REST service consumption code across mobile platforms. Can anyone recommend a standard/common callback pattern for notifying the caller when (say) a long running task has completed. For example:-

iOS app invokes long running task via cpprestsdk wrapped in static iOS lib.
long running cpprestsdk task starts -> notify iOs app
long running cpprestsdk task ends in error -> notify iOS app
long running cpprestsdk task finishes -> notify iOS app.

Essentially we need to bubble up the task events through the bridging glue to the caller.
Coordinator
Dec 14, 2015 at 8:10 PM
You can return pplx::task<T> objects from your REST service layer which the main app can chain callbacks onto using .then().

Ex:
pplx::task<Image> fetch_image_async(string imagename)
{
    auto task = /* code to launch the cpprestsdk task */;
    return task.then([](http_response res)
    {
        return /* convert response into Image object */;
    });
}
Then in your app you can just do
fetch_image_async("cat.png").then([](pplx::task<Image> t)
{
    try
    {
        Image img = t.get();
        /* use the img here */
    }
    catch (std::exception& e)
    {
        /* an error occurred, take action */
    }
});