Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add same object type as different tables in same realm?

Tags:

ios

realm

I'm using realm to cache certain network data coming down which is specific to the last search. I'd also like to be able to star or pin this data to save it for future usage. The data objects are exactly the same and I can't find anything in the documentation that allows me to save them in separate tables in Realm.

like image 879
Evan Anger Avatar asked Feb 02 '16 04:02

Evan Anger


Video Answer


1 Answers

The easiest way to go about doing this would be to simply create a subclass of your model object with a different name (e.g., MyDataObject and its subclass MySavedDataObject). This will create a new table in the Realm database file with the same schema and will let you distinguish between the two types of objects.

You can then create a copy of a normal object as a saved object as simply as the following:

let myNewSavedObject = MySavedDataObject(value: myDataObject)

That all being said, instead of duplicating data, I would personally recommend being a bit more efficient with the existing data set. Surely simply adding an additional boolean property to the schema named something like saved would let you simply mark objects that you wish to keep without needing a whole second table. :)

like image 88
TiM Avatar answered Sep 30 '22 06:09

TiM