Use data inside BlobCache.LocalMachine.GetObject<objectl>("Key");

846 views Asked by At

I am using xamarin.forms. I am trying to insert data in caches.

var getData = JsonConvert.DeserializeObject<UserViewModel>(Respone);
await BlobCache.LocalMachine.InsertObject("RsponseData",getData);

And then I try to get cached data.

var data = BlobCache.LocalMachine.GetObject<UserViewModel>("RsponseData");

How to to use data so that I can do:

data.UserName
data.password
1

There are 1 answers

0
Mike Ma On BEST ANSWER

Do not forget to use await when you try to get the cached data. Because you should wait inserting object done.

var data =await BlobCache.LocalMachine.GetObject<UserViewModel>("RsponseData");

Try the following code:

using System.Reactive.Linq;

namespace AkavacheTest
{
    [Activity(Label = "AkavacheTest", MainLauncher = true, Icon = "@drawable/icon")]
    public class MainActivity : Activity
    {
        protected override async void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            BlobCache.ApplicationName = "AkavacheText";
            string json = @"{ 'Username': 'Mike','Password': 'Ma'}"; 
            SetContentView(Resource.Layout.Main);
            var getData = JsonConvert.DeserializeObject<User>(json);
            await BlobCache.LocalMachine.InsertObject("MikeMa", getData);
            var myuser = await BlobCache.LocalMachine.GetObject<User>("MikeMa");
            System.Console.WriteLine(myuser.Username+"---"+myuser.Password);
        }
     }
    public class User
    {
      public  string Username { get; set; }
      public string   Password { get; set; }
    }
}

You will get the output:

enter image description here