在iOS上的Realm中存储数据的最佳实践

时间:2022-09-14 16:57:19

Say I have an Image model that stores x, y, width, height, and a thumbnail, then should I store the actual image data with a data: Data property or store a filePath: String and save the image data to disk? If storing in a Data var, from doc Data is only read when the properties are accessed so I assume the data is not loaded to memory directly; when reading the data property, is it the same underlying action as if we are reading from a filePath or would it be faster?

假设我有一个存储x,y,宽度,高度和缩略图的Image模型,那么我应该将实际图像数据与数据存储:Data属性还是存储filePath:String并将图像数据保存到磁盘?如果存储在Data var中,则只能在访问属性时读取doc Data,因此我假设数据未直接加载到内存中;在读取数据属性时,它是否与我们从filePath读取时的基本操作相同,还是更快?

Also for smaller sized thumbnail what would be the best practice?

对于较小尺寸的缩略图,最佳做法是什么?

1 个解决方案

#1


1  

In general (not just Realm), it is not a good practice to store binary data such as images in a database. Because binary data is not able to query or sort, So there are no advantages to use databases. It is a better practice to save the file path rather than binary data in most cases.

通常(不仅仅是Realm),将二进制数据(如图像)存储在数据库中并不是一个好习惯。由于二进制数据无法查询或排序,因此使用数据库没有任何优势。在大多数情况下,保存文件路径而不是二进制数据是更好的做法。

#1


1  

In general (not just Realm), it is not a good practice to store binary data such as images in a database. Because binary data is not able to query or sort, So there are no advantages to use databases. It is a better practice to save the file path rather than binary data in most cases.

通常(不仅仅是Realm),将二进制数据(如图像)存储在数据库中并不是一个好习惯。由于二进制数据无法查询或排序,因此使用数据库没有任何优势。在大多数情况下,保存文件路径而不是二进制数据是更好的做法。