isuru
isuru

Reputation: 3565

FIRDataEventTypeChildChanged not working for offline?

In my iOS app I have use firebase observeEventType:FIRDataEventTypeChildChanged to get realtime updates for particular node. However while user goes to offline the node update several times. But when user comes to online user cannot get previous updates. Is there a solution for this?

Upvotes: 0

Views: 115

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598728

The Firebase Realtime Database synchronizes state. It does (explicitly) not synchronize all intermediate state changes.

If there were multiple changes to the same node while your client was offline, it will actually only see the final state.

Client1              Database               Client2
                                            Start listener
write 1     -->         1           -->        1
write 2     -->         2           -->        2
                                            Go offline
write 3     -->         3                      
write 4     -->         4                      
                                            Go online
                                    -->        4
write 5     -->         5           -->        5

So in the above diagram, client 2 will only see values 1, 2 and 5. It may see value 4 too, depending on precisely when value 5 is written. But it will definitely not see value 3.

If you want each state change to be communicated to client 2 when it reconnects, you should store the exact state changes in your database. So instead of storing the resulting value, you would store the operation. Something like:

Changes: {
  "-PushId1": { value: 1  },
  "-PushId2": { value: 2  },
  "-PushId3": { value: 3  },
  "-PushId4": { value: 4  },
  "-PushId5": { value: 5  }
}

Now if your client listens to /Changes, it is guaranteed to see all the changes that happened.

Upvotes: 3

Related Questions