使用Firebase Swift错误检索数据

时间:2016-10-05 22:04:49

标签: ios swift firebase firebase-realtime-database

@IBAction func one_2(sender: AnyObject) {
    if un_1.textColor == UIColor.blueColor() {
        if aac == false {


            var aa = [FIRDataSnapshot]()
            DataService.ds.REF_ISSUELIKES.observeEventType(.Value, withBlock: { (snapshot) in
                if let snapshots = snapshot.children.allObjects as? [FIRDataSnapshot] {
                    for snap in snapshots {
                        aa.append(snap)
                    }
                    print(aa[0])

                    var aaUpvotes = String()
                    var aaDownvotes = String()
                    let a = Array(String(aa[0]).characters)
                    if a.count == 55 {
                        aaUpvotes = String(a[51])
                        aaDownvotes = String(a[34])
                    } else if a.count == 57 {
                        aaDownvotes = String("\(a[34])\(a[35])")
                        aaUpvotes = String("\(a[52])\(a[53])")
                    } else if a.count == 59 {
                        aaDownvotes = String("\(a[34])\(a[35])\(a[36])")
                        aaUpvotes = String("\(a[53])\(a[54])\(a[55])")
                    }

                    self.ref.child("IssueLikes").child("Abortion").child("Upvotes").setValue(Int(aaUpvotes)! + 1)
                    self.ref.child("IssueLikes").child("Abortion").child("Downvotes").setValue(Int(aaDownvotes)! - 1)
                }
            })

            aab = false
            un_1.text = String(Int(un_1.text!)!-1)
            un_2.text = String(Int(un_2.text!)!+1)
            un_1.textColor = UIColor.whiteColor()
            un_2.textColor = UIColor.blueColor()
            aac = true

        } else {
            print("AAC")
        }
    } else if un_1.textColor == UIColor.whiteColor() {
        if aac == false {


            var aa = [FIRDataSnapshot]()
            DataService.ds.REF_ISSUELIKES.observeEventType(.Value, withBlock: { (snapshot) in
                if let snapshots = snapshot.children.allObjects as? [FIRDataSnapshot] {
                    for snap in snapshots {
                        aa.append(snap)
                    }
                    print(aa[0])

                    var aaUpvotes = String()
                    var aaDownvotes = String()
                    let a = Array(String(aa[0]).characters)
                    if a.count == 55 {
                        aaUpvotes = String(a[51])
                        aaDownvotes = String(a[34])
                    } else if a.count == 57 {
                        aaDownvotes = String("\(a[34])\(a[35])")
                        aaUpvotes = String("\(a[52])\(a[53])")
                    } else if a.count == 59 {
                        aaDownvotes = String("\(a[34])\(a[35])\(a[36])")
                        aaUpvotes = String("\(a[53])\(a[54])\(a[55])")
                    }

                    self.ref.child("IssueLikes").child("Abortion").child("Upvotes").setValue(Int(aaUpvotes)! + 1)
                }
            })

            aab = false
            un_2.textColor = UIColor.blueColor()
            un_2.text = String(Int(un_2.text!)!+1)
            aac = true

        } else {
            print("AAC")
        }
    } else {
        if aac == false {


            var aa = [FIRDataSnapshot]()
            DataService.ds.REF_ISSUELIKES.observeEventType(.Value, withBlock: { (snapshot) in
                if let snapshots = snapshot.children.allObjects as? [FIRDataSnapshot] {
                    for snap in snapshots {
                        aa.append(snap)
                    }
                    print(aa[0])

                    var aaUpvotes = String()
                    var aaDownvotes = String()
                    let a = Array(String(aa[0]).characters)
                    if a.count == 55 {
                        aaUpvotes = String(a[51])
                        aaDownvotes = String(a[34])
                    } else if a.count == 57 {
                        aaDownvotes = String("\(a[34])\(a[35])")
                        aaUpvotes = String("\(a[52])\(a[53])")
                    } else if a.count == 59 {
                        aaDownvotes = String("\(a[34])\(a[35])\(a[36])")
                        aaUpvotes = String("\(a[53])\(a[54])\(a[55])")
                    }

                    self.ref.child("IssueLikes").child("Abortion").child("Upvotes").setValue(Int(aaUpvotes)! + 1)
                }
            })

            aab = false
            un_1.textColor = UIColor.whiteColor()
            un_2.textColor = UIColor.whiteColor()
            un_2.text = String(Int(un_2.text!)!+1)
            un_2.textColor = UIColor.blueColor()
            aac = true

        } else {
            print("AAC")
        }
    }
}

从我的代码中可以看出,我在点击按钮时尝试使用新值更新Firebase。但是,当我单击这些按钮时,数据库中的Firebase值会变得疯狂,每毫秒从1变为0。

我已多次检查我的代码,但发现它没有任何问题。非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

观察者初始化不属于@IBAction处理程序。

设置完成后,它们在父UIViewController的整个生命周期内保持有效,或者可能是整个应用程序检查文档的生命周期。因此,您有许多观察者注册到该事件,所有重复 - 每次单击按钮时都会重复。

观察员应设置在viewDidLoad(),并在deinit处删除,或相反地在viewWillAppear处删除,并在viewDidDisappear处删除。

除此之外,您的标识符名称是完全神秘的,几乎就像他们被选择故意混淆一样。这会减慢您自己的心理过程以及每个看到您的代码或试图提供帮助的人。