每次我获取快照然后将新的 "item" 添加到 firebase 数据库时,我只取回一项而不是全部三项
Every time I get the snapshot and then add a new "item" to the firebase database i only get back one item instead of all three
所以我在模拟器中启动应用程序,然后快照在 viewdidappear 中关闭,然后控制台中显示了预期的一切......但是在 @IBAction 之后,它向一个项目(->CourseName、AddDate、AmountOfHoles)添加了广告firebase 我只在控制台中获得 CourseName(AddedDate 和 AmonutOfHole 为零)但是在 firebase 控制台中一切都显示出来......?
我现在想知道如何从快照中无误地获取数据(AddedDate、CourseName...)并将其添加到我的 tableview...?
import UIKit
import Firebase
class CoursesViewController: UITableViewController {
var ref = FIRDatabaseReference.init()
override func viewDidLoad() {
ref = FIRDatabase.database().reference()
}
//TODO prevent crash when nil
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
var CourseRef = ref.child("Course")
//Get Snapshot from Course containing all children
var refhandlr = CourseRef.observeEventType(FIRDataEventType.ChildAdded, withBlock: { (snapshot) in
let CourseName = snapshot.value!["CourseName"] as! String
let AmonutOfHoles = snapshot.value!["AmountOfHoles"] as! String
let AddedDate = snapshot.value!["AddedDate"] as! String
print(snapshot.value)
}, withCancelBlock: { error in
})
}
@IBAction func addButtonDidTouch(sender: AnyObject) {
// Alert View for input
let alert = UIAlertController(title: "Course Item",message: "Add Course",preferredStyle: .Alert)
let saveAction = UIAlertAction(title: "Save", style: .Default) { (action: UIAlertAction) -> Void in
//Get Date String
let date = NSDate()
print(date)
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "dd-MM-yyyy"
let dateString = dateFormatter.stringFromDate(date)
print(dateString)
let courseField = alert.textFields![0]
let holesField = alert.textFields![1]
let Course = self.ref.child("Course").childByAutoId()
let amountOfHolesRef = Course.child("AmountOfHoles")
let courseNameRef = Course.child("CourseName")
let dateRef = Course.child("AddedDate")
courseNameRef.setValue(courseField.text)
amountOfHolesRef.setValue(holesField.text)
dateRef.setValue(dateString)
}
//Cancel
let cancelAction = UIAlertAction(title: "Cancel", style: .Default) { (action: UIAlertAction) -> Void in
}
//TextField placeholder in alert
alert.addTextFieldWithConfigurationHandler {
(courseField: UITextField!) -> Void in
courseField.placeholder = "Course Name"
}
alert.addTextFieldWithConfigurationHandler {
(holesField: UITextField!) -> Void in
holesField.placeholder = "Holes (6/9/18)"
}
//Add alert
alert.addAction(saveAction)
alert.addAction(cancelAction)
presentViewController(alert, animated: true, completion: nil)
}
}
由于每次 /Course/
(CourseRef.observeEventType(FIRDataEventType.ChildAdded
) 上出现新的 child 时您都在检索数据,因此在调用第一个 .setValue(courseField.text)
时,您的代码会触发事件侦听器并仅使用 CourseName
.
记录条目
因此请确保在添加新的 Course
时只调用 setValue()
一次。
您的代码将如下所示:
let Course = self.ref.child("Course").childByAutoId()
let course = ["AddedDate": dateString,
"CourseName": courseField.text,
"AmountOfHoles": holesField.text]
Course.setValue(course)
所以我在模拟器中启动应用程序,然后快照在 viewdidappear 中关闭,然后控制台中显示了预期的一切......但是在 @IBAction 之后,它向一个项目(->CourseName、AddDate、AmountOfHoles)添加了广告firebase 我只在控制台中获得 CourseName(AddedDate 和 AmonutOfHole 为零)但是在 firebase 控制台中一切都显示出来......? 我现在想知道如何从快照中无误地获取数据(AddedDate、CourseName...)并将其添加到我的 tableview...?
import UIKit
import Firebase
class CoursesViewController: UITableViewController {
var ref = FIRDatabaseReference.init()
override func viewDidLoad() {
ref = FIRDatabase.database().reference()
}
//TODO prevent crash when nil
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
var CourseRef = ref.child("Course")
//Get Snapshot from Course containing all children
var refhandlr = CourseRef.observeEventType(FIRDataEventType.ChildAdded, withBlock: { (snapshot) in
let CourseName = snapshot.value!["CourseName"] as! String
let AmonutOfHoles = snapshot.value!["AmountOfHoles"] as! String
let AddedDate = snapshot.value!["AddedDate"] as! String
print(snapshot.value)
}, withCancelBlock: { error in
})
}
@IBAction func addButtonDidTouch(sender: AnyObject) {
// Alert View for input
let alert = UIAlertController(title: "Course Item",message: "Add Course",preferredStyle: .Alert)
let saveAction = UIAlertAction(title: "Save", style: .Default) { (action: UIAlertAction) -> Void in
//Get Date String
let date = NSDate()
print(date)
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "dd-MM-yyyy"
let dateString = dateFormatter.stringFromDate(date)
print(dateString)
let courseField = alert.textFields![0]
let holesField = alert.textFields![1]
let Course = self.ref.child("Course").childByAutoId()
let amountOfHolesRef = Course.child("AmountOfHoles")
let courseNameRef = Course.child("CourseName")
let dateRef = Course.child("AddedDate")
courseNameRef.setValue(courseField.text)
amountOfHolesRef.setValue(holesField.text)
dateRef.setValue(dateString)
}
//Cancel
let cancelAction = UIAlertAction(title: "Cancel", style: .Default) { (action: UIAlertAction) -> Void in
}
//TextField placeholder in alert
alert.addTextFieldWithConfigurationHandler {
(courseField: UITextField!) -> Void in
courseField.placeholder = "Course Name"
}
alert.addTextFieldWithConfigurationHandler {
(holesField: UITextField!) -> Void in
holesField.placeholder = "Holes (6/9/18)"
}
//Add alert
alert.addAction(saveAction)
alert.addAction(cancelAction)
presentViewController(alert, animated: true, completion: nil)
}
}
由于每次 /Course/
(CourseRef.observeEventType(FIRDataEventType.ChildAdded
) 上出现新的 child 时您都在检索数据,因此在调用第一个 .setValue(courseField.text)
时,您的代码会触发事件侦听器并仅使用 CourseName
.
因此请确保在添加新的 Course
时只调用 setValue()
一次。
您的代码将如下所示:
let Course = self.ref.child("Course").childByAutoId()
let course = ["AddedDate": dateString,
"CourseName": courseField.text,
"AmountOfHoles": holesField.text]
Course.setValue(course)