For clarify and testing purposes, I don’t believe this line of code is valid;
login is asynchronous and has a closure
unless you’re using async/await, which would then be
let user = try await app.login(credentials: creds)
unless you have a some other code involved.
I have no idea if this will help, but here’s our results using your code and roughly 2000 Sync’d Realm objects with two properties per object.
Testing your code execution times produced an opposite and expected result. Reading the data with an existing current user was faster than authenticating and then reading. I pretty much copy and pasted your code - the only difference is we use partition sync, not flex sync.
The first column in the below chart (in seconds) runs when there is no logged in user, so the authentication code must run, which is the code after the
else statement in the question.
The second column is if there’s already an existing user logged in
user = app.currentUser which is the first section of code after the
|No Current User
|runs after ‘else’
||runs after ‘if’
If we change the speed testing to eliminate the delay caused by the authentication code, I ran the test 5 more times and the reading of the data is identical between the first section and second section of code
In summary - the auth code causes the second section of code to be slower, as expected. However, if we remove that from the performance test, it’s identical in speed.
Here’s a disk writing chart. The first section is on app start, no logged in user and the app is idling.
The second section is when the user logs in and reads the data (the code after the
else) and the third section is if the current user exists, which is the first section of code. Total Data written is roughly 1.4 Mb
Summary; we’re not seeing any big difference in disk writes between when a user is already logged in
user = app.currentUser or when they log in fresh. Total amount of written data was roughly 1.4Mb in either case.