What I observe: After an app has been built on iOS; if I log in an out the correct number of loops run. However, if I build the app new with the user still signed in (then it goes automatically to the homepage), way too many snapshot loops get run.
Here is an excerpt of the code:
let refArtists2 = Database.database().reference().child("people").queryOrdered(byChild: "caption").queryStarting(atValue:myInt).queryEnding(atValue: myInt1)
var handle: UInt = 0
handle = refArtists2.observe(DataEventType.value, with: { snapshot in
....
self.query1 = geoFire.query(at: self.dict, withRadius: 500)
self.handle1 = self.query1?.observe(.keyEntered, with: { (key, location) in
})
self.handle2 = self.query1?.observe(.keyExited, with: { key1, location in
})
self.query1?.observeReady({
while let people = enumerator.nextObject() as? DataSnapshot {
if people.key != thisUsersUid && self.componentArray.contains(people.key) {
print(self.componentArray,"man")
DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
if snapshot.exists() && self.people.isEmpty == false {
self.ref.removeObserver(withHandle: handle)
self.ref.removeObserver(withHandle: self.handle1)
self.ref.removeObserver(withHandle: self.handle2)
}
}
}
}
})
The key print to look at is "man". If there are 3 users that get displayed, man is printed 3 times, so loop is done 3 times. However, in the instance where way too many loops are run, it seems man is printed for each iteration of users that are within 500 miles.
The error ended up being related to the while let
loop. Using for in
has fixed it.