What I mean is you are on page A. A button press segues via Navigation Controller to page B. The processes on page A are very intensive and overwhelm page B to the point that it produces wrong UI results (duplicate cells or missing cells)

I have only observed these wrong results twice in about 100 tests. They both happened when I pressed buttons on page A like crazy, a second after loading, then immediately pressing the button to go to page B. I am certain these 2 times happened because page A had to process so much and was not done by the time page B was loaded.

I initially thought I had a race problem on page B, but I don't believe that could be the cause, since this error only happened when page A was extremely overwhelmed. I do not wish to use a skeleton view on page B. Anyway below is the code in page B that determines the users for which cells are to be displayed. The two error where 1) duplicate cells of users and 2) not all users being represented in a cell.

In viewDidLoad:

if CLLocationManager.locationServicesEnabled() {
switch(CLLocationManager.authorizationStatus()) {
case .authorizedAlways, .authorizedWhenInUse:
    print("Access")
    locationManager.delegate = self
    if CLLocationManager.locationServicesEnabled() {
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
        locationManager.startUpdatingLocation()
    }
    let uid = Auth.auth().currentUser!.uid
    let thisUserRef = Database.database().reference().child("user").child(uid)
    let myuserRef = thisUserRef.child("userWhoLike")
    var handle: UInt = 0
    handle = myuserRef.queryLimited(toLast: 30).observe(DataEventType.value,  with: {  snapshot in
        print("kkkkk")
        func decode(autoId: String) -> TimeInterval {
            ...
            return TimeInterval(exactly: timestamp)!
        }
        let uniqueArray1 = snapshot.children.allObjects as! [DataSnapshot]
        let userArray = Array(Set(uniqueArray1))
        for person4 in userArray where uid == Auth.auth().currentUser?.uid   {
            self.dg.enter()
            let personUid = person4.value as! String
            let planDict18 = person4.key
            let time22 = decode(autoId: planDict18)
            let time11 = Int(time22)
            self.dg.leave()
            self.dg.notify(queue: .main) {
                let date = Date(timeIntervalSince1970: TimeInterval(time11)/1000.0)
                if Calendar.current.isDateInToday(date){
                    self.printPersonInfo(uid: personUid)
                }
            }
        }
        DispatchQueue.main.asyncAfter(deadline: .now() + 4) {
            let ref = Database.database().reference().child("user")
            ref.removeObserver(withHandle: handle)
            ref.removeObserver(withHandle: self.handleA)
        }
    })
case .notDetermined:
    print("Location services are not enabled")
 } else {
print("Location services are not enabled")
}
0

There are 0 best solutions below