Using LinkingObjects with OutlineGroup in SwiftUI

I’m trying to create an OutlineGroup in SwiftUI to display an hierarchal folder like structure:

class Folder : Object, ObjectKeyIdentifiable {
    @Persisted(primaryKey: true) var _id: ObjectId
    @Persisted var name: String
    @Persisted var parent: Folder?
    @Persisted var icon = "folder"
    @Persisted(originProperty: "parent") var children: LinkingObjects<Folder>
    @Persisted(originProperty: "folder") var items: LinkingObjects<Item>
}

I have an @ObservedResults property in my view:

@ObservedResults(Folder.self, filter: NSPredicate(format: "parent == nil")) var rootFolders

And in my view:

    var body: some View {
        NavigationView {
            OutlineGroup(rootFolders[0], children: \.children) { folder in
                Text("\(folder.name)")
            }
            Text("Content")
        }
    }

This code though doesn’t compile, I’m getting the error “Key path value type ‘LinkingObjects’ cannot be converted to contextual type ‘LinkingObjects?’”
for the \.children keypath.

I’m kind of at a loss as to how to resolve this to get the OutlineGroup to get the item’s children.
I’m not quite sure even how to “unwrap” the children property into an [Folder]?.

Any assistance is appreciated.

Thanks,
Michael

Not sure if this is idiomatic or even a good idea but it did get me past the compile error and a bit further down the road.

    var maybeChildren: LinkingObjects<Folder>? {
        get {
            if children.count > 0 {
                return children
            } else {
                return nil
            }
        }
    }

Would the forward link be a List and the inverse be a LinkingObjects?

class Folder : Object, ObjectKeyIdentifiable {
    @Persisted(primaryKey: true) var _id: ObjectId
    @Persisted var name: String
    @Persisted var parent: Folder?
    @Persisted var icon = "folder"
 
    @Persisted var childList = List<Folder>() //forward link to the child folders
    @Persisted(originProperty: "childList" var linkedParent: LinkingObjects<Folder> //back link to parent
}

That being said, since the child folder can only ever have a single parent, what about forgoing the LinkingObjects and just use a single parent property?

class Folder : Object, ObjectKeyIdentifiable {
    @Persisted(primaryKey: true) var _id: ObjectId
    @Persisted var name: String
    @Persisted var parent: Folder?
    @Persisted var icon = "folder"
 
    @Persisted var childList = List<Folder>() //forward link to the child folders
    @Persisted var parentFolder: Folder!
}

It’s a bit more work but that keeps the relationships one-to-many and then the reverse is one-to-one.

In my case, I worked around this by adding a computed property:

extension Folder {
   var childrenArray: [Folder] {
        children.count == 0 ? nil : Array(children)
    }
}

I have same issue, resolved by this

import RealmSwift

class Item: Object, ObjectKeyIdentifiable {
  @Persisted(primaryKey: true) var id: ObjectId
  @Persisted var name = ""
  @Persisted var subItems = RealmSwift.List<Item>()
}

extension Item {
   var subItemArray: [Item]?  {
     subItems.count == 0 ? nil : Array(subItems)
    }
}

struct ContentView: View {
  @Environment(\.realm) var realm
  @ObservedResults(Item.self) var items

  var body: some View {
      List {
        ForEach(items) { item in
          OutlineGroup(item, children: \.subItemArray) { sItem in
              Text(sItem.name)
          }
        }
      }
  }
}


struct ContentView_Previews: PreviewProvider {
  static var previews: some View {
    ContentView()
  }
}