YosiFZ
YosiFZ

Reputation: 7900

Save object to the device

I have started to develop an app for Windows Phone. I'd like to make a Singleton object which has an array of custom objects. I would also like to have some custom functions like Add, CheckIfExists, Delete e.t.c.

I need to save this array when the app closes, and load the array to Singleton when the app starts.

Can anyone help me with what I should do? Where should I start?

On the iOS platform we have NSUserDefault that I use to save objects with a key that I want, and then I load it from the key.

Upvotes: 0

Views: 106

Answers (1)

Soonts
Soonts

Reputation: 21956

Isolated storage is just one of the available options.

The direct functional analog of NSUserDefault from iOS is IsolatedStorageSettings class. Here's a snipped with a few helper methods to save/load values by keys:

static T getValue<T>( string _propName )
{
    return getValue( _propName, default( T ) );
}

static T getValue<T>( string _propName, T _defaultValue )
{
    var iss = IsolatedStorageSettings.ApplicationSettings;
    T res;
    if( iss.TryGetValue( _propName, out res ) )
        return res;
    return _defaultValue;
}

static void setValue( string _propName, object val )
{
    IsolatedStorageSettings.ApplicationSettings[ _propName ] = val;
    IsolatedStorageSettings.ApplicationSettings.Save();
}

Or, you could use full-blown ORM + RDBMS. In iOS you use Core Data with underlying SQLite, in WP7 ~same is called Entity Framework with underlying SQLCE.

All 3 solutions has it's pros and cons. Choose carefully based on your data size (e.g. you shouldn't keep megabytes of data in IsolatedStorageSettings) and data access patterns (e.g. if you need to query, or you only insert small items while keeping the DB largely intact, the SQL engine can speed up by orders of magnitude).

Upvotes: 1

Related Questions