So if I change it to .observe(DataEventType.value?, the problem does not occur.
What is the problem: If I open the app for the 2nd time or later after installing, it fails to fetch updated firebase data when using .observeSingleEvent. For example a new user added or a user who changed his profile picture. This problem does not occur when I change it to .observe(DataEventType.value?. It also gets resolved if I delete the app and run it again with .observeSingleEvent. That leads me to believe it is a cache issue that cause the 2nd run after install to behave differently to the first install.
I use keychain (not user defaults) for user info. Below is the code, and in bold the line where changing to .observe(DataEventType takes away the problem. Part 1 is where reload is done. Part 2 is a snapshot that the important part 3 is in, but can otherwise be disregarded. Part 3 is where the problem is.
////Part 1
func printPersonInfo(uid: String) {
DispatchQueue.main.async{
print(uid)
self.table.reloadData()
}
}
///Part 2
override func viewDidLoad() {
super.viewDidLoad()
guard let myUid = Auth.auth().currentUser?.uid else { return }
refArtists = Database.database().reference().child("people").child(myUid).child("e2")
refArtists.observeSingleEvent(of:.value, with: {snapshot in
let myDomain = snapshot.value as? String
self.bSnap = myDomain
let peopleRef = Database.database().reference().child("people")
let thisPersonRef = peopleRef.child(myUid).child("e2")
thisPersonRef.observeSingleEvent(of:.value, with: {snapshot in
if snapshot.exists() {
........
print(self.array1, "ahah")
})
///Part 3
let thisUsersUid = Auth.auth().currentUser?.uid //Mr. Sam's uid
self.refArtists = Database.database().reference().child("people");
**self.refArtists.observeSingleEvent(of: .value, with: { [weak self]snapshot in**
print("test test testa")
guard let self = self else { return }
if snapshot.childrenCount>0{
print("test test testb")
self.people.removeAll()
for people in snapshot.children.allObjects as! [DataSnapshot] {
if people.key != thisUsersUid {
print("peoplekey",people.key)
let peopleObject = people.value as? [String: AnyObject]
let peoplePhotoPosts = peopleObject?["PhotoPosts"] as? String
let peopleimageDownloadURL = peopleObject?["imageDownloadURL"] as? String
let peoplepostID = peopleObject?["postID"] as? String
let peopleCoordinates = peopleObject?["Coordinates"] as? String
let peoplecaption = peopleObject?["caption"] as? Int
let userId = people.key
let coordSnap = people.childSnapshot(forPath: "Coordinates")
guard let lat = coordSnap.childSnapshot(forPath: "latitude").value as? CLLocationDegrees else { return /* put here something if your method does return some value */ }
guard let lon = coordSnap.childSnapshot(forPath: "longitude").value as? CLLocationDegrees else { return /* put here something if your method does return some value */ }
let locCoord = CLLocation(latitude: lat, longitude: lon)
print(userId, "coords: \(lat) \(lon)")
print(peoplePhotoPosts as Any, "plpl")
print(peopleimageDownloadURL as Any, "dwl")
print("v", self.dict)
let coordSnap12 = people.childSnapshot(forPath: "caption").value as? Int ?? 0
let date = Date(timeIntervalSince1970: TimeInterval(coordSnap12)/1000.0)
//let secondsInDay = 86400
if Calendar.current.isDateInToday(date) {
let distance = locCoord.distance(from: self.dict)
print(distance, "distancexy")
if distance/1609.344 < 3000 && self.array1.contains(people.key){
print(self.array1, "f111111")
print("fee", self.dict )
print(distance, "distancexr")
let peopl = Userx( PhotoPosts: peoplePhotoPosts, imageDownloadURL: peopleimageDownloadURL,........)
self.people.append(peopl)
let d = people.key as! String
self.printPersonInfo(uid:d)
} else {
print ("w")
}
} else {
print ("alphaaa")
}
}
print("aaaaaaaa", self.people.map {$0.distance})
}
self.people.sort { ($0.distance ?? 0) < ($1.distance ?? 0) }
}
})
}
}