I'm building a trivia game.
After the user successfully completes a Level, I calling a server request that opens the next Level. In addition to opening the next Level, I calling one more server request that updates the user's score.
Do you have a recommendation on how to optimize it, so that it takes less time, maybe take out one server request that will update the two fields?
I use Firebase and this how it looks inside realtime database:
Here some code:
If the answer is correct :
const onAnswerPressedHandler = async answer => {
const rightAnswer = answer === selectedLevel.rightAnswer;
dispatch(setShowLoader(true));
if (rightAnswer) {
if (isNotLastLevel) {
await updateRank();
await unlockNextLevelIfExist();
} else if (isNotLastWorld) {
await updateRank();
await unlockNextWorldIfExist();
} else {
await updateRank();
navigation.pop(3);
}
}
dispatch(setShowLoader(false));
};
Updating rank in firebase:
export const updateUserScoreAndRank = (score, rank) => new Promise(async (resolve, reject) => {
try {
await database()
.ref(firebaseRefs.USERS_REF)
.child(auth().currentUser.uid)
.child(firebaseRefs.CURRENT_USER_DETAILS)
.child('score')
.set(score);
await database()
.ref(firebaseRefs.USERS_REF)
.child(auth().currentUser.uid)
.child(firebaseRefs.CURRENT_USER_DETAILS)
.child('rank')
.set(rank);
resolve();
} catch (e) {
reject(e);
console.log(e);
}
});
Unlocking next Level:
export const setLevelPassed = (worldIndex, levelIndex) => new Promise(async (resolve, reject) => {
try {
await database()
.ref(firebaseRefs.USERS_REF)
.child(auth().currentUser.uid)
.child(firebaseRefs.CURRENT_USER_GAME_DETAILS)
.child(firebaseRefs.CURRENT_USER_GAME_DATA)
.child(worldIndex.toString())
.child(firebaseRefs.GAME_QUESTIONS)
.child(levelIndex.toString())
.child(firebaseRefs.IS_LOCKED)
.set(false);
resolve();
} catch (e) {
reject(e);
console.log('setLevelPassed', e);
}
});
Any idea how to improve the requests?