SwiftUI Slow Loading Views

I have an app entirely in SwiftUI with Realm and Realm Sync.

There are 2 ways that users interact with Realm partitions, one just for their user

"user=\(app.currentUser!.id)"

and one for the public realms that they are accessing

"publicRealm=\(someUID)"

As my app has become more complex, I have to continually switch back and forth, so I have these environment variables being passed in all over the place:

.environment(\.realmConfiguration, app.currentUser!.configuration(partitionValue: "publicRealm=\(someUID)"))

In one LazyVStack there may be a few hundred of these and it’s causing the view to take up to 20 seconds to load, even when nothing has changed and from what I assume, all of the data should be loading from the local realm at that point (for instance, viewing the list and then switching to another view and switching back).

Is there a better best practice that doesn’t cause the app to come to a crawl when we get beyond the simple examples?

Hoping this isn’t another issue of “Realm is great, just won’t scale well” and just a poor implementation on my part.

Hi @Kurt_Libby1 looking at your use case, each realm (user id and public realm) should be downloaded the first time you are syncing and then syncing updates should be effortless (unless there is a big amount of data syncing). As you correctly assume data is loading from the local realm and should be quick to load the data to the view.
Can you share the code of the view you are trying to show?, maybe I can reproduce your use case and find a solution to your issue.

Hey @Diana_Maria_Perez_Af.

It would be way too complex.

I was trying to figure out what I added that made it go so slow, but I couldn’t find anything.

SO… what I did was roll back from Realm Swift 10.22.0 back to 10.21.0 and that fixed it. I did some experimenting and 10.21.1 is the first version that introduces the incredibly slow load time for these views.

Not sure what I’m doing was either deprecated/introduced/broken in the updates to Realm, but just going back to 10.19.0 made all the view loading instantaneous again.

Hope this helps anyone else that runs into this issue.

Hey @Kurt_Libby1 we’ve been checking SwiftUI performance and created a branch with changes that may help to get better UI experience using Realm+SwiftUI.
If you don’t mind, would you test this branch (dp/fix_swiftui_performance_issues) from our repository with your project and check if you still have slow loading views?

Hey Diana,

I checked and it’s still loading super slow. :pleading_face:

Thanks for the help, I’ll let you know if we make any progress on this.

Are there any updates already on this? In our app we use SwiftUI and have noticed very slow loading times when accessing a view and passing .environment(…) and receiving with @ObservedResults(Example.self) var examples

The loading time is proportional to how many elements there are in examples, so for just a few the loading time is almost instant, but at about 20 it’s already a few full seconds

I’m continuing to use 10.21.0 and lower.

The major issue is not being able to try out Flexible Sync, but I’m waiting until they fix the loading time before using any newer versions.

I’m not great with github issues so I didn’t create one. I’ve searched in the issues for anything mentioning this and haven’t seen one. Maybe adding an issue will help get more eyes on it?

Rolling back to 10.21.0 completely fixed the issue for us as well. Thanks!! Just created an issue here

1 Like

@David_Kessler @Kurt_Libby1 we’ve been able to identify the issue and we are working and trying to find a fix for it.

1 Like

I have the same problem and can confirm that rolling back to 10.21.0 fixes it. In my case, slow performance occurs when presenting and closing a SwiftUI sheet view (for adding or editing a realm object).

I just updated to 10.23.0 but the issue is still there.

Hi @horatiu_anghel, I have a branch with a possible fix for this GitHub - realm/realm-swift at dp/fix_swiftui_performance_issues, if you can tests if this branch fix the issue, it will help us to make sure this works for your particular use case.

@Kurt_Libby1 I know this is the second time I ask, but can you test this branch GitHub - realm/realm-swift at dp/fix_swiftui_performance_issues and let me know if you still have performance issues?

Hi Diana,

I tested the provided branch but, in my case, the issue is still present.

Horatiu

I just raised and issue on Github about this as we have also run into major performance issues when we upgraded from 10.29.0 to 10.24.2.

Some fairly complex reports we generate usually take between 110 seconds and 300 seconds but with 10.24.2 they are taking hours to run, many hours. But they do run.

One thing to note here is that when using 10.24.2 in local mode with the exact same realm we don’t see the performance problem. This only occurs with Synced realms.

Happy to help debug this if necessary.

I tried using 10.21.0 but I was unable to compile with Xcode 13.3 using SPM.

Can you confirm you are able to build with Xcode 13.3 and the 10.21.0 SPM package ?

Thanks

Hi @Duncan_Groenewald I saw your post on GitHub and thanks for sharing the info which will really help us to diagnose this, I opened a new issue with a compilation of all SwiftUI performance related issues (SwiftUI Performance issues · Issue #7740 · realm/realm-swift · GitHub), which we are working on. I’ll post any update to this there.

I tried this branch and have the same performance issue

@Diana_Maria_Perez_Af

It looks to me like this performance issue might’ve been introduced in 10.21.1. There are several issues where that seems to be the case. See Slow Performance in Writing after updated to 10.24.2 · Issue #7734 · realm/realm-swift · GitHub

And as mentioned in this conversation:

I have the same problem and can confirm that rolling back to 10.21.0 fixes it.

Can you confirm there is still working being done to address this issue ? I got a note from @Thomas_Goyne re 10.27.0 being a partial fix but it is not really addressing the query performance hit which appears to be related to the fact that running multiple queries on separate threads causes severe blocking of the separate threads. This is not happening on the older version (10.21.0 or earlier) and nor does it happen with a non-synced realm.
I am attaching the CPU performance graphs showing the stark difference between them.