0

Using firebase and Swift. Is there any way of comparing the current user:

'let uid = Auth.auth().currentUser?.uid'

with the autogenerated id's under 'Users' or 'Advertiser' in the Firebase Structure?

enter image description here

I want to do a If statement and see if the currentUser matches with the Id's under Advertiser or Users.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
olle
  • 134
  • 3
  • 15
  • Keep in mind that we don't know what your data structure means. Are the keys under `/Advertiser` UIDs from Firebase Authentication? And if so, are you trying to load the Advertiser data for a user? Or are you trying to check if a user is an Advertiser? – Frank van Puffelen Oct 12 '18 at 13:36
  • Realized there names might be confusing. All of these IDs are users but some are categorised as Advertisers. Under each ID is profile data for each user registration. The "Advertiser" are essentially users who wants to sell, while the "Users" are customers. So i am trying to check if the current user is an advertiser, and if so the customers("Users") will be displayed, but if the current user is a "Users" then the Advertisers profiles will display. – olle Oct 12 '18 at 15:38
  • OK, so that sounds like you're trying to see if a certain node exists in the database. Should be a fairly simple `Database.database().reference(withPath: "Advertiser").child(uid).observeSingleEvent(of: .value, with: { (snapshot) in if snapshot.exists ...` – Frank van Puffelen Oct 12 '18 at 17:24
  • Thank you Frank this helped me solving this issue! – olle Oct 12 '18 at 18:17

1 Answers1

0

If you're trying to check if a certain node exists in the database, that should be a fairly simple with something like:

Database.database().reference(withPath: "Advertiser").child(uid)
.observeSingleEvent(of: .value, with: { (snapshot) in 
    if snapshot.exists() ...

Also see:

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807