Skip to content

Instantly share code, notes, and snippets.

View insidegui's full-sized avatar
🧩

Guilherme Rambo insidegui

🧩
View GitHub Profile
let predicate = NSPredicate(format: "self contains %@", title)
let query = CKQuery(recordType: "Movie", predicate: predicate)
CKContainer.default().publicCloudDatabase.add(operation)
NSPredicate(format: "distanceToLocation:fromLocation:(location, %@) < %f", currentLocation, radius)
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound]) { authorized, error in
guard error == nil, authorized else {
// not authorized...
return
}
// subscription can be created now \o/
}
UIApplication.shared.registerForRemoteNotifications()
let subscription = CKQuerySubscription(recordType: "Movie",
predicate: NSPredicate(value: true),
options: [.firesOnRecordCreation])
let info = CKNotificationInfo()
info.alertLocalizationKey = "movie_registered_alert"
info.alertLocalizationArgs = ["title"]
info.soundName = "default"
info.desiredKeys = ["title"]
subscription.notificationInfo = info
container.publicCloudDatabase.save(subscription) { [weak self] savedSubscription, error in
guard let savedSubscription = savedSubscription, error == nil else {
// awesome error handling
return
}
// subscription saved successfully
// (probably want to save the subscriptionID in user defaults or something)
}
/// Helper method to retry a CloudKit operation when its error suggests it
///
/// - Parameters:
/// - error: The error returned from a CloudKit operation
/// - block: A block to be executed after a delay if the error is recoverable
/// - Returns: If the error can't be retried, returns the error
func retryCloudKitOperationIfPossible(with error: Error?, block: @escaping () -> ()) -> Error? {
guard let effectiveError = error as? CKError else {
// not a CloudKit error or no error present, just return the original error
return error
let container = CKContainer.default()
let containerIdentifier = "iCloud.br.com.guilhermerambo.KitchenContainer"
let secondContainer = CKContainer(identifier: containerIdentifier)