从 Firebase 完成加载后如何使 UIRefreshControl 结束刷新
How to make UIRefreshControl end refreshing when finish loading from Firebase
下面的方法是我刷新数据的方法。它将从 firebase 读取以获取最新数据。因为firebase是不同步的所以我在endRefreshing之前等了5秒等待Firebase完成读取过程。
func refreshData() {
//Remove old data
self.items.removeAll()
//Renew all data
var ref: DatabaseReference!
ref = Database.database().reference(withPath: "tasks")
//Loading local drafts
var drafts : [Task]!
if let local_drafts = NSKeyedUnarchiver.unarchiveObject(withFile: Task.ArchiveURL.path) as? [Task] {
drafts = local_drafts
}
else{
drafts = []
}
//Reloading the database
ref.observe(.value, with: { snapshot in
var newItems: [Task] = []
self.num_of_tasks = Int(snapshot.childrenCount)
for item in snapshot.children {
//let local = item as! DataSnapshot
//let snapshotValue = local.value as! [String: AnyObject]
//print(snapshotValue["main_content"] as! String!)
let taskItem = Task(snapshot: item as! DataSnapshot)
newItems.append(taskItem!)
}
let merged = drafts + newItems
self.items = merged
self.tableView.reloadData()
//Wait for 5 seconds
let deadlineTime = DispatchTime.now() + .seconds(3)
DispatchQueue.main.asyncAfter(deadline: deadlineTime, execute: {
self.refreshControl!.endRefreshing()
})
})
}
但这不是一个很好的解决方案,因为有时所有数据都已加载但刷新仍在继续,这很奇怪。有没有办法解决这种情况?让 refreshControl 结束刷新,直到我从 firebase 加载所有数据?
下面是一种方法。它会等到异步数据下载完成,然后当 completionHandler return 为 true 或 false 时,refreshControl 将停止刷新。我这里没有编译器,所以我希望它能正常工作。
func viewDidLoad(){
refreshData{ _ in
self.refreshControl!.endRefreshing()
}
}
func refreshData(completionHandler:@escaping (Bool)->() ) {
//Remove old data
self.items.removeAll()
//Renew all data
var ref: DatabaseReference!
ref = Database.database().reference(withPath: "tasks")
//Loading local drafts
var drafts : [Task]!
if let local_drafts = NSKeyedUnarchiver.unarchiveObject(withFile: Task.ArchiveURL.path) as? [Task] {
drafts = local_drafts
}
else{
drafts = []
}
//Reloading the database
ref.observe(.value, with: { snapshot in
var newItems: [Task] = []
self.num_of_tasks = Int(snapshot.childrenCount)
for item in snapshot.children {
//let local = item as! DataSnapshot
//let snapshotValue = local.value as! [String: AnyObject]
//print(snapshotValue["main_content"] as! String!)
let taskItem = Task(snapshot: item as! DataSnapshot)
newItems.append(taskItem!)
}
let merged = drafts + newItems
self.items = merged
self.tableView.reloadData()
completionHandler(true)
})
}
下面的方法是我刷新数据的方法。它将从 firebase 读取以获取最新数据。因为firebase是不同步的所以我在endRefreshing之前等了5秒等待Firebase完成读取过程。
func refreshData() {
//Remove old data
self.items.removeAll()
//Renew all data
var ref: DatabaseReference!
ref = Database.database().reference(withPath: "tasks")
//Loading local drafts
var drafts : [Task]!
if let local_drafts = NSKeyedUnarchiver.unarchiveObject(withFile: Task.ArchiveURL.path) as? [Task] {
drafts = local_drafts
}
else{
drafts = []
}
//Reloading the database
ref.observe(.value, with: { snapshot in
var newItems: [Task] = []
self.num_of_tasks = Int(snapshot.childrenCount)
for item in snapshot.children {
//let local = item as! DataSnapshot
//let snapshotValue = local.value as! [String: AnyObject]
//print(snapshotValue["main_content"] as! String!)
let taskItem = Task(snapshot: item as! DataSnapshot)
newItems.append(taskItem!)
}
let merged = drafts + newItems
self.items = merged
self.tableView.reloadData()
//Wait for 5 seconds
let deadlineTime = DispatchTime.now() + .seconds(3)
DispatchQueue.main.asyncAfter(deadline: deadlineTime, execute: {
self.refreshControl!.endRefreshing()
})
})
}
但这不是一个很好的解决方案,因为有时所有数据都已加载但刷新仍在继续,这很奇怪。有没有办法解决这种情况?让 refreshControl 结束刷新,直到我从 firebase 加载所有数据?
下面是一种方法。它会等到异步数据下载完成,然后当 completionHandler return 为 true 或 false 时,refreshControl 将停止刷新。我这里没有编译器,所以我希望它能正常工作。
func viewDidLoad(){
refreshData{ _ in
self.refreshControl!.endRefreshing()
}
}
func refreshData(completionHandler:@escaping (Bool)->() ) {
//Remove old data
self.items.removeAll()
//Renew all data
var ref: DatabaseReference!
ref = Database.database().reference(withPath: "tasks")
//Loading local drafts
var drafts : [Task]!
if let local_drafts = NSKeyedUnarchiver.unarchiveObject(withFile: Task.ArchiveURL.path) as? [Task] {
drafts = local_drafts
}
else{
drafts = []
}
//Reloading the database
ref.observe(.value, with: { snapshot in
var newItems: [Task] = []
self.num_of_tasks = Int(snapshot.childrenCount)
for item in snapshot.children {
//let local = item as! DataSnapshot
//let snapshotValue = local.value as! [String: AnyObject]
//print(snapshotValue["main_content"] as! String!)
let taskItem = Task(snapshot: item as! DataSnapshot)
newItems.append(taskItem!)
}
let merged = drafts + newItems
self.items = merged
self.tableView.reloadData()
completionHandler(true)
})
}