0

I want to get a user corresponding to an event

I have a list of Events

let eventsRef = FIRDatabase.database().reference.child("Events") 

I have a list of Users

let usersRef = FIRDatabase.database().reference.child("Users") 

My Event and User model is as below

  • Events
    • Event1
      • eventHost: user1_uid
    • Event2
      • eventHost: user2_uid
  • Users
    • User1
      • email: email1
    • User2
      • email: email2

The following callback (in the Event model) is never invoked:

if let userKey = eventData["eventHost"] as? String { userRef = usersRef.child(userKey) userRef.observeSingleEvent(of: .value, with: { snapshot in ... }) } 

I can confirm that I have not enabled disk persistence and that user uid is available. Is there anything I am doing obviously wrong?

====== EDIT: The simplified event model

import Foundation import Firebase class Event { // event metadata private var _eventId: String! private var _eventHost: User! var eventId: String { return _eventId } var eventHost: User { return _eventHost } init(eventId: String, eventData: Dictionary<String, Any>) { self._eventId = eventId if let userKey = eventData["eventHost"] as? String { let usersRef = FIRDatabase.database().reference().child("Users") let userRef = usersRef.child(userKey) print(userRef) userRef.observeSingleEvent(of: .value, with: { (snapshot) in print("USERY: \(snapshot.key)") if let userDict = snapshot.value! as? Dictionary<String, Any> { let id = snapshot.key self._eventHost = User(userId: id, userData: userDict) } }) } } } 

The print(userRef) resolves to

https://xxxx.firebaseio.com/Users/AFHpS3npOga4tfj10GS2HGeT9uJ3` 

which is a valid object in my Firebase structure. Snippet of Firebase User structure

 "AFHpS3npOga4tfj10GS2HGeT9uJ3" : { "email" : "[email protected]", "firstName" : "Wilma", "lastName" : "Flintstone", "profileImageUrl" : "http://images.iimg.in/c/569f4771c45d324bda8b4660-4-501-0-1453279096/google/user-icon-png-pnglogocom.img", "provider" : "Firebase", "userId" : "AFHpS3npOga4tfj10GS2HGeT9uJ3" }, 
16
  • I want to help, but I don't see anything that is obviously wrong. I assume when you say the callback is never invoked, you put a breakpoint in it? Commented May 9, 2017 at 16:12
  • @JenPerson Yes I put a breakpoint in, and it does not get there Commented May 9, 2017 at 16:15
  • If you print something in your observe function, will that execute? Commented May 9, 2017 at 16:15
  • @J.Doe No, it is not going in at all. I do see it print once in a while, but have been unable to put a finger on how to get it to do consistently Commented May 9, 2017 at 16:19
  • More information is needed: Does the if let userKey = code get called? If you put a breakpoint at self._eventId and manually step through the code, one line at a time, on what line does it stop working? Commented May 9, 2017 at 17:27

1 Answer 1

1

can you take a look at the rules of your firebase database :

It should be something like this

{ "rules": { ".read": "auth != null", ".write": "auth != null" } } 

in order to let non authenticated people read / write data you should do the following :

{ "rules": { ".read": true, ".write": true } } 

From the code you post I don't see any problem ... If this doesn't work please post more code and tell us what you plan to do exactly so we can better help you.

Edit: To go along with the above suggestion, adding a cancel block to the observe function will reveal if there's a rule issue. If the user cannot access the node, the Xcode console will print 'Permission Denied'

userRef.observeSingleEvent(of: .value, with: { (snapshot) in print("USERY: \(snapshot.key)") if let userDict = snapshot.value! as? Dictionary<String, Any> { let id = snapshot.key self._eventHost = User(userId: id, userData: userDict) } }, withCancel: { error in print(error.localizedDescription) }) 
Sign up to request clarification or add additional context in comments.

3 Comments

Yes, it is set to let non authenticated people read/write data. I have edited the post to include the relevant/simplified parts of the event model. The complete code is posted here
your code is correct , could you try to add keepSynced(true) and execute again ?
I believe that is only when you have persistence enabled? In either case, adding it does not help

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.