简体   繁体   English

如何使用 Realm swift 在本地设备上存储资产?

[英]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.我正在尝试制作一个简单的 iOS 应用程序,其中包含大量文本、视频链接等。我想使用领域存储这些数据。我不是要存储用户数据,而是要存储文本、缩略图甚至一些 3D 文件等资产。 How can I do this locally ?我怎样才能在本地做到这一点?

Realm is an offline first database so by it's nature, all data is stored locally. Realm 是离线优先的数据库,因此从本质上讲,所有数据都存储在本地。 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.上面的代码只会在本地存储一个 Task 对象。

That being said, Realm object properties are limited to 16Mb - which is great for textual data.话虽如此,Realm 对象属性限制为 16Mb - 这对于文本数据来说非常有用。

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.如果您要存储图像,MongoDB Realm 为图像数据提供了其他解决方案,还有其他解决方案以及 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.请注意,Realm 可以处理小缩略图或图形,因为它们通常有几百 K,而 3D 文件(通常基于矢量)可能可以存储在 Realm 以及它的文本数据中。

For more reading see my answer to this question更多阅读请看我对这个问题的回答

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM