How to group data from realmDB

I user RealmDB in my swift ios project. I have table with users which has online status flag. On my GUI I have UITableView with sections splited on online/offline rows.
I need to make correct request to RealmDB to take users list and grouped by online flag. And same moment I want to use Result.observe closure for make update in UI.
But can’t find correct query request. for it.
Currently I solve this issue with 2 separated requests to database. But its make change async in same table and broke data consistent for UI, and my app will crash.
Please help me to understand how to use this realmDB in real iOs projects.
Before it, I used YapDatabase and this case solved very easily.

@Maxim_Bunkov Are you able to share more code snippets so we can understand what is going on?

I have UITableView with 2 separated sections of users.
1 section - online users
2 section - offline users
When I try to make two separated realmdb request like that:

    conversationsResult = dbUsers.filter{$0.isOnline}

    conversationsNotificationToken = conversationsResult?.observe { [weak output] changes in
      guard let output = output else { return }
      output.receivedConversationsDataChanges(changes)
    }

For users who is online and for user who is offline

    conversationsOfflineResult = dbUsers.filter{!$0.isOnline}

    conversationsNotificationTokenOffline = conversationsResultOffline?.observe { [weak output] changes in
      guard let output = output else { return }
      output.receivedConversationsDataChangesOffline(changes)
    }

Each of changes change only specific section by change example method from documentation:

  func applyChanges<T>(changes: RealmCollectionChange<T>, section: Int = 0) {
    switch changes {
    case .initial:
      reloadSections([section], with: .none)
    case .update(_, let deletions, let insertions, let updates):
      let fromRow = {(row: Int) in
        return IndexPath(row: row, section: section)}

      beginUpdates()
      deleteRows(at: deletions.map(fromRow), with: .automatic)
      insertRows(at: insertions.map(fromRow), with: .automatic)
      reloadRows(at: updates.map(fromRow), with: .none)
      endUpdates()
    default: break
    }
  }

But when user change status from offline to online or vice verse both sections update async and my app crashed. Because data array is wrong.
Currently I use reloadData instead method for update whole talbeView. But its ugly solution.
Help find correct solution for update UITableView/UICollectionView with multiply section based on Results of Realm db class.

@Maxim_Bunkov Thank you - can I also see your schema of the object in question as well as the queries you are using to propagate data to the UI?

It’s a typical object, nothing new:

class SLRConversation: Object {
  override public static func primaryKey() -> String? {
    return "topic"
  }

  override public static func ignoredProperties() -> [String] {
    return ["groupName__", "pingInterval"]
  }

  @objc dynamic var createdAt: Date?
  @objc dynamic var descUpdatedAt: Date?
  @objc dynamic var lastMessageAt: Date?
  @objc dynamic var subUpdatedAt: Date = Date(milliseconds: 0)
  @objc dynamic var isOnline: Bool = true
  @objc dynamic fileprivate var meta__: Data?
  @objc dynamic var conversationIcon: String?
  ...
}

I didn’t see how the scheme can help to you. For an example, how works group in SQL like group by in YapDB here:https://github.com/yapstudios/YapDatabase/wiki/Views
It’s what I need. With changes triggering. Which will be moved offline user to online in one change event.

@Maxim_Bunkov It looks like you are trying to use live objects to fill table cells’ data. It would probably be a good thing to use frozen objects as a table’s data source. I would try using our new Frozen Objects which we talk about in this blog post -
https://www.mongodb.com/article/realm-cocoa-swiftui-combine

Also documented here -
https://docs.mongodb.com/realm/ios/threading/index.html#frozen-objects

@Ian_Ward looks like what I need. So if user changed status from online to offline for example. It will be only one rx signal which we froze and reload sections?

And one more question. Didn’t prepare data array changes from the box? For sections in UITableView? Users should controll the data flow? I mean when indexPath[0][12] moved to indexPath[1][3].