hanssens / localstorage

LocalStorage for .NET - A simple and lightweight tool for persisting data in dotnet (core) apps.
MIT License
74 stars 17 forks source link

Saving/reading from LocalStorage just doing nothing. #33

Open kamilk91 opened 4 years ago

kamilk91 commented 4 years ago

Hello

{
            bool initStart = true;

            bool startExist = store.Exists("start");
            if (!startExist)
            {

                initStart = true;
                store.Store("start", true);

            }

            if (initStart)
            {
                store.Store<bool>("start", true);
                store.Store<SingleUserData>("data", new SingleUserData { answers = new List<SingleUserAnswer>(), id = 1 });
            }
            else
            {
                var lastQuestion = store.Get<SingleUserData>("data");
                int lastQuestionId = lastQuestion.answers.Select(x => x.answerId).LastOrDefault();
            }

        }

Even if i added manualy "key" and "value" your library can't see it. The same situation is when i try to save something (simply boolean) in storage.

hanssens commented 3 years ago

Hi @kamilk91, if I simplify your sample it just works. See the example below. Also, all tests seem to prove the same.

// Store it
var key = "data";
store.Store<SingleUserData>(key, new SingleUserData { answers = new List<SingleUserAnswer>(), id = 42 });

// Fetch it
var target = store.Get<SingleUserData>(key);

// Prove it
Console.WriteLine(target.id);  // Prints: 42

Can it be that you're expecting it to be stored every time you run the program? In that case, you have to call the .Persist() operation before the routine closes.