Skip to content

Instantly share code, notes, and snippets.

@mitnick78
Last active February 5, 2021 21:08
Show Gist options
  • Save mitnick78/c210501566314f780019d4a6ec79847a to your computer and use it in GitHub Desktop.
Save mitnick78/c210501566314f780019d4a6ec79847a to your computer and use it in GitHub Desktop.
//
// ExoModelFirebase.swift
//
import Foundation
import FirebaseDatabase
class ExoModelFirebase: ObservableObject {
var ref = Database.database().reference()
@Published var exos: [ExoModel] = [] {
willSet {
objectWillChange.send()
}
}
var section: String = ""
func getResultSearch(_ section: String){
DispatchQueue.main.async {
self.ref.child("Exercises").queryOrdered(byChild: "pcategory").queryStarting(atValue: section).queryEnding(atValue: section+"\u{f8ff}").observeSingleEvent(of: .value) { (snapshot) in
self.exos.removeAll()
for exo in snapshot.children {
let exoSnapshot = exo as! DataSnapshot
print(exoSnapshot)
let exoArrayData = exoSnapshot.value as? [String:AnyObject]
self.exos.append(ExoModel(pcategory: exoArrayData?["pcategory"] as! String, pdate: exoArrayData?["pdate"] as! String, pheading: exoArrayData?["pheading"] as! String, piconlink: exoArrayData?["piconlink"] as! String, preadcolor: exoArrayData?["preadcolor"] as! String, psimpleblack: exoArrayData?["psimpleblack"] as! String))
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment