Beginning CloudKit - Part 7: Subscriptions | Ray Wenderlich

This video will show you how to use subscriptions to inform you about changes to your CloudKit records.


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/4247-beginning-cloudkit/lessons/7

I wish we had a simpler example than Session, where remoteRecord has a var, init, init? in its class.

So, if you had a simple CoreData NSManagedObject Session class already defined with a list of vars (but without remoteRecord, say existing app where we need to add new remoteRecord), how exactly would you add the remoteRecord property, so the guard statement will not fail? What kind of init()s will be needed, if any?

guard let recordName = session?.remoteRecord.recordID.recordName else {
  return
}

Hi Brian,
Thanks for taking the time to update “Introduction to CloudKit” for iOS 10 and XCode 8, it’s really a good tutorial on a complicated subject.
I noticed that all CKSubscription initializers (CKSubscription(recordType: predicate: options:) is deprecated in iOS 11. This is quite straightforward to replace with a CKQuerySubscription.

However, other parts of the subscription fonctionality is also deprecated, like CKMarkNotificationsReadOperation, or CKFetchNotificationChangesOperation … Could you give us a hint about how to replace these? For once, the Apple doc is not particularly clear …
Or maybe you will tackle those in a future video tutorial series ?

Thanks !
Fred

Also, I could complete the challenge as asked, but can’t have the subscription work properly on a real device. The subscriptions I create just don’t appear in the dashboard (the subscription type appears, though), and nothing happens when I delete or update a record under watch by the subscription.

I notice that you didn’t demonstrate the arrival of a notification when a record under subscription is deleted or updated. Did you too encounter issues ?

Hi,

When I push on the “Follow Button”, the subscription is already created but I get an error on RWDevConDB.share.publicDB.save(subscription), subscriptionId is nil.

I’m running on iOS11, swift4.

Great tutorial … until now … but maybe it is only on my side?

Is there is somebody who succeeded with the subscription?

Brian, may we have your feedbak? As Freddy said, the video didn’t show us the happy end! :))

ok, I get my first subscription :))))

after

“let subscription = CKQuerySubscription(recordType: RemoteRecords.session, predicate: predicate, options: [.firesOnRecordDeletion,.firesOnRecordUpdate])”

add the following

let notificationInfo = CKNotificationInfo()
notificationInfo.alertBody = “something happening”
notificationInfo.shouldBadge = true
subscription.notificationInfo = notificationInfo

  RWDevConDB.share.publicDB.save(subscription) {
    subcriptionId, error in
    if error != nil {
      print("RWDevCon follow error: \(error!.localizedDescription)")
    } else {
      DispatchQueue.main.async {
        self.followButton.titleLabel?.text = "Follow"
        self.notifyUser(message: "Session followed")
      }
    }

Then go to the dashboard and you’ll see the subscription type

I was able to add the new lines of code and I do see the subscriptions. However, I used by iPhone to do this and when I changed the data on CloudKit, I did not receive any notifications. Any suggestions on what I am doing wrong to test this?

@vegetarianzombie Do you have any feedback about this? Thank you - much appreciated! :]

Hi Brian.

I can’t get any remote notification from my real device.

Think I checked everything but couldn’t find what I am missing here.

Is there any further step or setting to get actual remote notification?

@bdmoakley Can you please help with this when you get a chance? Thank you - much appreciated! :]

Is your app configured to receive push notifications? That may be part of the problem.

Thank you for response!
It seems push notification capability was not a problem…
Actually right after I wrote the original question, suddenly notification started working…
Still don’t know why though

Hi there

I followed this excellent CK Tutorial

I have an issue:
After I successfully created the subscription (I can see it in the DashBoard)
An then I change one Attendee FirstName (adding some characters)
I have got no feedback in return… I do not see any notification (and the Notifcations are allowed for the RW App.

Any thought ?

You don’t need to enable push notifications. Per Apple’s documentation, that is something that is automatically handled for you. if you aren’t receiving the notification, make sure you are on a device and have an active subscription.

@freddyf, have you found a workaround to replace the deprecated APIs?

Thanks.

@bdmoakley i have a problem with notification CKAccountChanged, my app use subscription to the public database, so i need to take action, when user sign out from the icloud, been frustated for 2 days for this problem, the notifications comes, but the problem is, notification comes 5 times, last resort i create new blank project, i put AppDelegate as observer for CKAccountChanged notification, and still called 5 times, it’s makes me crazy, appreciate for the help… i already try in blank view controller too, add observer in viewWillAppear, remove it in viewWillDisappear, always get same result, CKAccountChanged comes 5 times again, been crazy about this, please help…

Hi @kireonz, I haven’t run into that issue. My suggestion is to head over to the Apple developer forums and see what they have to say. StackOverflow is another alternative.

@kireonz Do you still have issues with this?

Yeap, notification still arrive 5 times, frustated now it almost 3 weeks, and maybe i will option out icloud

I managed to create subscription programmatically and receive update notification using NSPredicate(value: true). However when I try to subscribe to specific Session using recordID, I couldn’t get any notification despite dashboard showing the subscription is saved.

How can I troubleshoot whether the subscription is working correctly? There’s not much information on iCloud dashboard.