Sharing Core Data objects between iCloud users
Use Core Data and CloudKit to synchronize data between devices of an iCloud user and share data between different iCloud users.
摘自:developer.apple.com/documentati…
1. Implement a custom sharing flow
Apps can implement a custom sharing flow when the system sharing UI is unavailable or doesn’t fit. The sample app performs the following tasks so users can share photos from watchOS:
- It creates a share using
share(_:to:completion:)when an owner shares a photo.
- It configures the share with appropriate permissions, and adds participants for a share. A share is private if its
publicPermissionis.none. For shares that have a public permission more permissive than.none(called public shares), users can participate by tapping the share link, so there’s no need to add participants beforehand. The sample app looks up participants usingfetchParticipants(matching:into:completion:), configures the participant permission usingCKShare.Participant.permission, and adds it to the share usingaddParticipant(_:).
-
It allows users to deliver the share URL (
CKShare.url) to a participant usingShareLink. -
It implements
userDidAcceptCloudKitShare(with:)to accept the share usingacceptShareInvitations(from:into:completion:). After the acceptance synchronizes, the photo and its relationships are available in the participant’s shared persistent store.
- It manages the participants using
addParticipant(_:)andremoveParticipant(_:)from the owner side, or stops the sharing or participation by callingpurgeObjectsAndRecordsInZone(with:in:completion:). (The purge API deletes the zone from CloudKit, and also the object graph from the Core Data store. Apps that need to keep the object graph can make a deep copy, ensure the new graph doesn’t connect to any share, and save it to the store.). In this process, the sample app callspersistUpdatedShare(_:in:completion:)when it changes the share using CloudKit APIs forNSPersistentCloudKitContainerto update the store.