gpt4 book ai didi

ios - Cloudkit 获取非常慢

转载 作者:行者123 更新时间:2023-11-28 10:16:11 25 4
gpt4 key购买 nike

运行以下代码从 Cloudkit 获取数据,目前填充 tableView 需要很长时间,具体取决于有多少结果,但如果结果超过 15 个,则需要 10 秒以上。他们有什么方法可以加快速度吗?

这是我的获取函数:

func loadData() {
venues = [CKRecord]()
let location = locationManager.location

let radius = CLLocationDistance(500)

let sort = CKLocationSortDescriptor(key: "Location", relativeLocation: location!)

let predicate = NSPredicate(format: "distanceToLocation:fromLocation:(%K,%@) < %f", "Location", location!, radius)

let publicData = CKContainer.defaultContainer().publicCloudDatabase

let query = CKQuery(recordType: "Venues", predicate: predicate )

query.sortDescriptors = [sort]

publicData.performQuery(query, inZoneWithID: nil) { (results:[CKRecord]?, error:NSError?) in
if let venues = results {
self.venues = venues
dispatch_async(dispatch_get_main_queue(), {
self.tableView.reloadData()
self.refreshControl.endRefreshing()
self.tableView.hidden = false
})
}
}
}

这是我的 tableView 函数:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! NearMe2ViewCell

if venues.count == 0 {


return cell
}

let venue = venues[indexPath.row]




print(indexPath.row)


let venueLocation = venue["Location"] as? CLLocation
let venueTitle = (venue["Name"] as! String)
let venueImages = venue["VenuePhoto"] as! CKAsset

let userLocation = locationManager.location
let distanceBetween: CLLocationDistance = (venueLocation!.distanceFromLocation(userLocation!))
self.venueDistance = String(format: "%.f", distanceBetween)

cell.venueDistance?.text = venueDistance
cell.venueName.text = venueTitle
cell.venueImage?.image = UIImage(contentsOfFile: venueImages.fileURL.path!)


return cell


}

最佳答案

您应该首先搜索记录键,因此 fetchOperation 将包含此指令。

fetchOperation.desiredKeys = ["record.recordID.recordName"]

那应该会更快。将返回的 key 分解为可以在屏幕上显示的大小,然后只获取它们。显示它们之后,在后台线程中获取下一批,当你在后台获取下一批时,等等。

也许应该补充一点,如果可能的话,获取 Assets 也应该在单独的线程上完成,在您通过重复重新加载表来提取 Assets 时更新表。

这是搜索和返回键的方法。

 func zap(theUUID:String) {
var recordID2Zap: String!
let predicate = NSPredicate(format: "(theUUID = %@)",theUUID)
let query = CKQuery(recordType: "Blah", predicate: predicate)
let searchOperation = CKQueryOperation(query: query)
searchOperation.desiredKeys = ["record.recordID.recordName"]
searchOperation.recordFetchedBlock = { (record) in
recordID2Zap = record.recordID.recordName
}

if error != nil {
print("ting, busted",error!.localizedDescription)
} else {
print("ok zapping")
if recordID2Zap != nil {
self.privateDB.delete(withRecordID: CKRecordID(recordName: recordID2Zap), completionHandler: {recordID, error in
NSLog("OK or \(error)")
})
}
}

}

searchOperation.qualityOfService = .background

privateDB.add(searchOperation)
theApp.isNetworkActivityIndicatorVisible = true
}

}

至于您的表格 View 和图像...使用您的 icloud 代码中的完成功能向表格 View 发送通知。

database.fetchRecordWithID(CKRecordID(recordName: recordId), completionHandler: {record, error in
let directDict = ["blah": "whatever"] as [String : String]
NotificationCenter.default.post(name: Notification.Name("blahDownloaded"), object: nil, userInfo: directDict)
}

然后在 VC 中注册所述通知。

NotificationCenter.default.addObserver(self, selector: #selector(blahDownloaded), name: Notification.Name("blahDownloaded"), object: nil)

func blahDownloaded(notification: NSNotification) {
if let userInfo = notification.userInfo as NSDictionary? as? [String: Any] {

//update you cell
//reload your table
}

这一切都有意义吗?

关于ios - Cloudkit 获取非常慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41325442/

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