Use ObservedRealmObject property in ObservedResults query

I have this piece of code which sorts all the messages by date of a certain conversation which allows me to show users per day an overview of the messages that has been sent. This all works perfect, but maybe you already noticed that I hard coded in one of the conversation ID’s to test if it works. Now my problem is that I get this error when I try to use instead of the hardcoded id: conversation._id

This is the error: Cannot use instance member ‘conversation’ within property initializer; property initializers run before ‘self’ is available

I know about initializing and have been struggling with it for quite some time, but I just don’t get it to work for this scenario where I want to use an property of an ObservedRealmObject in an ObservedResults query.

Any help is greatly appreciated!

struct ConversationDetail: View {
    @Environment(\.presentationMode) var presentationMode
    @ObservedRealmObject var conversation: Conversation
    
    @ObservedResults(Message.self, where: ({ $0.conversation == "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXX"}), sortDescriptor: SortDescriptor(keyPath: "timestamp", ascending: true)) var messages
    
    var groupedByDate: [String: [Message]] {
        Dictionary(grouping: messages, by: {$0.timestamp.toLocalTime(format: "EEEE")})
    }
    var headers: [String] {
        groupedByDate.map({ $0.key }).sorted()
    }

    var body: some View { MyView }

hey @Jesse_van_der_Voorn Are you using the Realm SDKs with Swift or is this just a general question on Swift? We do have a SwiftUI guide here for Realm -

If you are using Realm then likely you’ll get a better answer on our Realm forums over here -