1

For some reason when i add the code if user.uid == uid {return} it will return out of the function altogether and not add any users to the array even though i only want to filter out the current user from being added to the array?

func fireStoreFetchUsers(){
    guard let uid = Auth.auth().currentUser?.uid else { return }
    let db = Firestore.firestore()

    db.collection("Users")
       .getDocuments() { (querySnapshot, err) in

          if let err = err {
            print("Error getting documents: \(err)")
          } else {
            for document in querySnapshot!.documents {
                let dictionary = document.data()
                let user = User2(uid: "", distanceFrom: "any", dictionary: dictionary)

                if user.uid == uid {return}

                self.users.append(user)               
                self.collectionView?.reloadData()
            }
        }
    }
}
1
  • That's what return means. It sounds like you want to continue. Commented Jan 17, 2018 at 13:07

1 Answer 1

1

It seems that your problem in summary is that you want to skip a specific element of the array you are iterating through. This is exactly what continue does, so just change return to continue.

After the above change, the last two lines of the loop won't be executed when user.uid == uid and hence that specific user won't be added to the users array and the collectionView won't be reloaded, but the loop will continue its iteration with the next element rather than returning from the function.

for document in querySnapshot!.documents {
    let dictionary = document.data()
    let user = User2(uid: "", distanceFrom: "any", dictionary: dictionary)

    if user.uid == uid {continue}

    self.users.append(user)               
    self.collectionView?.reloadData()
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks David works as you know, has this always been like this as i swear the return code worked before i updated to the new firestore database?
Yes, as far as I know, there was no change in the behaviour of Control Transfer Statements since Swift was introduced.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.