Access EmbeddedObject with RealmSwift in SwiftUI

I have the following object and embedded object in my code:

final class Customer: Object, ObjectKeyIdentifiable {
    @Persisted(primaryKey: true) var _id: ObjectId
    @Persisted var nameFirst = ""
    @Persisted var nameLast = ""
    @Persisted var active = true
    @Persisted var email = ""
    @Persisted var phone = ""
    @Persisted var notes = ""
    @Persisted var address: Address?
    @Persisted(originProperty: "customers") var custGroup: LinkingObjects<CustomerGroup>

final class Address: EmbeddedObject {
    @Persisted var city: String?
    @Persisted var postalCode: String?
    @Persisted var provinceCode: String?
    @Persisted var street1: String?

Then in my view object, I attempt to access the embedded object like so:

import SwiftUI
import RealmSwift

struct CustomerForm: View {

    @ObservedRealmObject var customer: Customer

    var body: some View {
    TextField("Street", text: $customer.address.street1)

Which produces the error: Failed to produce diagnostic for expression; please submit a bug report ( and include the project

Somewhat new to swift and realm but the error strikes me as odd.


1 Like

Old thread, but wanted to post solution here. Just worked thru the same issue with a realm embedded object, and found out that this is more of a swift problem than a realm one.

According to this, swift does not like binding nested observableobjects.

The best solution is to create a separate, small view just to give the UI elements for the nested object, and pass only the nested object to it. In this way, the separate view only appears to have one level of object, and things work.

In my case, I have a parent object with a nested embedded object containing optional data. I was planning on having one edit view for the parent & embedded data combined. However, I ended up having a parent_edit view and an embedded_edit view. Seems harmless enough and was only a tiny bit of boiler plate extra.

In the parent view:

    @ObservedRealmObject var parentData: type


                 TextField("displayName", text: $parentData.displayName)
            ........  other parent data fields

                embedded_edit(embedded: parentData.nestedObj )

In the embedded_edit view:

struct embedded_edit: View {
    @State var embedded: embeddedType
                        TextField("displayName", text: $embedded.displayName)