Created
July 7, 2015 20:15
-
-
Save evermeer/5df7ad1f8db529893f40 to your computer and use it in GitHub Desktop.
CloudKit working with cursor
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
/** | |
Generic query handling | |
- parameter type: An object instance that will be used as the type of the records that will be returned | |
- parameter query: The CloudKit query that will be executed | |
- parameter completionHandler: The function that will be called with the result of the query | |
- parameter errorHandler: The function that will be called when there was an error | |
:return: No return value | |
*/ | |
internal func queryRecords<T:EVCloudKitDataObject>(type:T, query: CKQuery, completionHandler: (results: [T]) -> Bool, errorHandler:((error: NSError) -> Void)? = nil) { | |
if !(query.sortDescriptors != nil) { | |
query.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)] | |
} | |
let operation = CKQueryOperation(query: query) | |
var results = [T]() | |
operation.recordFetchedBlock = { record in | |
if let parsed = self.fromCKRecord(record) as? T { | |
results.append(parsed) | |
} | |
} | |
operation.queryCompletionBlock = { cursor, error in | |
self.handleCallback(error, errorHandler: errorHandler, completionHandler: { | |
if completionHandler(results: results) { | |
if cursor != nil { | |
self.queryRecords(cursor!, continueWithResults: results, completionHandler: completionHandler, errorHandler: errorHandler) | |
} | |
} | |
}) | |
} | |
operation.resultsLimit = CKQueryOperationMaximumResults; | |
database.addOperation(operation) | |
} | |
/** | |
Generic query handling continue from cursor | |
- parameter type: An object instance that will be used as the type of the records that will be returned | |
- parameter cursor: the cursor to read from | |
- parameter completionHandler: The function that will be called with the result of the query | |
- parameter errorHandler: The function that will be called when there was an error | |
:return: No return value | |
*/ | |
private func queryRecords<T:EVCloudKitDataObject>(cursor: CKQueryCursor, continueWithResults:[T], completionHandler: (results: [T]) -> Bool, errorHandler:((error: NSError) -> Void)? = nil) { | |
var results = continueWithResults | |
let operation = CKQueryOperation(cursor: cursor) | |
operation.recordFetchedBlock = { record in | |
if let parsed = self.fromCKRecord(record) as? T { | |
results.append(parsed) | |
} | |
} | |
operation.queryCompletionBlock = { cursor, error in | |
self.handleCallback(error, errorHandler: errorHandler, completionHandler: { | |
if completionHandler(results: results) { | |
if cursor != nil { | |
self.queryRecords(cursor!, continueWithResults: results, completionHandler: completionHandler, errorHandler: errorHandler) | |
} | |
} | |
}) | |
} | |
operation.resultsLimit = CKQueryOperationMaximumResults; | |
database.addOperation(operation) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment