CloudKit 查询中的 UITableView 部分

Posted

技术标签:

【中文标题】CloudKit 查询中的 UITableView 部分【英文标题】:UITableView Sections from CloudKit Query 【发布时间】:2016-09-21 18:19:40 【问题描述】:

我有一个简单的 CloudKit 记录,它有两个字段,名称和等级。我希望能够对 CloudKit 进行查询,返回所有记录,但按等级分组。我知道我可以用 NSFetchResultsController 做到这一点,但似乎找不到用 CKQuery 做到这一点的简单方法。

当前获取代码:

    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?)
    

【问题讨论】:

什么是“等级”的一些例子?是字符串字母等级吗?有/没有修饰符? (例如:“A”、“A-”?)某种数字等级? 这是一个字符串。可能是 K,1,2,3... 好的,太好了。我在下面提供了一个解决方案,应该能够满足您的需求。如果您有任何问题,请告诉我。 【参考方案1】:

要将检索到的 CKRecords 数组拆分为多个部分以在 UITableView 中显示,您可以使用下面的帮助器类。

(CKQuery 本身不提供执行此分段的功能 - 它只是使您能够检索记录数组,可以选择排序。)


使用SectionedCKRecords 类:

首先,使用 CKQuery 从 CloudKit 获取所需的记录。 (您的示例代码已经这样做了。)这将为您提供一个 CKRecords 数组。

假设这些记录(根据您的示例代码)包含一个存储字符串值的“Grade”键,并且您希望根据“等级”。

简单地说:

1.) 使用 CKRecords 数组和所需的 sectionNameKey 初始化 SectionedCKRecords

let sectionedRecords = SectionedCKRecords(records: records, sectionNameKey: "Grade")

2.) 实现您的 UITableViewDataSource 以在 sectionedRecords 上调用适当的方法:

SectionedCKRecords 公开了类似于 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 行为:

如果您想自定义 sectionIndexTitles 的生成方式,可以将 sectionIndexTitleForSectionName 闭包传递给 SectionedCKRecords 初始化程序。

默认情况下,SectionedCKRecords 匹配 NSFetchedResultsController 的行为以生成 sectionIndexTitles,使用部分名称的大写首字母。

闭包接受一个字符串(sectionName)作为输入,并返回sectionIndexTitle。

SectionIndexTitleForSectionName 结构中提供了一些示例闭包。

例子:

let sectionedRecords = SectionedCKRecords(records: records, sectionNameKey: "Grade", sectionIndexTitleForSectionName: SectionIndexTitleForSectionName.firstLetterOfString)

SectionedCKRecords.swift: (Swift 3)

// SectionedCKRecords.swift (Swift 3)
// © 2016 @breakingobstacles (http://***.com/users/57856/breakingobstacles)
// Source: http://***.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)

【讨论】:

【参考方案2】:

就像模型一样,您可以为 tableView 编写类似的东西。您可以编辑仪表板条目或键以按您想要的任何方式进行排序。

Teachers.swift

class Teachers: NSObject 
var recordID: CKRecordID!
var name: String!
var grade: String!

你可能有一个这样的提交类。

SubmitViewController.swift

//There can be a fail at any time so CloudKit send methods have an
//error being passed into the closure. You can set an isDirty property.
CKContainer.default().publicCloudDatabase.save(teacherRecord)  [unowned self] record, error in
DispatchQueue.main.async 
//code 
ViewController.dirty = true
//code 

ViewController.swift

//Property that will store an array of Teachers
//objects so that you can show them in a table view
var teachers = [Teachers]() 

//A “dirty” flag tracks when the derived data is out of sync with the primary data.
//It is set when the primary data changes. If the flag is set when the derived data
//is needed, then it is reprocessed and the flag is cleared. Otherwise,
//the previous cached derived data is used.
static var isDirty = true

//viewWillAppear() is going to clear the table view's selection if it has one,
//then it will use the isDirty flag to call loadTeachers() if it's needed.
override func viewWillAppear(_ animated: Bool) 
super.viewWillAppear(animated)

if let indexPath = tableView.indexPathForSelectedRow 
    tableView.deselectRow(at: indexPath, animated: true)


if ViewController.dirty 
    loadTeachers()

 

func loadTeachers() 
let pred = NSPredicate(value: true)
let sort = NSSortDescriptor(key: "creationDate", ascending: true)
let query = CKQuery(recordType: "TeacherType", predicate: pred)
query.sortDescriptors = [sort]

let operation = CKQueryOperation(query: query)

//Set the desiredKeys property to be an array of the record keys you want
operation.desiredKeys = ["name", "grade"]
operation.resultsLimit = 25

//CKQueryOperation has two closures. One streams records and one is
//called when the records have been downloaded. To handle this you
//can create a new array that will hold the teachers as they are parsed.
var newTeachers = [Teachers]()

//Set a recordFetchedBlock closure on the CKQueryOperation object.
//This will be given a CKRecord value for every record that gets
//downloaded, and the convert that into a Teachers object. 
operation.recordFetchedBlock =  record in
let teacher = Teachers()
teacher.recordID = record.recordID
teacher.name = record["name"] as! String
teacher.grade = record["grade"] as! String
newTeachers.append(teacher)


//Called by CloudKit when all records have been downloaded, and will be
//given two parameters: a query cursor and an error if there was one.
//The query cursor is useful if you want to implement paging.
operation.queryCompletionBlock =  [unowned self] (cursor, error) in
DispatchQueue.main.async 
    if error == nil 
        self.teachers = newTeachers
        self.tableView.reloadData()
     else 
        let ac = UIAlertController(title: "Fetch failed", message: "Please try again: \(error!.localizedDescription)", preferredStyle: .alert)
        ac.addAction(UIAlertAction(title: "OK", style: .default))
        self.present(ac, animated: true)
    



//Ask CloudKit to run it
CKContainer.default().publicCloudDatabase.add(operation)

 //End of loadTeachers()

确认:

您是否在 CloudKit 仪表板中看到了您的数据? 您在写作和阅读时是否将记录类型命名为“TeacherType”? 对于元数据索引,您是否选择了 ID 旁边的查询和日期旁边的排序 已创建? 您的设备在线吗?

*在保罗的帮助下。

【讨论】:

这对于从 CloudKit 获取数据来说非常详细,但是设置所需的键对章节标题有何帮助? 我无法确定这是否能回答问题,所以请边走边解释。

以上是关于CloudKit 查询中的 UITableView 部分的主要内容,如果未能解决你的问题,请参考以下文章

CloudKit - 每次运行相同的查询时,CKQueryOperation 结果都不同

UITableView 单元格未填充 CloudKit 数据

Swift3、CloudKit 和 UITableView 返回空白表

无法根据 Cloudkit 中的用户 ID 进行查询

为啥我不能从 Xcode 或 CloudKit Dashboard 查询 CloudKit?

Xcode Swift 在 tableView 中显示来自 CloudKit 的数据