gpt4 book ai didi

swift - 使用 swift 在 CloudKit 中加载更多内容

转载 作者:行者123 更新时间:2023-11-30 12:17:07 24 4
gpt4 key购买 nike

我有 2 个函数 loadData 和 loadMore 如下:

import UIKit
import CloudKit

class HireView: UIViewController, UITableViewDelegate, UITableViewDataSource, UIGestureRecognizerDelegate, UIScrollViewDelegate, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource, UIPickerViewDelegate, UIPickerViewDataSource {

@IBOutlet weak var tableView: UITableView!
let myData = CKContainer.default().publicCloudDatabase
let predicate = NSPredicate(value: true)

override func viewDidLoad() {
super.viewDidLoad()
self.loadData()
}

func loadData(){
var result = [CKRecord]()
let query = CKQuery(recordType: "Hire", predicate: predicate)
query.sortDescriptors = [NSSortDescriptor(key: "postDate", ascending: false)]
let operation = CKQueryOperation(query: query)
operation.resultsLimit = 100
operation.qualityOfService = .userInteractive
operation.recordFetchedBlock = { (record) in
DispatchQueue.main.async {
result.append(record)
hireItems = result
self.tableView.reloadData()
}
}
operation.queryCompletionBlock = { (cursor, error) in
if error != nil {

} else {
if result.isEmpty == true {
DispatchQueue.main.async {
hireItems = result
self.tableView.reloadData()
}
}
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
}
myData.add(operation)
}

func loadMore(){
let query = CKQuery(recordType: "Hire", predicate: predicate)
query.sortDescriptors = [NSSortDescriptor(key: "postDate", ascending: false)]
let operation = CKQueryOperation(query: query)
operation.resultsLimit = hireItems.count + 100
operation.qualityOfService = .userInteractive
operation.recordFetchedBlock = { (record) in
if (record["postDate"] as! Date) < (hireItems[hireItems.count - 1]["postDate"] as! Date) {
DispatchQueue.main.async {
hireItems.append(record)
self.tableView.reloadData()
}
}
}
operation.queryCompletionBlock = { (cursor, error) in
if error != nil {
DispatchQueue.main.async {
}
} else {
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
}
myData.add(operation)
}
}

我的问题是:

loadMore() 函数是最好的吗?有什么方法可以让 loadMore() 更好吗?

最佳答案

更好的方法可以使用 CKQueryCursor在 CKQueryOperation 的 queryCompletionBlock 中返回。

如果此游标不是nil,那么您还有更多记录等待获取。要继续获取,您应该使用 init(cursor: CKQueryCursor)初始化器。

Use this method to initialize a query operation that retrieves the next batch of results from a previous search.

关于swift - 使用 swift 在 CloudKit 中加载更多内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45276523/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com