Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Nov 27, 2023. It is now read-only.

🌤 Swift Combine extensions for reactive CloudKit record processing

License

NotificationsYou must be signed in to change notification settings

chris-araman/CombineCloudKit

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Swift Combine extensions for reactive CloudKit record processing. Designed for simplicity.

SwiftPlatformsLicenseRelease

Lint | Build | TestCoverage

CombineCloudKit exposesCloudKit operations asCombine publishers. Publishers can be used to process values overtime, using Combine's declarative API.

⚠️ Deprecated

I am unfortunately unable to maintain this project moving forward. If you would like to fork and maintain it, please reach out.

Please consider making use of the new async functionality in CloudKit and SwiftData.

📦 Adding CombineCloudKit to Your Project

CombineCloudKit supportsSwift Package Manager,CocoaPods, andCarthage. You can use whicheveryou prefer, but Swift Package Manager is most likely to be supported in the future.

Swift Package Manager

Add a dependency on CombineCloudKit to yourPackage.swift usingXcode or theSwift Package Manager. Optionally, specify aversion requirement.

dependencies:[.package(url:"https://github.com/chris-araman/CombineCloudKit.git", from:"1.0.0")]

Then resolve the dependency:

swift package resolve

To update to the latest CombineCloudKit version compatible with your version requirement:

swift package update CombineCloudKit

CocoaPods

Add adependency onCombineCloudKit to yourPodfile. Optionally, specify aversion requirement.

pod'CombineCloudKit','~>1.0'

Theninstall the dependency:

pod install

Toupdate to the latest CombineCloudKit versioncompatible with your version requirement:

pod update CombineCloudKit

Carthage

Add a dependency on CombineCloudKit to yourCartfile.Optionally, specify aversion requirement.

github "chris-araman/CombineCloudKit" ~> 1.0

Because Carthage assumes dependencies are provided as shared frameworks, but Swift Package Manager builds only librariesor executables, we have to generate an.xcodeproj for Carthage to use.

⚠️ Thegenerate-xcodeproj command has been deprecated. This solution may stop working in a future release of the SwiftPackage Manager.

carthage bootstrap --no-buildpushd Carthage/Checkouts/CombineCloudKit&& swift package generate-xcodeproj&&popdcarthage bootstrap --use-xcframeworks

To update to the latest CombineCloudKit version compatible with your version requirement:

carthage update CombineCloudKit --use-xcframeworks

🌤 Using CombineCloudKit in Your Project

Combine allows you to chain value processingPublishersfor one or moreSubscribers. Here, we perform a query onourCKDatabase, then process the resultsasynchronously. As eachCKRecord is read from thedatabase, it is passed to themappublisher which publishes the value of the record's name field. Any errors in the chain so far can be handled in thecatch publisher, which passesCKRecordValue valuesalong to oursink subscriber where thefinal values are processed.

import CloudKitimport Combineimport CombineCloudKitfunc queryDueItems(database:CKDatabase, due:Date){letcancellable= database.performQuery(ofType:"ToDoItem", where:NSPredicate(format:"due >= %@", due)).map{record: CKRecord-> CKRecordValuein      // Map each ToDoItem to its Nameprint("Received record:\(record)")returnrecord["Name"]}.catch{error: Errorin      // Handle any upstream errorprint("Received error:\(error)")}.sink{value: CKRecordValuein      // Process the Name of each ToDoItemsprint("Received result:\(value)")}  // ...}

Queueing and Cancellation

Just creating aPublisher does not queue a CloudKit operation. An operation is queued only once aSubscribersubscribes to thePublisher and indicatesDemand.

Note that theCancellable subscriber fromsink will cancel the upstream publisherswhen it is deinitialized. Take care to ensure that your subscribers live long enough to process values. If aCombineCloudKit publisher is cancelled before it is finished emitting values, the underlyingCKOperation will be cancelled. This may be desirablewhen performing a query and processing only the first few results. However, failing to wait for completion of asave,delete, ormodify operation may result in undesirable cancellation.

Note that because theatBackgroundPriority publishers are built onCKDatabase methods that do not provide means ofcancellation, they will not respond to requests for cancellation. If you need the publishers to respond to requests forcooperative cancellation, please use the publishers that do not haveatBackgroundPriority in their names. You canstill specifyQualityOfService.backgroundby passing in aCKOperation.Configuration.

Sharing Publishers among Multiple Subscribers

If two or moreSubscribers subscribe to the same CombineCloudKitPublisher, the operation will be queued twice.This may be surprising if you're new to Combine! Queueing the same database operation twice could be inefficient orpotentially harmful. If you need to subscribe to aPublisher twice, use theshare andmakeConnectableoperators. This will ensure the operation is queued only once.

I considered making thePublishers all conform toConnectablePublisherby default, but that would require all callers to callconnectexplicitly or to use theautoconnectoperator, even if they did not intend to share thePublisher.

For more on this topic, please review:

📘 Documentation

💯%documented usingJazzy.Hosted byGitHub Pages.

❤️ Contributing

Contributions are welcome!

📚 Further Reading

To learn more about Combine and CloudKit, watch these videos from WWDC:

...or review Apple's documentation:

If you're looking for Swift concurrency extensions for CloudKit usingasync,await,andAsyncSequence, take a look atAsyncCloudKit!

📜 License

CombineCloudKit was created byChris Araman. It is published under theMIT license.

Sponsor this project

    Contributors2

    •  
    •  

    [8]ページ先頭

    ©2009-2025 Movatter.jp