Here is my ViewModel code which grabs data from firebase firestore:
gettingData = true
//read docs at path templeu
print("<-- Getting restaurants from \(selectedCollege) database -->")
db.collection(selectedCollege).addSnapshotListener({ [self] querySnapshot, error in
guard let documents = querySnapshot?.documents else {
print("no documents")
return
}
self.restaurantList = documents.map { (queryDocumentSnapshot) -> Categories in
let data = queryDocumentSnapshot.data()
return Categories(
id: data["id"] as? String ?? UUID().uuidString,
name: data["name"] as? String ?? "[Name]",
type: data["type"] as? [String] ?? ["None"],
pic: data["pic"] as? String ?? "",
price: data["price"] as? String ?? "",
hours: data["hours"] as? [String:[String]] ?? ["Thursday": ["0.00", "0.00"]],
hoursString: data["hoursString"] as? String ?? "",
stars: data["stars"] as? String ?? "",
website: data["website"] as? String ?? "",
location: data["location"] as? String ?? "",
coordinates: data["coordinates"] as? [Double] ?? [0.0, 0.0],
phoneNumber: data["phoneNumber"] as? String ?? "1112223456",
currency: data["currency"] as? String ?? "[Currency]",
review: data["review"] as? [String] ?? ["Review"],
wait: data["wait"] as? String ?? "[Wait]",
campus: data["campus"] as? String ?? "[Campus]",
featured: data["featured"] as? Bool ?? false,
forceClose: data["forceClose"] as? Bool ?? false
)
}
self.moodList = self.restaurantList
print("<-- Done Getting Data -->")
self.gettingData = false
})
Currently, when I update something in the database, the user is forced out of whatever screen they were on as the data updates in my views. How do I detect when there is a change to my database, then notify the user of this and prompt them to refresh the data with a button that appears in a view?
You can make a closure and call it in viewDidLoad() function and when there is any change in Firestore, you can show the user an alert message if he wants to reload the interface or not.
show example:
class HomePageViewController: UIViewController {
var restaurantList: [Category] = []
var gettingData: Bool = true
override func viewDidLoad() {
super.viewDidLoad()
self.setListnerCollectionOfCategories() { categories in
let alert = UIAlertController(title: "New Data!!", message: "Are you sure want to refresh data?", preferredStyle: .alert)
let ok = UIAlertAction(title: "Yes", style:
UIAlertAction.Style.default) {
UIAlertAction in
self.restaurantList = categories
self.gettingData = true
//MARK:- reload data
}
let cancel = UIAlertAction(title: "Cancel", style:
UIAlertAction.Style.cancel) {
UIAlertAction in
self.gettingData = true
}
alert.addAction(ok)
alert.addAction(cancel)
self.present(alert, animated: true, completion: nil)
}
}
func setListnerCollectionOfCategories(completion: @escaping(([Category])->(Void))) {
//read docs at path templeu
print("<-- Getting restaurants from \(selectedCollege) database -->")
db.collection(selectedCollege).addSnapshotListener({ [self] querySnapshot, error in
guard let documents = querySnapshot?.documents else {
print("no documents")
return
}
var categories = [Category] = []
categories = documents.map { (queryDocumentSnapshot) -> Categories in
let data = queryDocumentSnapshot.data()
return Categories(
id: data["id"] as? String ?? UUID().uuidString,
name: data["name"] as? String ?? "[Name]",
type: data["type"] as? [String] ?? ["None"],
pic: data["pic"] as? String ?? "",
price: data["price"] as? String ?? "",
hours: data["hours"] as? [String:[String]] ?? ["Thursday": ["0.00", "0.00"]],
hoursString: data["hoursString"] as? String ?? "",
stars: data["stars"] as? String ?? "",
website: data["website"] as? String ?? "",
location: data["location"] as? String ?? "",
coordinates: data["coordinates"] as? [Double] ?? [0.0, 0.0],
phoneNumber: data["phoneNumber"] as? String ?? "1112223456",
currency: data["currency"] as? String ?? "[Currency]",
review: data["review"] as? [String] ?? ["Review"],
wait: data["wait"] as? String ?? "[Wait]",
campus: data["campus"] as? String ?? "[Campus]",
featured: data["featured"] as? Bool ?? false,
forceClose: data["forceClose"] as? Bool ?? false
)
}
print("<-- Done Getting Data -->")
self.gettingData = false
completion(categories)
})
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.