Struggling with Realm Data Structure on SwiftUI app

I am new to data persistence on SwiftUI and have decided to give Realm a go. So far I am able to create a simple to do list app with CRUD operations, but I’m struggling to scale it up to manage more complex data sets.

I am building a small writing app that needs to allow the user to create multiple stories step by step. Each story should have its own array of characters, plots, locations, etc.

So basically, I’ve created a Story class that refers to other classes (Realm objects):

class Story: Object, Identifiable {
    @objc dynamic var id : Date = Date()
    @objc dynamic var warmUp: WarmUp = WarmUp()
    @objc dynamic var settings: Settings = Settings()
    @objc dynamic var characters = StoryCharacter()
    @objc dynamic var world: World = World()
    @objc dynamic var locations = Location()
    @objc dynamic var plots = Plot()
    @objc dynamic var scenes = StoryScene()
    @objc dynamic var ideas = Idea()
}

But I am unable to figure out how to store an array of objects without making the app constantly crash.

I’ve tried:

List<Object>(), RealmSwift.List<Object>(), [Object](), etc

But all of those make my app crash whenever I’m trying to build it.

Any ideas? + Am I taking the right approach according to you regarding how I’m planning to store my app’s data?

Thanks for the help guys!

A List object is defined like this

let myList = List<YourRealmObject>()

A List object is not an array - while that have similar interfaces, there are some fundamental differences as well.

For example a List can be used to created a relationship and LinkingObjects object can be used to create an inverse relationship. List objects are also live updating - so if an element in the list is updated (say, by another user), the List will reflect that update

Hi @Charlie_Marechal – note that there’s been an improvement in syntax options (Realm-Cocoa 10.10), I’d declare your class like this:

class Story: Object, ObjectKeyIdentifiable {
    @Persisted var id = Date()
    @Persisted var warmUp = WarmUp()
    @Persisted var settings = Settings()
    @Persisted var characters = List<StoryCharacter>()
    @Persisted var world = World()
    @Persisted var locations = List<Location>()
    @Persisted var plots = List<Plot>()
    @Persisted var scenes = List<StoryScene>()
    @Persisted var ideas = List<Idea>()
}