Bartłomiej Semańczyk
Bartłomiej Semańczyk

Reputation: 61784

How do I subscribe for changes from Shared Database?

When I create subscription:

let options: CKSubscriptionOptions = [.firesOnRecordCreation, .firesOnRecordUpdate, .firesOnRecordDeletion]
let subscription = CKDatabaseSubscription(recordType: "Territory", predicate: NSPredicate(value: true), options: options)

and save it to my shared database:

sharedDatabase?.save(subscription) { _, _ in }

It still doesn't appear in my Cloudkit dashboard. Why?

enter image description here

While for private database, appears...

enter image description here

After I register I need to know what record has changed and what type of change it was.

Upvotes: 0

Views: 828

Answers (2)

Eamonn
Eamonn

Reputation: 171

Dunno if you ever figured this out, it's been a year, but:

func subscribeToSharedDBForRecordType(recordType:String){

        let newSubscription = CKDatabaseSubscription()
        newSubscription.recordType = recordType

        let info = CKSubscription.NotificationInfo()
        info.soundName = "default"
        info.alertBody = "Something changed the record!"
        newSubscription.notificationInfo = info

        CKContainer.default().sharedCloudDatabase.save(newSubscription) { [weak self] savedSubscription, error in

            guard let savedSubscription = savedSubscription, error == nil else {
                print("error saving subscription:\(String(describing: error))")
                return
            }

            UserDefaults.standard.set(newSubscription.subscriptionID, forKey: "someUniqueKey")
        }

    }

Will subscribe you to all changes to the given record type in the shared db. The next thing to do would be to look at the zone notification to determine which zone was affected. I didn't do that for my project, so I don't know how.

Upvotes: 0

Adolfo
Adolfo

Reputation: 1862

You have to create a custom zone where to save your Territory record type using CKDatabaseSubscription

Be aware that CKQuerySubscription is not supported in the shared database, and CKDatabaseSubscription currently only tracks the changes from custom zones in the private and shared database.

More info at Technical Q&A QA1917

Upvotes: 3

Related Questions