Skip to content

Instantly share code, notes, and snippets.

View insidegui's full-sized avatar
🧩

Guilherme Rambo insidegui

🧩
View GitHub Profile
let container = CKContainer.default()
/// 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
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)
}
let info = CKNotificationInfo()
info.alertLocalizationKey = "movie_registered_alert"
info.alertLocalizationArgs = ["title"]
info.soundName = "default"
info.desiredKeys = ["title"]
subscription.notificationInfo = info
let subscription = CKQuerySubscription(recordType: "Movie",
predicate: NSPredicate(value: true),
options: [.firesOnRecordCreation])
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()
NSPredicate(format: "distanceToLocation:fromLocation:(location, %@) < %f", currentLocation, radius)
CKContainer.default().publicCloudDatabase.add(operation)
let predicate = NSPredicate(format: "self contains %@", title)
let query = CKQuery(recordType: "Movie", predicate: predicate)
let movieRecords: [CKRecord] = []
operation.recordFetchedBlock = { record in
// record é um registro do tipo Movie que foi obtido na operação
movieRecords.append(record)
}
operation.queryCompletionBlock = { cursor, error in
// movieRecords agora contém todos os registros que foram obtidos nesta operação
print(movieRecords)