I have an Firebase-backed app with the following database structure:
posts
/uid
/postId
Originally, i'd load data from the posts/uid
node using ObserveEventOfType
with .childAdded
. This would load stale data frequently (~5 times a day) for all users of my app simutaneously. When attempting to update the data by making a new post, Firebase would still return stale data.
As a result, I decided to try keepSynced
. Now, if my reference looked like this:
reference = Database().database.reference.child("posts").child(uid)
keepSynced
would load all of the data at that node, which could amount to very large downloads if there are many children in that node. So, I decided to change the reference/query to:
reference = Database().database.reference.child("posts").child(uid).queryLimited(toLast: 25)
When turning keepSynced
on for this node, it syncs for the last 25 children in the node successfully. However, I still am facing the issue of receiving stale data rather frequently. So here are my questions:
When adding the
keepSynced
mode on the limited query, does it only sync from the initial node you added it to, or does it always just sync the 25 latest children under that node?Where is the best place to add the
keepSynced(true)
line in code? Before we load the reference, inviewWillAppear
, or inside of the actual download callback?Similarly, where is the best place to use
keepSynced(false)
?Do the
keepSynced
listeners delete when the app fades into the background?Why does
keepSynced
sometimes not address for child updates?
I currently use keepSynced(true)
inside of the function I use to load posts which is called on viewDidLoad
.
Thanks in advance.