will cohen will cohen - 2 months ago 17
iOS Question

Retrieving data with firebase swift bug

@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")
}
}
}


As you can see from my code, I am trying to update firebase with new values when buttons are clicked. What happens, however, when I click these buttons, is that the firebase values in the data base go crazy, going from 1 to 0 every milisecond. I have checked my code multiple times over and just cannot find anything wrong with it, any help is greatly appreciated.

Answer

Observer initialization does not belong in @IBAction handlers.

Once they are set, they remain valid throughout the lifetime of the parent UIViewController, or perhaps the lifetime of the entire App -- check documentation. So you have many observers registered to the event, all duplicates -- an additional duplicate every time the button is clicked.

Observers should be set up at viewDidLoad() and removed at deinit, or conversely at viewWillAppear and removed at viewDidDisappear.

An aside your identifier names are totally cryptic, almost as if they were chosen to deliberately obfuscate. This slows down your own mental processes as well as everyone who sees your code or tries to help.