2016-10-05 181 views
0
@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。使用Firebase Swift檢索數據錯誤

我檢查了我的代碼多次,只是找不到任何錯誤。任何幫助是極大的讚賞。

回答

0

觀察者初始化不屬於@IBAction處理程序。

一旦它們被設置,它們在父UIViewController的整個生命週期內保持有效,或者整個應用程序的生命週期 - 檢查文檔。因此,您有許多觀察員註冊了該活動,所有重複項 - 每次單擊該按鈕時都會有一次重複。

觀察員應設置在viewDidLoad()並在deinit處移除,或者相反在viewWillAppear處移除並在viewDidDisappear處移除。

除了你的標識符名稱是完全神祕的,就好像它們被選擇故意混淆一樣。這會減慢你自己的心理過程,以及每個看到你的代碼或試圖提供幫助的人。

+0

啊,這有助於immensly。關於這些神祕的標識符名稱,它是一個很大的應用程序,如果你說出整件事情,它實際上是一種有組織的方式。非常感謝。 –