I am developing an app with SwiftUI and Combine using MVVM architecture targeting only iOS 13. I want to implement Data sync between devices and sharing using CloudKit framework. How can I Combine-ify the CloudKit framework to use in my project? Is there anything that can be done to CloudKit operations to use Combine ?
The CloudKit API is heavily based around completion callbacks (per CloudKit Tips and Tricks), but it's not set up (currently - as of iOS 13.3) for any specific publishers. So anything you want there, you'd need to wrap the underlying APIs yourself, creating your own custom publishers.
Probably the most direct way to wrap asynchronous calls with Combine is to build your own publisher using Future publisher, and possibly Deferred publisher, depending on how you want it to react. This is the same API structure that you might use to make any async-API into a publisher.
There's an example (with source) of doing this in Using Combine with the section wrapping an asynchronous call with a Future to create a one-shot publisher and creating a repeating publisher by wrapping a delegate based API.
As a general pattern, I'd aim at making a publisher to receive whatever updates are relevant to the UI experience you want to create. A lot of the process of doing so involves choosing what you want to encapsulate and wrap (error handling, type manipulation, and determining what you want to present from underlying data changes).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With