Search code examples
iosswiftrealmlocal-storageassets

How do I use Realm swift to store assets on device locally?


I am trying to make a simple iOS app that has loads of text, video links, etc. I want to store this data using realm .I'm not trying to store user data but assets like text, thumbnails and even some 3D files. How can I do this locally ?


Solution

  • Realm is an offline first database so by it's nature, all data is stored locally. See the Quick Start to get started with local storage

    You would need to add additional code to sync and store in the cloud - see Sync Quick Start

    Here's a simple example for storing data locally

    class TaskClass: Object {
        @Persisted var task: String = ""
    }
    
    let task = TaskClass()
    task.task = "Go Shopping"
    let localRealm = try! Realm()
    try! localRealm.write {
        localRealm.add(task)
    }
    

    The above code will store a Task object locally only.

    That being said, Realm object properties are limited to 16Mb - which is great for textual data.

    However, it's not ideal for image storage as images can easily surpass that. If you are storing images, MongoDB Realm offers other solutions for the image data and there are other solutions as well like Firebase Storage.

    Note that Realm can handle small thumbnails or graphics as they are usually a couple hundred K, and 3D files, which are generally Vector based could probably be stored in realm as well as it's textual data.

    For more reading see my answer to this question