This file contains 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 operation = CKFetchRecordsOperation(recordIDs: myRecordIDs) | |
operation.fetchRecordsCompletionBlock = { (recordsByRecordID, error) in | |
// recordsByRecordID is a dictionary of records, keyed by their recordIDs | |
} | |
database.addOperation(operation) |
This file contains 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 operation = CKModifyRecordsOperation(recordsToSave: someRecords, recordIDsToDelete: someRecordIDs) | |
operation.modifyRecordsCompletionBlock = { (savedRecords, deletedRecordIDs, error) in | |
// savedRecords is an array of saved CKRecords | |
// deletedRecordIDs is an array of the CKRecordIDs for the deleted records | |
} | |
database.addOperation(operation) |
This file contains 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
var allRecords: [CKRecord] = [] | |
let operation = CKQueryOperation(query: query) | |
operation.recordFetchedBlock = { (record: CKRecord) in | |
allRecords.append(record) | |
} | |
operation.queryCompletionBlock = { (cursor: CKQueryCursor?, error: NSError?) in | |
// There is another batch of records to be fetched | |
if let cursor = cursor { | |
let newOperation = CKQueryOperation(cursor: cursor) | |
newOperation.recordFetchedBlock = operation.recordFetchedBlock |
This file contains 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 operation = CKModifyRecordsOperation(recordsToSave: largeRecords, recordIDsToDelete: nil) | |
operation.longLived = true | |
operation.longLivedOperationWasPersistedBlock = { | |
print("Operation with ID", operation.operationID, "was persisted") | |
} | |
operation.modifyRecordsCompletionBlock = { (savedRecords, _, error) in | |
print("Operation with ID", operation.operationID, "saved records", savedRecords, "error", error) | |
} | |
CKContainer.defaultContainer().publicCloudDatabase.addOperation(operation) |
This file contains 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.defaultContainer().fetchAllLongLivedOperationIDsWithCompletionHandler { (operationIDs, error) in | |
guard let operationIDs = operationIDs else { | |
print("error:", error) | |
return | |
} | |
operationIDs.forEach { (operationID) in | |
CKContainer.defaultContainer().fetchLongLivedOperationWithID(operationID) { (operation, error) in | |
guard let operation = operation else { | |
print("Unable to fetch operation with ID", operationID, "error:", error) | |
return |
This file contains 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
if let asset = record["myImageKey"] as? CKAsset, | |
data = NSData(contentsOfURL: asset.fileURL), | |
image = UIImage(data: data) | |
{ | |
// Do something with the image | |
} |
This file contains 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
do { | |
let data = UIImagePNGRepresentation(myImage)! | |
try data.writeToURL(tempURL, options: NSDataWritingOptions.AtomicWrite) | |
let asset = CKAsset(fileURL: tempURL) | |
record["myImageKey"] = asset | |
} | |
catch { | |
print("Error writing data", error) | |
} |
This file contains 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
// | |
// UIImage+CKAsset.swift | |
// CloudKitDemo | |
// | |
// Created by Marcus Smith on 2/29/16. | |
// Copyright © 2016 FrozenFireStudios. All rights reserved. | |
// | |
import UIKit | |
import CloudKit |
This file contains 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
// Get image from an asset | |
if let asset = record["myImageKey"] as? CKAsset, image = asset.image { | |
// Do something with the image | |
} | |
// Save an image as an asset | |
do { | |
let asset = try CKAsset(image: myImage) | |
record["myImageKey"] = asset | |
} |
This file contains 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
// | |
// ImageLoading.swift | |
// CloudKitDemo | |
// | |
// Created by Marcus Smith on 2/29/16. | |
// Copyright © 2016 FrozenFireStudios. All rights reserved. | |
// | |
import UIKit | |
import CloudKit |