This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let container = CKContainer.default() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let info = CKNotificationInfo() | |
info.alertLocalizationKey = "movie_registered_alert" | |
info.alertLocalizationArgs = ["title"] | |
info.soundName = "default" | |
info.desiredKeys = ["title"] | |
subscription.notificationInfo = info |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let subscription = CKQuerySubscription(recordType: "Movie", | |
predicate: NSPredicate(value: true), | |
options: [.firesOnRecordCreation]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
NSPredicate(format: "distanceToLocation:fromLocation:(location, %@) < %f", currentLocation, radius) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CKContainer.default().publicCloudDatabase.add(operation) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let predicate = NSPredicate(format: "self contains %@", title) | |
let query = CKQuery(recordType: "Movie", predicate: predicate) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |