Search code examples
iosfirebasesetvalue

Firebase iOS - guarantees on FDataSnapshot when calling observeSingleOfEventType


I would like clarification on what Firebase guarantees about the FDataSnapshot returned from observeSingleEventOfType. In particular, the iOS docs state:

Value events are always triggered last and are guaranteed to contain updates from any other events which occurred before that snapshot was taken.

Does this also guarantee that updates occurring AFTER observeSingleEventOfType is called are NOT included?

For example in the following code:

Firebase *ref = [[Firebase alloc] initWithUrl:@"someUrl"];
[ref setValue:@{@"someKey1" : @"someValue1"}];
[ref observeSingleEventOfType:FEventTypeValue 
    withBlock:^(FDataSnapshot *snapshot) {
        // is it ever possible that snapshot will contain someKey2?
}
[ref setValue:@{@"someKey2" : @"someValue2"}];

I'm wondering if it's ever possible for the snapshot to contain "someKey2," or does the snapshot only include updates that occurred before observeSingleEventOfType was called?


Solution

  • Calling setValue() is not an event; you're confused here. An event is something you can pass into observe[Single]Event*(), and they are enumerated here.

    Firebase is not synchronous and it won't wait for your write to complete. However, if you listen for ChildAdded and observeSingleEventOfType() of value, then all the child keys in the "snapshot" will have already triggered ChildAdded events.

    If you want to wait for the write to complete, use withCompletionBlock:

    [ref setValue:@"I'm writing data" withCompletionBlock:^(NSError *error, Firebase *ref) {
        // start your listeners here
    }];