views:

429

answers:

2

I've written a Mac OS X Dashboard to show the StackOverflow flair of yourself and other people.

My problem is that whenever I update that widget to a new version, all previous instances are removed and a single new instance is created on the Dashboard. So if you previously followed the flair of 4 people you'll have to recreate the widgets and enter their user-IDs again. :(

Is there any way to keep all running instances while updating a widget?

Checking the preferences file, I see that a new instance is created after a new version of the widget is deployed. Is this by design? Does all widgets work like this, by any chance?? If so, can this be manually circumvented somehow?

You can find the widget in question, including the project files, at http://widget.huxhorn.de

I've changed the code in remove() to set the preferences to the correct values instead of null as suggested below - but this doesn't help, either.
What's my mistake?? Help! I'm stuck!


The other problem of my widget has been fixed:
I accidentally called

widget.preferenceForKey(null, dashcode.createInstancePreferenceKey(userIdPrefKey));

instead of

widget.preferenceForKey(dashcode.createInstancePreferenceKey(userIdPrefKey));

but this is now fixed (not yet released).

A: 

I think your calls to setPreferenceForKey and preferenceForKey look strange. Where does that null come from. Shouldn't they look like

widget.preferenceForKey(dashcode.createInstancePreferenceKey(userIdPrefKey))
widget.setPreferenceForKey(value, dashcode.createInstancePreferenceKey(userIdPrefKey));

Does the syntax with null

widget.setPreferenceForKey(null, dashcode.createInstancePreferenceKey(userIdPrefKey))

in remove() delete the preference? That would explain you situation I guess. On Update the remove() gets surley called and thus on restart of the widget the preferences are gone.


I guess there is only one way to keep your preferences during an update. Create one preferencekey without relying on dashboard.createInstancePreferenceKey but instead think yourself of a unique key value.

In this preferenceKey you could then save all ever entered userids (by any instance) with a growing (say comma seperated) list. When a widget instance is opened an the widget.preferenceForKey(dashcode.createInstancePreferenceKey(userIdPrefKey)) isn't already set you could let the user choose one of those by giving him a select or such a thing

I was just thinking maybe the update process is nice enough to migrate the preferences to the new verison. Even when you don't remove the preferences I think the widget.identifier (createInstancePreferenceKey) will have changed an thus you can't access the "old" settings.

jitter
Good idea, I tried that and changed the code to - again - set the preferences to the correct values... does not help :( That code was originally meant to clean up after the widget so that there wouldn't be any unused prefs left from removed widget instances...
Huxi
Setting the values to null was ok. A single new instance is created in case of an update so removing the values of previous instances is the way to go, I guess.
Huxi
I'm aware of that. Proposed another solution in modified answer
jitter
I've just created a followup question: http://stackoverflow.com/questions/1027240/is-it-possible-to-create-new-widget-instances-from-within-a-dashboard-widgetI think that this would be necessary to be able to keep/restore previous instances. Thanks for staying with me on that topic.
Huxi
+1  A: 
Philipe Fatio
Now that's a very handy tool, thanks for the info!
Huxi