Change values ​​without affecting saved objects - Realm swift

I use realm to save my data. My problem is that I want to have small, temporary changes in objects, I do not want these changes to be reflected in my saved objects, but the area does not allow me to change the properties of returned objects without a write block, which ultimately leads to save my temporary changes to the database. I cannot create copies of objects by creating new objects and assigning values, because I have a large data set.

Is there any simple solution to achieve this?

+7
ios swift realm
source share
1 answer

Unfortunately, as you have already stated, you cannot apply new values ​​to Realm properties “temporarily” once the Realm Object been written to the Realm file. This is an established fact of the realization of the Kingdom, and there is no official path around it. Thus, it will be necessary to create another mechanism for storing these temporary values ​​in a different place and write them to Realm when the time comes.

Something you can consider here is using the Realm ignored properties function. Essentially, you can mark certain properties in a Realm Object so that you do NOT explicitly write to Realm files, and you can change them at any time (and in any stream) that you want.

 class TestObject: Object { dynamic var tmpValue = "" dynamic var actualValue = "" override static func ignoredProperties() -> [String] { return ["tmpValue"] } } 

If the type of temporary data being created is constantly the same type, you can create special ignored properties in the model object to store each of these properties, and then when you want to actually save them in Realm, you simply copy the values ​​from the ignored properties into Realm properties in a write transaction.

Of course, these ignored values ​​are only saved in the instance of the Realm Object to which they were added. Therefore, if your architecture means that you are dealing with multiple instances of a Realm Object pointing to the same data source on disk, it might be better to separate these temporary values ​​completely from the Realm Object and hang them in memory elsewhere.

Finally, although you said you didn’t want to create non-resident copies of Realm objects because of their size, some people have already created some pretty cool ways to execute such a copy without a lot of code (my favorite example is in the Realm-JSON project ), which maybe worth considering. Good luck

+7
source share

All Articles