Home > Software engineering >  Swift Realm temporary storage
Swift Realm temporary storage

Time:08-16

I'm searching the best solution to save an object during it's modification with Realm because if you put the app in background, the object's modified values are currently lost. I would like to save it without impacting the original object until user press save

I try two solution:

  • duplicate the object (BaseObject, EditedObject) but this solution make a lot of code duplication.
  • duplicate the object in the base storage, adding a bool property to know if it is a temporary object but this create issue for updating object.
  • I've create different Realm Configurations but the data are available for both

Is there a way to have multiple version of a same storage? So I could have a RealmObject stored in a realm or tempRealm? Any ideas?

CodePudding user response:

I would suggest to save the modified object as a dictionary in the UserDefaults with a specific key. You can check if there's a value for that key when the app comes back from the background and actually update the realm DB. Make sure to delete the data in the UserDefaults once the update is done.

https://developer.apple.com/documentation/foundation/userdefaults

CodePudding user response:

I've encapsulate my object in an array so I can keep track of different version of it.

If someone know how to create multiple storage of a same object type with Realm, please let me know

  • Related