0

The app returns an empty snapshot when the logs in; however, if they click the reload button after logging in, it returns the correct snapshot. The same function downloadInitialUsers() is called when the view is loaded upon logging in, and when clicking the reload button. I am confused why the same query would return an empty snapshot at first, but then work as expected when clicking the reload button if its the same query! So when downloadUsersFromFirebase is called upon loading the view after logging in, my query returns an empty snapshot; but, when downloadUsersFromFirebase after clikcing reload, the query does NOT return an empty snapshot.

 override func viewDidLoad() { print("did load") super.viewDidLoad() emptyDataView.delegate = self downloadInitialUsers() setup() } 
private func downloadInitialUsers() { ProgressHUD.show() FirebaseListener.shared.downloadUsersFromFirebase(isInitialLoad: isInitialLoad, limit: initialLoadNumber, lastDocumentSnapshot: lastDocumentSnapshot) { (allUsers, snapshot) in if allUsers.count == 0 { ProgressHUD.dismiss() } self.lastDocumentSnapshot = snapshot self.isInitialLoad = false self.initialCardModels = [] self.userObjects = allUsers let allUsersShuffle = allUsers.shuffled() for user in allUsersShuffle { print(user) user.getUserAvatarFromFirestore { (didSet) in let cardModel = UserCardModel(id: user.objectId, name: user.username) self.initialCardModels.append(cardModel) self.numberOfCardsChecked += 1 if self.numberOfCardsChecked == allUsers.count { DispatchQueue.main.async { ProgressHUD.dismiss() self.layoutCardStackView() } } } } print("initial \(allUsers.count) received") self.downloadMoreUsersInBackground() } } 
func downloadUsersFromFirebase(isInitialLoad: Bool, limit: Int, lastDocumentSnapshot: DocumentSnapshot?, completion: @escaping (_ users: [FUser], _ snapshot: DocumentSnapshot?) ->Void) { var query: Query! var users: [FUser] = [] let user = Auth.auth().currentUser!.uid if isInitialLoad { query = FirebaseReference(.User).whereField(kTYPE, isEqualTo: "female").limit(to: limit) print("first \(limit) users loading") } else { if lastDocumentSnapshot != nil { query = FirebaseReference(.User).whereField(kTYPE, isEqualTo: "female").limit(to: limit).start(afterDocument: lastDocumentSnapshot!) print("next \(limit) user loading") } else { print("last snapshot is nil") } } if query != nil { guard let snapshot = snapShot else { return } if !snapshot.isEmpty { for userData in snapshot.documents { let userObject = userData.data() as NSDictionary users.append(FUser(_dictionary: userObject)) } users.shuffle() completion(users, snapshot.documents.last!) } else { print("no more users to fetch") users.shuffle() completion(users, nil) } } } else { users.shuffle() completion(users, nil) } } 
func didClickReloadButton() { let currentUser = FUser.currentUser() currentUser?.seenIdArray = [] saveUserData(user: currentUser!) resetLoadCount() downloadInitialUsers() emptyDataView.reloadButton.isEnabled = false } 
  • Tried to catch an error in the query (no errors)
  • Tried changing the query to load all users, same issue happened
5
  • Likely a timing issue you are calling asynchronous functions and expecting them to work synchronously. Look into async await fire ase has provided Concurrent alternatives to most of its functions and it will work similarly to what you are expecting. Commented Mar 14, 2023 at 1:39
  • Could you give an example of such a function ? Commented Mar 14, 2023 at 6:55
  • stackoverflow.com/questions/73636543/… Commented Mar 14, 2023 at 16:32
  • What happens when you move the database call from viewDidLoad to viewWillAppear? If nothing changes, what happens when you move it to viewDidAppear? Commented Mar 15, 2023 at 0:16
  • it finally worked, I just had to add a completion block when logging in Commented Mar 15, 2023 at 15:55

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.