- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我有一个简单的 CloudKit 记录,它有两个字段,Name 和 Grade。我希望能够对 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?)
}
}
最佳答案
要将检索到的 CKRecords 数组拆分为多个部分以便在 UITableView 中显示,您可以使用下面的帮助程序类。
(CKQuery 本身不提供进行这种分段的能力——它只是使您能够检索记录数组,可以选择排序。)
SectionedCKRecords
类:首先,使用 CKQuery 从 CloudKit 获取所需的记录。 (您的示例代码已经这样做了。)这将为您提供一组 CKRecords。
让我们假设这些记录(根据您的示例代码)包含存储字符串值的“Grade”键,并且您希望根据“”将记录拆分为多个部分成绩”。
简单地:
1.) 使用 CKRecords 数组和所需的 sectionNameKey 初始化一个 SectionedCKRecords:
let sectionedRecords = SectionedCKRecords(records: records, sectionNameKey: "Grade")
2.) 实现您的 UITableViewDataSource 以在 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
行为:如果您想自定义 sectionIndexTitles 的生成方式,您可以将 sectionIndexTitleForSectionName
闭包传递给 SectionedCKRecords 初始化程序。
By default, SectionedCKRecords matches the behavior of NSFetchedResultsController for generating
sectionIndexTitles
, using the capitalized first letter of the section name.
闭包将字符串(sectionName)作为输入,并返回 sectionIndexTitle。
SectionIndexTitleForSectionName 结构中提供了一些示例闭包。
例子:
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)
}
关于ios - 来自 CloudKit 查询的 UITableView 部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39623839/
要在私有(private) CKRecordZone 中添加 CKRecord,您需要确保该区域已经存在。 但这是否意味着每次我需要插入记录时,我都需要获取所有区域并使用 fetchAllRecord
我正在与 cloudkit 仪表板交互并查看我的应用程序收集的数据。 如何从仪表板导出所有数据(数据 -> csv 或 json),以便我可以对其进行一些分析? 谢谢! 最佳答案 我认为苹果永远不会提
我使用 Cloudkit 和私有(private)数据库将一些文件存储到 iCloud 并在 iOS 和 OSX 之间同步。现在我想实现一些东西,用户可以在不离开应用程序并查看首选项的情况下查看他的
我有两个看起来像这样的 CloudKit 数据对象: 父对象: { "records": [ { "recordName": "14102C0A-60F
有谁知道这个闭包中的String字段的用途public var recordWithIDWasDeletedBlock: ((CKRecordID, String) -> Void)?。我不知道它的用
我的应用程序在 CloudKit 中执行的几乎所有操作现在都会返回此错误: (lldb) po [error userInfo] { CKDHTTPHeaders = {
我在尝试初始化 Cloudkit schema 时遇到此错误: Error Domain=NSCocoaErrorDomain Code=134060 "A Core Data error occur
我正在更新 iOS13 之前的 Core Data 应用程序以使用 Core Data+CloudKit 同步来支持多个设备上的单个用户。同步应该是自动发生的,并且在我开发的一个中间步骤中它确实起作用
有没有办法使用 CloudKit 获取 iCloud 笔记?我看过文档: https://developer.apple.com/library/archive/documentation/DataM
swift 3.1,Xcode 8.3.3 我在 CloudKit 中有一个名为 Aircraft 的记录类型,它有一个名为 fieldValues 的字段,类型为 Reference List。 在
swift 3.1,Xcode 8.3.3 我在 CloudKit 中有一个名为 Aircraft 的记录类型,它有一个名为 fieldValues 的字段,类型为 Reference List。 在
当我尝试从 CloudKit Dashboard 查询 CloudKit 时,收到一条错误消息: There was a problem querying the “Entry” type. no a
出于某种原因,一小部分 iOS 10 用户无法从我的公共(public) iCloud 容器中读取数据。 CloudKit 返回的 localisedError 是“Account doesn't h
我有一个在 iOS 上运行良好的应用程序,但是当使用催化剂运行时,如果我在 macOS 上滑动到另一个虚拟桌面,然后再返回大约 10 次,它会间歇性地崩溃。它主要发生在 UICollectionVie
用户 B 从用户 A 拥有的共享记录中删除自己的正确方法是什么?我想我记得从一些 WWDC 视频中用户 B 会删除 CKShare来自他的共享数据库,但在查看用户 A 设备的权限时,该用户似乎仍然是参
对于我们的消息传递应用程序,如果我们将用户消息直接发送到 CloudKit (不做任何我们自己的加密),我们可以声称我们的应用程序是 end-to-end encrypted ,“只有通信用户可以阅读
我之前曾尝试使用 OSX/iOS 应用程序实现 CloudKit,由于我的强制症,我非常担心设备之间某些数据的优先级,以及可能丢失数据或恢复已删除的数据。 我的逻辑是,将从 iCloud 获取的数据和
Apple 发布了一种针对 CloudKit 的服务器到服务器进行身份验证的新方法。 https://developer.apple.com/library/content/documentation
因此根据Apple的文档,未登录iCloud的用户仍然可以从公共(public)数据库中读取数据,但是在查询公共(public)数据库时,我收到以下错误: Error Domain=NSCocoaEr
我需要创建一个CKQuery,其中谓词包含记录的引用,而不是记录的字段。 像这样 let query = CKQuery(recordType: "OUP", predicate: NSPredica
我是一名优秀的程序员,十分优秀!