Skip to content

Instantly share code, notes, and snippets.

//
// ImageLoading.swift
// CloudKitDemo
//
// Created by Marcus Smith on 2/29/16.
// Copyright © 2016 FrozenFireStudios. All rights reserved.
//
import UIKit
import CloudKit
// 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
}
//
// UIImage+CKAsset.swift
// CloudKitDemo
//
// Created by Marcus Smith on 2/29/16.
// Copyright © 2016 FrozenFireStudios. All rights reserved.
//
import UIKit
import CloudKit
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)
}
if let asset = record["myImageKey"] as? CKAsset,
data = NSData(contentsOfURL: asset.fileURL),
image = UIImage(data: data)
{
// Do something with the image
}
@MarcusSmith
MarcusSmith / RetrieveLongLivedOperations.swift
Created February 22, 2016 21:59
Retrieve persisted long-lived operations, reassign their completion blocks and add them back to a CKDatabase
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
@MarcusSmith
MarcusSmith / KickOffLongLivedOperation.swift
Created February 22, 2016 21:56
Create new CKOperation and make it long lived
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)
@MarcusSmith
MarcusSmith / CKQueryOperation.swift
Created February 22, 2016 20:08
CKQueryOperation
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
@MarcusSmith
MarcusSmith / CKModifyRecordsOperation.swift
Created February 22, 2016 19:47
CKModifyRecordsOperation
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)
@MarcusSmith
MarcusSmith / CKFetchRecordsOperation.swift
Last active February 22, 2016 19:47
CKFetchRecordsOperation
let operation = CKFetchRecordsOperation(recordIDs: myRecordIDs)
operation.fetchRecordsCompletionBlock = { (recordsByRecordID, error) in
// recordsByRecordID is a dictionary of records, keyed by their recordIDs
}
database.addOperation(operation)