所以我有一个表,从我的数据库中获取一些数据并将其显示在他的单元格中。我做了一个刷新按钮,它可以重新加载我的代码,这样当你点击它时,用户就能看到别人添加到数据库中的新条目。
我的刷新按钮的代码是这样的:
@IBAction func refreshButton(sender: AnyObject) {
textArray.removeAllObjects()
let predicate = NSPredicate(value: true)
let query = CKQuery(recordType: "Conversation", predicate: predicate)
query.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: true)]
publicCloudDatabase?.performQuery(query, inZoneWithID: nil,
completionHandler: ({results, error in
if (error != nil) {
dispatch_async(dispatch_get_main_queue()) {
self.notifyUser("Cloud Access Error",
message: error.localizedDescription)
}
} else {
if results.count > 0 {
var record = results[0] as! CKRecord
self.currentRecord = record
dispatch_async(dispatch_get_main_queue()) {
for x in results{
self.textArray.addObject(x.objectForKey("message") as! String)
self.tableView.reloadData()
}
}
} else {
dispatch_async(dispatch_get_main_queue()) {
self.notifyUser("No Match Found",
message: "No record matching the address was found")
}
}
}
}))
}
我想删除该按钮,使表刷新每30秒(在应用程序的后台-我不希望用户知道它),这是可能的吗?如果是,怎么做?
谢谢!
使用计时器是一种方法,在ViewDidload
self.myTimer = NSTimer(timeInterval: 30.0, target: self, selector: "refresh", userInfo: nil, repeats: true)
NSRunLoop.mainRunLoop().addTimer(self.myTimer, forMode: NSDefaultRunLoopMode)
func refresh() {
// a refresh of the table
}
1。perform每30秒查询一次。
2.比较新旧数据的差异。3。使用这个Api
插入不同的部分- (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(uitableviewwrowanimation)动画
而不是reloadData