Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cloudkit with Combine

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 ?

like image 440
Mohamed Wasiq Avatar asked Dec 20 '19 13:12

Mohamed Wasiq


1 Answers

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).

like image 169
heckj Avatar answered Nov 20 '22 11:11

heckj