ObservedRealmObject View Counter Lags

I have been playing around with the test application (Flexible Sync) for Swift (realm-template-apps/swiftui at main · mongodb-university/realm-template-apps · GitHub) and found that the UI can be quite laggy. For example below I added a view var to the Item Object to count the number of times the Item has been viewed. But on opening and closing the detailView the UI is very laggy. Is this because the write transaction is happening on the main thread? If so how can I do it on the background in the exact scenario as below? I tried using writeAsync but this did not solve the problem.

TLDR how do I perform tasks on the background while making use of the ObservedRealmObject like shown below?

import SwiftUI
import RealmSwift

/// Show a detail view of a task Item. User can edit the summary or mark the Item complete.
struct ItemDetail: View {
    // This property wrapper observes the Item object and
    // invalidates the view when the Item object changes.
    @ObservedRealmObject var item: Item

    var body: some View {
        Form {
            Section(header: Text("Edit Item Summary")) {
                // Accessing the observed item object lets us update the live object
                // No need to explicitly update the object in a write transaction
                TextField("Summary", text: $item.title)
        .navigationBarTitle("Update Item", displayMode: .inline)
        .onAppear(perform: {
            $item.views.wrappedValue += 1

Can you elaborate a bit on that? I copied and pasted your code and it’s very fluid for me. Tried it in both a macOS App as well as iOS.

Also, it appears the Item object was modified with a views property. Can we see what that object looks like?


Thanks for your reply :slight_smile: , yeah I added some properties this is what it looks like now:

class Item: Object, ObjectKeyIdentifiable {

@Persisted(primaryKey: true ) var _id: ObjectId
@Persisted var title: String
@Persisted var picture: Data
@Persisted var about: String
@Persisted var price: Double
@Persisted var condition: String
@Persisted var createdAt = NSDate().timeIntervalSince1970
@Persisted var favorites: MutableSet<String>
@Persisted var likes = 0
@Persisted var views = 0
// :state-start: flexible-sync
@Persisted var owner_id: String
// :state-end:

I am not too familiar yet, but the UI freezes/lags on every transaction that’s happening. I also have this like button on the items in the rowView and when I tap it it increments the ‘like’ properties with 1, but it also freezes the UI for around 0.5-1 second. Same thing happens when I tap to go to the detailView where the view increment happens onAppear and also when I go back to the listView. Even just scrolling through the list can be quite laggy. Opening the tab where the itemsView is displayed also causes the whole UI to freeze up for around 1.5 seconds before it actually switches tabs to the itemsView.

I was also wondering if it’s possible to get the loading state of the ObservedResults collection. So I can show a progressView while the collection is loading.

So basically my problem here is that every transaction just freezes up the UI and I think should be moved to a background thread, but I am not sure how :smiling_face_with_tear:. I have tried stuff with writeAsync and read the whole documentation but I have not seen any cases where they do this with ObservedResults/ObservedRealmObjects.

I am working with your code and your object in a small test project and I am just not able to duplicate the issue. Honestly, this code

$item.views.wrappedValue += 1

is such a tiny amount of data, even if it was being written on the main UI thread, you’d likely never notice it. That being said, it appears you’re using @ObservedRealmObject, and connecting asynchronously so you’re already on a background thread with those writes. Right?