Search code examples
iosswiftcore-datansmanagedobjectmodel

A way to persist a duplicate version of the core data model temporarily


This is a bit of a tough question to explain. I've been looking for an answer to this over two weeks but I couldn't find a solution for this use case.

Before I get into the nitty gritty details, please have a look at this image. enter image description here

The user is presented with the Main List view at first. The data (patient records) shown in this view is fetched from an API. One important thing to note here is that the data is retrieved only for the current date. There are records for the past days and days to come in the database but by default the data is of current date. After I call the API and get the data, I create objects out of them and store those objects in Core Data. Then I fetch them from my core data model and display them in the table view.

Each Patient record in Main List is actually a group of smaller records. When the user taps on a record, he is taken to a Sub List where you can see all those smaller record details. He can also edit, delete these smaller record items.

Here's where I need help. While the user is in the Sub List he can change the date from within that view. And it should display the small record details for the newly selected date. This means calling the API again, sending the selected date as a parameter and get the data for that date and display them in the Sub List view.

But this whole date changing operation has no effect on the Main List's data. In other words, the Main List's current date's data should not change even though the date was changed inside the Sub List. If/when the user taps the back button to get to the Main List, the old, current date's data should be readily available as he left it and the new data in the Sub List view is discarded upon leaving that view.

Another thing is although the new data is temporary, the user still needs to be able to edit those records. It's not simply for display purposes. That's why I need to add them to core data.

My question is once I get the data for the new date within the Sub List, is there a way I can persist that data temporarily without affecting my original data set?

I'd really appreciate any help.

Thank you.


Solution

  • When you say "fetched from an API", I presume that means fetched over the network from a web API. Right?

    In answer to the question you asked: this looks like a good case for a separate NSManagedObjectContext. You can create a new MOC for your Sub List, and then destroy that MOC when you're finished. That will allow you to work in Core Data in your Sub List view, but the changes won't be persisted in your datastore.

    Another approach would be to have a duplicate persistent store, either in permanent storage or in memory.

    However, it sounds like Core Data is not the right tool for this job. If you're pulling data from a web service, and persisting different chunks of it for different periods of time, you'll spend more time fighting Core Data than it's worth.