CloudKit lives on the client. You write Swift or Objective-C code to interact with Apple's iCloud servers to save and retrieve data. But what if your app wants to support push notifications? Don’t worry CloudKit has you covered. Use CKSubscription to tell CloudKit you'd like to know when records matching a predicate are created, updated, or deleted. CloudKit will deliver a special push notification to your app denoting the change, which you can customize for your needs.

Let's take a look at setting it up: (don't forget to setup and register for push notifications!)

let publicDB = CKContainer.defaultContainer().publicCloudDatabase

let subscription = CKSubscription(
  recordType: "Spaceships",
  predicate: NSPredicate(format: "TRUEPREDICATE"),
  options: .FiresOnRecordCreation
)

let info = CKNotificationInfo()

info.alertBody = "New Spaceship Entered the Fleet!"
info.shouldBadge = true

subscription.notificationInfo = info

publicDB.saveSubscription(subscription) { record, error in }

Then when you receive the push notification use it's userInfo to create a CKNotification object and grab the ID of the new record:

let notification: CKNotification = CKNotification(
  fromRemoteNotificationDictionary: userInfo
)

if notification.notificationType == CKNotificationType.Query {
  let queryNotif = notification as! CKQueryNotification
  // do something interesting with queryNotif.recordID
}