65

I have a simple Xamarin Forms app. I've now got a simple POCO object (eg. User instance or an list of the most recent tweets or orders or whatever).

How can I store this object locally to the device? Lets imagine I serialize it as JSON.

Also, how secure is this data? Is it part of Keychains, etc? Auto backed up?

cheers!

Pure.Krome
  • 78,923
  • 102
  • 356
  • 586

5 Answers5

86

You have a couple options.

  1. SQLite. This option is cross-platform and works well if you have a lot of data. You get the added bonus of transaction support and async support as well. EDIT: In the past I suggested using SQLite.Net-PCL. Due to issues involving Android 7.0 support (and an apparent sunsetting of support) I now recommend making use of the project that was originally forked from: sqlite-net
  2. Local storage. There's a great nuget that supports cross-platform storage. For more information see PCLStorage
  3. There's also Application.Current.Properties implemented in Xamarin.Forms that allow simple Key-Value pairs of data.

I think you'll have to investigate and find out which route serves your needs best.

As far as security, that depends on where you put your data on each device. Android stores app data in a secure app folder by default (not all that secure if you're rooted). iOS has several different folders for data storage based on different needs. Read more here: iOS Data Storage

Will Decker
  • 3,047
  • 16
  • 28
  • For very simple data, I also recommend the Application.Current.Properties. But keep in mind, that you call Application.Current.SavePropertiesAsync(); after setting a property. Otherwise, you can not know if they will survive very a restart of the application. – Tillito Apr 27 '18 at 17:38
  • 9
    Also keep in mind, that the Application.Current.Properties is meant for pause-resume scenarios. The value normally does not persist when the application re-starts. – Tillito May 09 '18 at 12:17
23

Another option is the Xamarin Forms settings plugin.

E.g. If you need to store a user instance, just serialize it to json when storing and deserialize it when reading.

Uses the native settings management

  • Android: SharedPreferences
  • iOS: NSUserDefaults
  • Windows Phone: IsolatedStorageSettings
  • Windows RT / UWP: ApplicationDataContainer

      public User CurrentUser
      {
         get
         {
            User user = null;
            var serializedUser = CrossSettings.Current.GetValueOrDefault<string>(UserKey);
            if (serializedUser != null)
            {
               user = JsonConvert.DeserializeObject<User>(serializedUser);
            }
    
            return user;
         }
         set
         {
            CrossSettings.Current.AddOrUpdateValue(UserKey, JsonConvert.SerializeObject(value));
         }
      }
    

EDIT: There is a new solution for this. Just use Xamarin.Essentials.

Preferences.Set(UserKey, JsonConvert.SerializeObject(value));
var user= JsonConvert.DeserializeObject<User>(Preferences.Get(UserKey, "default_value");
puko
  • 2,156
  • 3
  • 13
  • 23
17

Please use Xamarin.Essentials

The Preferences class helps to store application preferences in a key/value store.

To save a value:

Preferences.Set("my_key", "my_value");

To get a value:

var myValue = Preferences.Get("my_key", "default_value");
Rumit Patel
  • 3,667
  • 10
  • 38
  • 49
chunhunghan
  • 34,833
  • 3
  • 38
  • 59
0

If you want to store a simple value, such as a string, follow this Example code.

setting the value of the "totalSeats.Text" to the "SeatNumbers" key from page1

Application.Current.Properties["SeatNumbers"] = totalSeats.Text;
await Application.Current.SavePropertiesAsync();

then, you can simply get the value from any other page (page2)

var value = Application.Current.Properties["SeatNumbers"].ToString();

Additionally, you can set that value to another Label or Entry etc.

SeatNumbersEntry.Text = value;
MrMalith
  • 832
  • 8
  • 20
-1

If it's Key value(one value) data storage, follow below code

Application.Current.Properties["AppNumber"] = "123"

await Application.Current.SavePropertiesAsync();

Getting the same value

var value = Application.Current.Properties["AppNumber"];