I have a simple CloudKit record that has two fields, Name and Grade. I would like to be able to do a query to CloudKit returning all of the records but grouped into sections by Grade. I know I can do this with NSFetchResultsController but can't seem to find an easy way to do this with CKQuery.
Current code for fetching:
func fetchTeachers(_ completion: @escaping (_ teachers: [CKRecord]?, _ error: NSError?) -> () ) {
let query = CKQuery(recordType: TeacherType, predicate: NSPredicate(value: true))
query.sortDescriptors = [NSSortDescriptor(key:"Grade",ascending:true)]
publicDB.perform(query, inZoneWith: nil) { results, error in
completion(results, error as NSError?)
}
}
To split an array of retrieved CKRecords into sections for display in a UITableView, you can use the helper class below.
(A CKQuery itself does not provide the ability to do this sectioning - it just enables you to retrieve an array of records, optionally sorted.)
SectionedCKRecords
class:First, fetch the desired records from CloudKit using a CKQuery. (Your example code already does this.) This will provide you with an array of CKRecords.
Let's assume that those records (per your example code) contain a "Grade" key that stores a String value, and that you want to split the records into sections based on the "Grade".
Simply:
1.) Initialize a SectionedCKRecords with the array of CKRecords, and the desired sectionNameKey:
let sectionedRecords = SectionedCKRecords(records: records, sectionNameKey: "Grade")
2.) Implement your UITableViewDataSource to call the appropriate methods on sectionedRecords:
SectionedCKRecords exposes methods similar to those of NSFetchedResultsController.
class YourDataSource: UITableViewDataSource {
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let record = sectionedRecords.record(at: indexPath)
// TODO: construct a UITableViewCell based on the record
// ...
}
func numberOfSections(in tableView: UITableView) -> Int {
return sectionedRecords.sections.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return sectionedRecords.sections[section].numberOfRecords
}
func sectionIndexTitles(for tableView: UITableView) -> [String]? {
return sectionedRecords.sectionIndexTitles
}
// etc...
}
sectionIndexTitle
Behavior:If you'd like to customize how the sectionIndexTitles are generated, you can pass a sectionIndexTitleForSectionName
closure to the SectionedCKRecords initializer.
By default, SectionedCKRecords matches the behavior of NSFetchedResultsController for generating
sectionIndexTitles
, using the capitalized first letter of the section name.
The closure takes a String (the sectionName) as input, and returns the sectionIndexTitle.
Some example closures are provided in the SectionIndexTitleForSectionName struct.
Example:
let sectionedRecords = SectionedCKRecords(records: records, sectionNameKey: "Grade", sectionIndexTitleForSectionName: SectionIndexTitleForSectionName.firstLetterOfString)
// SectionedCKRecords.swift (Swift 3)
// © 2016 @breakingobstacles (http://stackoverflow.com/users/57856/breakingobstacles)
// Source: http://stackoverflow.com/a/39737583/57856
//
// License: The MIT License (https://opensource.org/licenses/MIT)
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
import UIKit
import CloudKit
// MARK: - SectionedCKRecords
class SectionedCKRecords {
private let sectionNameToSection: [String: Int]
private let sectionIndex: [String]
private let sectionIndexTitleToFirstSection: [String: Int]
init(records: [CKRecord], sectionNameKey: String, sectionIndexTitleForSectionName: (String) -> String? = SectionIndexTitleForSectionName.firstLetterOfString) {
self.records = records
self.sectionNameKey = sectionNameKey
// split records into sections
let splitResults = split(records: records, bySectionNameKey: sectionNameKey)
self.sections = splitResults.sections
self.sectionNameToSection = splitResults.sectionNameToSection
// build section index
var sectionIndex: [String] = []
var sectionIndexTitleToFirstSection: [String: Int] = [:]
for (index, section) in splitResults.sections.enumerated() {
guard let sectionIndexTitle = sectionIndexTitleForSectionName(section.name) else {
continue
}
section.indexTitle = sectionIndexTitle
if sectionIndexTitleToFirstSection.index(forKey: sectionIndexTitle) == nil {
sectionIndex.append(sectionIndexTitle)
sectionIndexTitleToFirstSection[sectionIndexTitle] = index
}
}
self.sectionIndex = sectionIndex
self.sectionIndexTitleToFirstSection = sectionIndexTitleToFirstSection
}
/// MARK: - Configuring Information
// The input array of records.
let records: [CKRecord]
// The key on the CKRecords used to determine the section they belong to. Assumes that record[sectionNameKey] returns a String value.
let sectionNameKey: String
/// MARK: - Accessing Results
// Returns the record at the given index path in the sectioned records.
func record(at indexPath: IndexPath) -> CKRecord {
return sections[indexPath.section].records[indexPath.row]
}
/// MARK: - Querying Section Information
// The sections for the fetch results.
private(set) var sections: [SectionInfo]
// Returns the section number for a given section title and index in the section index.
func section(forSectionIndexTitle sectionIndexTitle: String, at: Int) -> Int {
return sectionIndexTitleToFirstSection[sectionIndexTitle] ?? -1
}
// The array of section index titles.
var sectionIndexTitles: [String] {
get {
return sectionIndex
}
}
}
class SectionInfo: CustomStringConvertible {
var numberOfRecords: Int { return records.count }
let name: String
fileprivate(set) var indexTitle: String?
private(set) var records: [CKRecord]
init(name: String, indexTitle: String? = nil, records: [CKRecord] = []) {
self.name = name
self.indexTitle = indexTitle
self.records = records
}
fileprivate func add(record: CKRecord) {
records.append(record)
}
// MARK: - CustomStringConvertible
var description: String {
return "SectionInfo(name: \"\(name)\", indexTitle: \(indexTitle), numberOfRecords: \(numberOfRecords), records: \(records))"
}
}
// Example options for mapping section names to section index titles:
struct SectionIndexTitleForSectionName {
static let firstLetterOfString = { (string: String) -> String? in
guard let firstCharacter = (string as String).characters.first else {
return ""
}
return String(firstCharacter).uppercased()
}
static let fullString = { (string: String) -> String? in
return string as String
}
static let fullStringUppercased = { (string: String) -> String? in
return (string as String).uppercased()
}
}
/// split(records:bySectionNameKey)
///
/// Takes an input array of CKRecords, and splits them into sections using the (String) value retrieved from each record's "sectionNameKey".
///
/// The relative ordering of the records in the input array is maintained in each section.
///
/// - parameter records: An array of records to be split into sections.
/// - parameter bySectionNameKey: The key on the CKRecords used to determine the section they belong to.
/// Assumes that record[sectionNameKey] returns a String value.
///
/// - returns: An array of sections, and a dictionary mapping sectionName -> the index in the sections array.
func split(records: [CKRecord], bySectionNameKey sectionNameKey: String) -> (sections: [SectionInfo], sectionNameToSection: [String: Int])
{
func sectionName(forRecord record: CKRecord, withSectionNameKey sectionNameKey: String) -> String? {
guard let sectionNameValue = record.object(forKey: sectionNameKey) else {
assertionFailure("Record is missing expected sectionNameKey (\(sectionNameKey)): \(record)")
return nil
}
guard let sectionName = sectionNameValue as? String else {
assertionFailure("Record[\(sectionNameKey)] contains a value that cannot be converted directly to String. Record: \(record)")
return nil
}
return sectionName
}
var sections: [SectionInfo] = []
var sectionNameToSection: [String: Int] = [:]
var currentSection: SectionInfo? = nil
for record in records {
guard let sectionName = sectionName(forRecord: record, withSectionNameKey: sectionNameKey) else {
assertionFailure("Unable to obtain expected sectionNameKey (\(sectionNameKey)) for record: \(record)")
continue
}
if let currentSection = currentSection, currentSection.name == sectionName {
currentSection.add(record: record)
}
else {
// find existing section, if present
if let desiredSectionIndex = sectionNameToSection[sectionName] {
sections[desiredSectionIndex].add(record: record)
}
else {
// create new section
let newSection = SectionInfo(name: sectionName, records: [record])
sections.append(newSection)
sectionNameToSection[sectionName] = sections.count - 1
currentSection = newSection
}
}
}
return (sections: sections, sectionNameToSection: sectionNameToSection)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With