opened
spandana-batchu
username_0
### [READ] Step 1: Are you in the right place?

  Yes

### [REQUIRED] Step 2: Describe your environment

  * Xcode version: 8.3
  * Firebase SDK version: 3.1
  * Library version: 
  * Firebase Product:  database

### [REQUIRED] Step 3: Describe the problem

We are facing the below issue on our iOS app that uses firebase iOS SDk. 
To give you some context, the users of our app are part of Groups. Each of these groups have some group information. 
In the database we have user who is mapped with a list of group IDs. Using these Group Ids Further we fetch group’s info. Below is the structure. 
```
+User1
    +GroupID1
        +timestamp
    +GroupID2
        +timestamp
+GroupID1
    +groupName:
    +groupCount:……
    +timestamp
```
Whenever there is a change in group’s info, we have a firebase cloud fuction that updates the timestamp at both the places. Hence any change to group’s info would notify the observers at both references `database\GroupID1` and `database\User1`
The issue we are facing is that we have a continuous observer at reference `database\User1` using the method observe event type. 
Changes to group info is notifying that continuous observer as expected. After which we are fetching group info using observe single event method. But that group info returned is the cached info. 
The info returned at reference `database\GroupID1` is always one step old data. 
Is there anything we are missing here. 

#### Relevant Code:
 observer at reference `database\User1` : 

`observe(_ eventType: .value, with block: @escaping (FIRDataSnapshot) -> Swift.Void, withCancel cancelBlock: ((Error) -> Swift.Void)? = nil) -> UInt`
 
 observer at reference `database\ GroupID1 ` : 

`observeSingleEvent(of eventType: .value, andPreviousSiblingKeyWith block: @escaping (FIRDataSnapshot, String?) -> Swift.Void)`
Observe Single event method returning cached data 
issue
closed
schmidt-sebastian
username_1
issue
created
schmidt-sebastian
username_1
Thanks for sending over this issue. While I understand that this can be frustrating, this is expected behavior. Single even observers will return data from the cache if there is data available. This allows us to fire them immediately. If you use .value observers, you will receive updated data in its second call.
comment
created
cbess
username_2
I suppose this makes sense. But, is there a way to do have it pull from the server immediately/initially, without having to call it twice?

Also, I would say the [documentation](https://firebase.google.com/docs/reference/ios/firebasedatabase/api/reference/Classes/FIRDatabaseQuery#-observesingleeventoftypewithblock) should be updated.
comment
created
MarksCode
username_3
Any update?
comment
created
paulb777
username_4
@username_3 What kind of update are you looking for? This issue was closed as "expected behavior". [stackoverflow](https://stackoverflow.com/questions/tagged/firebase) is a  better place to ask usage questions.
comment
created
quanvo87
username_5
Is there a better way of doing this? I've noticed that when using a cache and attempting a `singleEvent` query, it can take 55-60 attempts before fresh data is returned.
comment
created
morganchen12
username_6
@username_5 load time is going to depend on the size of the data you're loading and the quality of your internet connection. Are you observing one minute load times for small queries on good internet?
comment
created
quanvo87
username_5
@username_6 When testing this, I was querying an endpoint that returned a small String. My connection is 15 ping 50/10 down/up. I could probably be more clear--by 55-60 attempts, what I mean is I did a `ref.observeSingleEvent`, and in the callback, if `!snapshot.exists()`, print something and repeat the function, until it finally got back data. The print statement executed 50+ times each test run. This is with persistence enabled. With persistence disabled, it just fires once.
comment
created
morganchen12
username_6
Oh, I misread your last comment.

With the current API, observing a single event with persistence enabled doesn't let you specify whether you're trying to retrieve cached or server data. The SDK defaults to returning cached data, but this isn't useful if you're trying to retrieve server data. For the time being you should use value listeners instead like Sebastian mentioned above, but this does seem like a non-feature.
comment
created
Rahish
username_7
Why in the Second call? As we expect refreshed data whenever we apply the observer to the Database Reference. I tried below code too, its also not refreshing the data
weak var weakSelf = self
            valueRef = dbRef?.observe(.value) { (snapShotData) in
                
                if singleValueObserver
                {
                    weakSelf?.dbRef?.removeObserver(withHandle: weakSelf?.valueRef ?? 0)
                }
                weakSelf?.dataFetched(snapShotData)
            }
comment