如何显示 CloudKit 最接近今天的日期 post?
How can I show the post closest to today's date from CloudKit?
我正在使用 CloudKit 开发一个有趣的社交媒体应用程序,但我一直卡在首先显示最近发布的图片的问题上。
我在 CloudKit 上设置了记录类型以发布图片和发布日期,该日期以编程方式设置为 iPhone 发布的日期。
这是我打开应用程序时显示图像的代码:
override func viewDidAppear(animated: Bool) {
let container = CKContainer.defaultContainer()
let publicDatabase = container.publicCloudDatabase
let predicate = NSPredicate(value: true)
let query = CKQuery(recordType: "Post", predicate: predicate)
publicDatabase.performQuery(query, inZoneWithID: nil) { results, error in
if error == nil { // There is no error
results
print("No Error")
if results!.count > 0 {
print("Found some")
self.newResults = results!
let randomNumber = arc4random_uniform(UInt32(results!.count - 1)) + 0
let randomNumberInt = Int(randomNumber)
let record = results![randomNumberInt]
let img = record.valueForKey("Picture") as? CKAsset
let image = UIImage(contentsOfFile: img!.fileURL.path!)
self.imageView.image = image
dispatch_async(dispatch_get_main_queue(), { () -> Void in
})
}
}
else {
print(error)
}
}
}
目前,此代码仅显示 results
数组中的随机图像。但我想以某种方式对结果进行排序并使用 DatePosted
CloudKit 记录类型 属性 显示最近发布的图像。我完全不知道如何做到这一点,所以非常感谢任何帮助!
此代码向您展示了基础知识。它不是最新的,它是最旧的,但我敢肯定你能弄明白:) 利用 swift 的一篇优秀且非常有趣的文章作为你可以在这里阅读的日期。
func cleanup4Cloud() {
let container = CKContainer(identifier: "iCloud.ch")
let publicDB = container.publicCloudDatabase
let predicate = NSPredicate(value: true)
let query = CKQuery(recordType: "Blah", predicate: predicate)
query.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
cleanUpOperation = CKQueryOperation(query: query)
cleanUpOperation.desiredKeys = ["record.recordID.recordName"];
cleanUpOperation.recordFetchedBlock = { (record) in
self.recordsRead.append(record.recordID)
let recordDOC = record["creationDate"] as! NSDate
let newTimeInterval = 4.hours.fromNow
let formatter = NSDateFormatter()
formatter.dateStyle = .NoStyle
formatter.timeStyle = .MediumStyle
let string = formatter.stringFromDate(newTimeInterval)
let string2 = formatter.stringFromDate(recordDOC)
if newTimeInterval.timeIntervalSinceReferenceDate > recordDOC.timeIntervalSinceReferenceDate {
print("recordDOC is older than \(string2) [\(string)]")
}
}
cleanUpOperation.queryCompletionBlock = {(cursor, error) in
if error != nil {
print("ting, busted",error!.localizedDescription)
} else {
print("self.recordsRead.count \(self.recordsRead.count)")
self.cleanOutDB()
}
}
cleanUpOperation.qualityOfService = .Background
publicDB.addOperation(cleanUpOperation)
}
我正在使用 CloudKit 开发一个有趣的社交媒体应用程序,但我一直卡在首先显示最近发布的图片的问题上。
我在 CloudKit 上设置了记录类型以发布图片和发布日期,该日期以编程方式设置为 iPhone 发布的日期。
这是我打开应用程序时显示图像的代码:
override func viewDidAppear(animated: Bool) {
let container = CKContainer.defaultContainer()
let publicDatabase = container.publicCloudDatabase
let predicate = NSPredicate(value: true)
let query = CKQuery(recordType: "Post", predicate: predicate)
publicDatabase.performQuery(query, inZoneWithID: nil) { results, error in
if error == nil { // There is no error
results
print("No Error")
if results!.count > 0 {
print("Found some")
self.newResults = results!
let randomNumber = arc4random_uniform(UInt32(results!.count - 1)) + 0
let randomNumberInt = Int(randomNumber)
let record = results![randomNumberInt]
let img = record.valueForKey("Picture") as? CKAsset
let image = UIImage(contentsOfFile: img!.fileURL.path!)
self.imageView.image = image
dispatch_async(dispatch_get_main_queue(), { () -> Void in
})
}
}
else {
print(error)
}
}
}
目前,此代码仅显示 results
数组中的随机图像。但我想以某种方式对结果进行排序并使用 DatePosted
CloudKit 记录类型 属性 显示最近发布的图像。我完全不知道如何做到这一点,所以非常感谢任何帮助!
此代码向您展示了基础知识。它不是最新的,它是最旧的,但我敢肯定你能弄明白:) 利用 swift 的一篇优秀且非常有趣的文章作为你可以在这里阅读的日期。
func cleanup4Cloud() {
let container = CKContainer(identifier: "iCloud.ch")
let publicDB = container.publicCloudDatabase
let predicate = NSPredicate(value: true)
let query = CKQuery(recordType: "Blah", predicate: predicate)
query.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
cleanUpOperation = CKQueryOperation(query: query)
cleanUpOperation.desiredKeys = ["record.recordID.recordName"];
cleanUpOperation.recordFetchedBlock = { (record) in
self.recordsRead.append(record.recordID)
let recordDOC = record["creationDate"] as! NSDate
let newTimeInterval = 4.hours.fromNow
let formatter = NSDateFormatter()
formatter.dateStyle = .NoStyle
formatter.timeStyle = .MediumStyle
let string = formatter.stringFromDate(newTimeInterval)
let string2 = formatter.stringFromDate(recordDOC)
if newTimeInterval.timeIntervalSinceReferenceDate > recordDOC.timeIntervalSinceReferenceDate {
print("recordDOC is older than \(string2) [\(string)]")
}
}
cleanUpOperation.queryCompletionBlock = {(cursor, error) in
if error != nil {
print("ting, busted",error!.localizedDescription)
} else {
print("self.recordsRead.count \(self.recordsRead.count)")
self.cleanOutDB()
}
}
cleanUpOperation.qualityOfService = .Background
publicDB.addOperation(cleanUpOperation)
}