In previous post on state preservation and restoration, I presented the basics. This post deals with something that’s fairly common in data-driven apps.
Core Data is one of my favourite iOS frameworks; I use it in almost all of my apps, anything that requires even a minor object graph. So, let’s see how to save/restore state of table views (it’s the same for collection views) fueled by
In Couch to 5k apps, this Runs tab contains
UITableViewController subclass. We know from previous post that we should adopt
UIDataSourceModelAssociation protocol for its data source:
To refresh your memory - this view controller is created in App Delegate, as top level VC for the Runs tab. Thus I only need to set its
restorationIdentifier and UIKit will do the rest (no need for the restoration class).
1 2 3 4 5 6 7 8 9
To save the state, the main thing is to choose the modelIdentifier - a string to uniquely identify
NSManagedObject (subclass) that corresponds to each cell.
The obvious choice is NSManagedObjectID as it’s universal identifier for the given object. To convert it into string, use
[[NSManagedObjectID URIRepresentation] absoluteString] thus for my case the method looks like this:
1 2 3 4 5 6 7
And that’s all that’s needed to preserve the (scrolling) state.
This was more fun to figure out and it takes 3 steps:
- use that to load actual
- finally get
NSIndexPaththat we are after, from
Here’s the full method:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
The workaround at the end is hopefully going away quickly, as we all move to iOS 7 only apps.