每次我获取快照然后将新的“项目”添加到firebase数据库时,我只返回一个项目而不是全部三个项目

时间:2016-06-14 23:51:26

标签: ios swift uitableview firebase firebase-realtime-database

I only get back "CourseName instead of all the others"

Structure of my Firebase Database

所以我在模拟器中启动应用程序,然后快照在viewdidappear内部关闭,然后所有预期的内容都显示在consolole中......但在@IBActionwhich广告之后将一个项目( - > CourseName,AddDate,AmountOfHoles)发送到firebase我只在控制台中获取CourseName(AddedDate和AmonutOfHole是nil)但是在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)

}

}

1 个答案:

答案 0 :(得分:0)

由于每次新的子项出现在/Course/CourseRef.observeEventType(FIRDataEventType.ChildAdded)时都要检索数据,因此在调用第一个.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)