Using Combine for promise chaining async functions

Combine sounds like a great framework, and is finally an excuse to dig into reactive programming. I am finding the documentation a bit spare though around the concept of promise chaining.


I am hoping to find a good example of promise chaining for combining two serial asynchronous functions -- that is, use the result of one to populate the second. Here's something I was working with as a potential use case:

  • given a UIViewController with a MKMapView
  • user pans the map, and I can store the region when mapView:regionDidChangeAnimated is called in a @published var (e.g.
    theRegion
    )
  • create a pipeline that takes
    theRegion
    and then does the following:
    -- does an asynchronous function to find all PoIs in the region
    -- does another asynchronous function to find distance for each PoI to the center of the map
    -- finally updates UI by highlighting closest PoI


I am beginning to grok Futures in general, but have not done enough to fully understand it. For a similar idea I have used a lot of OperationQueues and state management and it's a mess... hope this can help simplify that in the end.


A bonus would be showing how to execute the async functions off the main thread.

Replies

Did you look at this ? It does not specifically answer your question, but may provide some hints on how to.

h ttps://javascript.info/promise-chaining


and this:

https://stackoverflow.com/questions/48767872/combining-classic-promises-with-async

This may also be useful (aka tutorail):


h ttps://www.avanderlee.com/swift/combine/

Thanks for the pointer - it got me a little further.


I would also recommend if someone finds this thread the following:


https://heckj.github.io/swiftui-notes/


Still stuck but making progress.