I'm switching my app from firebase web (V8) to react-native-firebase. (I figured it would be less work to refactor than going to web V9) But I have run into a problem.
I use Priority values to store a value that indicates ordering of objects in a realtime database container, and when I read the snapshot the priorities are not there. It seems like the top-level snapshot has a priority value, but when I access the child snapshots with snap.child("child1') or snap.forEach(), the child snapshots do not contain a .priority property.
This is the code has been working in my app for over 5 years (with added debugging printouts).
subscribeFB() {
if (!this.unlistenFB) {
this.unlistenFB = this.user.child('Counternames').on(
'value',
(snap) => {
// Need to also grab the current timestamps when
// Counternames changes.
this.getTimestamps(snap);
},
(error) => {
info('Counternames subscription cancelled because', error);
}
);
}
}
getTimestamps(snap) {
this.user.child('Timestamps').once(
'value',
(tsSnap) => {
setTimeout(
() =>
MultiActions.setCounters({
counterSnap: snap,
timestamps: tsSnap.val(),
}),
0
);
},
(error) => {
info('Could not get timestamps because', error);
}
);
}
// Then in setCounters I use both snaps
setCounters(snaps) {
[...]
if ( timestamps.sequence > this.state.timestamps.sequence) {
debug('Getting new sequence from FB');
// get sequence from FB
var CounternameSnap = snaps.counterSnap;
var cns = CounternameSnap.exportVal();
debug(`CounternameSnap exportVal ${JSON.stringify(cns, null, 2)}`);
var ch = CounternameSnap.child('-MH3NnGU5iM0eakrd7ZS').exportVal();
debug(`Counter 44 exportVal ${JSON.stringify(ch)}`);
CounternameSnap.forEach((ctr) => {
var p = ctr.getPriority();
debug(`Counter priority ${p} has key ${ctr.key}, val ${ctr.val()}`);
// Do something with priority...
});
When I change the order of the counters on another device, my .on listener triggers and I get the following printout:
LOG MultiStore:debug Getting new sequence from FB +2ms
LOG MultiStore:debug CounternameSnap exportVal {
".value": {
"-MH3MqLK32gCi0pqBUg3": "Counter 33",
"-MH4yucF8rmeewGbPAVI": "Counter 55",
"-MH3NnGU5iM0eakrd7ZS": "Counter 44",
"-NuBYkNVlAUIUeoQQCKA": "Counter 66",
"-MGQOBLgEvEekOg6geQI": "Counter 11",
"-MGzzZXdUwpBr8RlLLE-": "Counter 22"
},
".priority": null
} +1ms
LOG MultiStore:debug Counter 44 exportVal {".value":"Counter 44"} +1ms
LOG MultiStore:debug Counter priority undefined has key -MGzzZXdUwpBr8RlLLE-, val Counter 22 +2ms
LOG MultiStore:debug Counter priority undefined has key -MH4yucF8rmeewGbPAVI, val Counter 55 +1ms
LOG MultiStore:debug Counter priority undefined has key -MH3NnGU5iM0eakrd7ZS, val Counter 44 +1ms
LOG MultiStore:debug Counter priority undefined has key -MH3MqLK32gCi0pqBUg3, val Counter 33 +0ms
LOG MultiStore:debug Counter priority undefined has key -MGQOBLgEvEekOg6geQI, val Counter 11 +1ms
LOG MultiStore:debug Counter priority undefined has key -NuBYkNVlAUIUeoQQCKA, val Counter 66 +1ms
This shows the top-level snap contains a .priority value (null) but the "Counter 44" child does not have the property at all. I can verify that the other device successfully pushed the new priorities to firebase: although the firebase console does not show priorities, I can export and download the container, and it looks like this:
{
"-MGQOBLgEvEekOg6geQI": {
".value": "Counter 11",
".priority": 4
},
"-MGzzZXdUwpBr8RlLLE-": {
".value": "Counter 22",
".priority": 0
},
"-MH3MqLK32gCi0pqBUg3": {
".value": "Counter 33",
".priority": 1
},
"-MH3NnGU5iM0eakrd7ZS": {
".value": "Counter 44",
".priority": 2
},
"-MH4yucF8rmeewGbPAVI": {
".value": "Counter 55",
".priority": 3
},
"-NuBYkNVlAUIUeoQQCKA": {
".value": "Counter 66",
".priority": 5
}
}
Is this exposing a previously unknown bug in my program, is it a bug in RNFirebase, a change in underlying FB V9+ iOS/Android libraries (It has the same problem on both platforms), or ???
react-native: 0.70.15
react-native-firebase: 19.1.1
[UPDATE:] I wondered if offline persistence might have something to do with this so I enabled persistence and found the problem remains the same either way.