MVC design Q - remembering Core Data selections
MVC design Q - remembering Core Data selections
- Subject: MVC design Q - remembering Core Data selections
- From: Ken Tabb <email@hidden>
- Date: Mon, 25 Jan 2010 09:38:09 +0000
Hi folks,
I have a Core Data app that needs to remember state of selected items
between launches.
The app's UI is a bit like iTunes (containers / playlists down the
side and contents show in a big tableview), except that there are 3
layers of containment, so rather than Playlists and Tracks, you could
think of it as Companies, Departments & Employees.
What I'd like is for each Company to remember the last selected
Department in it, and for each Department to remember the last
selected Employee in it. I'd like to remember this between launches.
To clarify, I'm not just wanting to remember which Company / Dept /
Employee the user was last looking at before they quit, but for each
Company & Department, what the last selected object within it was.
So in other words say the user clicks on "Apple", and then on the
"Upper Echelons" Department, and then "Steve Jobs". After this, the
user then clicks on a different company, say... Slate Computing LLC...
and on some department and employee within it. The user then quits.
When the user relaunches, I'd like it to go straight to showing the
Slate Computing LLC + selected department + selected employee.
However, if the user clicks on Apple, I'd like it to remember that
they were last looking at its "Upper Echelons" department, and "Steve
Jobs" in particular.
The hack that I can think of to do this would be for each Company
entity to have a "selectedDept", and for each Department entity to
have a "selectedEmployee" relationship, but this kind of breaks MVC...
other than it being easy to implement, the model shouldn't care what
the user was last looking at. Furthermore if I ever implement multi-
client access, then it can only remember (in the central data
repository) what 1 user was last looking at, so if Fred was last using
the app and I launched it, it would start me up with Fred's last
selection if this is stored inside the model.
What is the preferred MVC design for implementing this kind of
functionality?
I could be barking up the wrong tree here but I'm thinking I should
override the Company & Department array controllers, so that each time
their selection changes, I get the managed object IDs for the new
selection, and write it out to a preference list (or a separate per-
user persistent store), and reading from this when the user selects
something else, so we know which child item to select by default for
them. Would this break when the managed objects go from being
temporary (i.e. new) to permanent (i.e. saved) though? Or should I
only write the file / 2nd store when the app is quitting (after
everything that's staying has been committed)?
Hope I've stated my problem well enough - if anything needs
clarification let me know.
Thanks in advance for any help you can give,
Ken
The app's UI is like iTunes, with playlists and songs inside playlists
(except that my app has nothing to do with music etc.). Playlists and
songs are all Core Data objects, and I have the equivalent of a
"Playlists" controller and a "Songs" controller (whose content is
bound to the selected Playlist). In my app, the playlists down the
left of iTunes have an extra textfield saying what their currently
selected song is (whether that playlist/song is the one playing or not).
I'd like my app to remember the selected song in each playlist (not
just the playlist the user was last in when quitting). What is the
cleanest MVC way of accomplishing this?
I can see it would be easy to have an attribute within a Playlist that
has selectedSong, and the Songs controller sets that whenever its
selection changes. But clearly this isn't a "model" issue, it's merely
a GUI nicety for the user.
So maybe I'm barking up the wrong tree here, but I'm considering
storing the managed object IDs of all Playlists, and their selected
Song, in a prefs dictionary. But I'd need to do that after the store
has been committed, in order to be able to grab 'permanent' object
IDs. And then upon launch, how do I set each
In order to remember this state, I'd like to use managedObjectIDs (for
an SQLite store). So to use an iTunes analogy, each playlist might
have a "lastTrackBeingPlayed" managed attribute, into which I'd like
to stick the managedObjectID of whatever track was being played when
the user quit the app.
However I'd like to ensure that I'm using permanent objectIDs, not
temporary ones, so would the best route be, in my appDelegate's -
applicationWillTerminate: method, to do:
- (void)applicationWillTerminate:(NSNotification *)aNotification
{
NSManagedObjectContext *moc = [self managedObjectContext];
NSError *error = nil;
[moc save:&error]; //save to convert all objects to permanent
[self doSomeCacheStuff]; //perform caching of managedObjectIDs here
[moc save:&error]; //now resave, to make sure all object IDs are
permanent
}
I have a Core Data app that's a bit like iTunes (except it doesn't
play music or do any of the other iTunes stuff), and I'd like it to
remember state between launches. So basically, when you re-launch it,
I'd like it to:
[1] Remember which playlist you were last playing (like iTunes does
between launches)
[2] Remember which song of each playlist was being played (whereas
iTunes defaults to track 0 between launches)
So if in session 1, I listen to:
- Playlist 5, track 150
followed by:
- Playlist 12, track 27
... then hopefully in session 2, it would remember that last time I
launched this app, I was last listening to:
- Playlist 12, track 27
... but I'd also like that, were I to switch to Playlist 5 at some
point in the future, it would remember that I was last listening to
track 150 from that playlist.
- - - - - - - - - -
Dr. Ken Tabb
Mac & UNIX Developer - Health & Human Sciences
Machine Vision & Neural Network researcher - School of Computer Science
University of Hertfordshire, UK
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden