0

I have a Workout App and a model which has all the data for all exercises. Now i am using a singleton to access the model from every Child VC of that TableView. I am running into issues and i would like to remove the singleton.

Now how would i keep the model updated throughout the app and access it within the app from any place? That was easy with a singleton. I think i need to pass the model back from the Child VC to the Parent, so the Parent can pass it to the next Child VC when the next exercise is selected.

RyanR
  • 7,728
  • 1
  • 25
  • 39
Drazen
  • 2,776
  • 1
  • 25
  • 39

1 Answers1

0

I haven't used state preservation & restoration so I don't know what issues it has with your model being a singleton (if you elaborate on that problem I might have some ideas to help), but I've found that on apps where the data model is simple, like you have indicated, a singleton is usually easier. That said, if it just isn't an option you'll want to pass the model object between controllers. Basically put a property on each controller, then in your segues you pass that object to the destination controller.

When you pop controllers off the navigation stack you'll want to update the view as well, in case any data changed. If you're using storyboards and have a small number of scenes (the individual 'screens' within the storyboard) it's pretty easy to wire up the Unwind segue, which calls a method on the parent controller to notify it the child is getting popped off the stack - that is where you would grab the object off the child, store it on the parent controller, and refresh its view.

There are several ways to persist data in an iOS app. My favorites are Core Data and serialization. If your model is really simple (one or two object instances and just a few properties on each object) serialization is pretty easy - use NSCoding to encode/decode your object and save that data to a file in your applications data directory. If your model is more complicated (such as having relationships between objects, or a large number of object instances) Core Data is a good way to go. It manages the object 'graph' in memory and handles persisting that to disk for you; it even handles upgrading your persistent store if you need to evolve your model in the future. It is extremely powerful, but has a bit of a learning curve.

Community
  • 1
  • 1
RyanR
  • 7,728
  • 1
  • 25
  • 39
  • Thanks it is also my first time with state preservation & restoration and it confuses the hell out of me. I know how to pass the object to the destination VC but how would i pass it back to the parent, so it has the new data when moving to the next child vc? – Drazen Oct 15 '13 at 20:08
  • Btw did you use something else to remember the app state and data between launches? – Drazen Oct 15 '13 at 20:11
  • I updated my answer to elaborate on how to work with unwind segues in storyboards. If you aren't using storyboards, edit your question to elaborate on how you are handling navigation and I can give you an answer tailored to it. – RyanR Oct 15 '13 at 20:19
  • In the apps I've written I made sure data was persisted regularly so if the app quit nothing was lost. The vast majority of the time the app never quits, so state restoration was unnecessary. In the rare cases where it did quit, the only thing lost was a couple pushes on the nav stack. According to Apple we should all be using state restoration, but for relatively simple apps it seems like overkill. – RyanR Oct 15 '13 at 20:20
  • How did you persist it? Did you put it in NSUserDefaults? That's what i am thinking about doing with NSCoding instead of State restoration. – Drazen Oct 15 '13 at 20:31
  • I updated my answer to elaborate on saving data. I wouldn't use NSUserDefaults for anything except user preference-type information (pounds or kilograms, miles or kilometers, etc). I have a strong predisposition to using Core Data for anything that doesn't need to be shared with a server, just because it handles so much for you automatically. That said, it does have a learning curve, and I strongly recommend reading the whole Core Data guide before diving in with it (realistically, you should be up and running with CD in 4 hours or so) – RyanR Oct 15 '13 at 20:38
  • Thanks a lot, i have used CoreData but my app syncs heavily with the server. All your Workouts are available inside a Webapp too. I am now thinking about using NSArchiver for persistence – Drazen Oct 15 '13 at 20:42
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/39302/discussion-between-ryanr-and-drale2k) – RyanR Oct 15 '13 at 20:43