localstorage icon indicating copy to clipboard operation
localstorage copied to clipboard

Error with new LocalStorage instance

Open ahmedsaidzahran opened this issue 5 years ago • 4 comments

JsonReaderException: Additional text encountered after finished reading JSON content: c. Path '', line 1, position 2.

using (var storage = new LocalStorage())

it worked fine but when used .Clear() it crashed, also I used the new release v2.0.0-beta-2 but nothing

ahmedsaidzahran avatar Feb 12 '19 07:02 ahmedsaidzahran

The error you mentioned occurred in older versions. However, it should be resolved in v2.0.0-beta-2. I'm really interested in getting to the bottom of this and make sure it's not a regression issue.

@ahmedsaidzahran Do you have additional info, or a code sample, that helps me reproduce the problem?

hanssens avatar Mar 19 '19 20:03 hanssens

@hanssens and @ahmedsaidzahran, The issue is related to using(){}. The storage object is not destroyed so it is overrated by the new object that was created in an inner method that responsible for storing the value. you will find below my examples for the issue and my solution.

Issue Ex:

  using (var storage = new LocalStorage())
            {
                
                if(storage.Exists(Strings.Token))
                {
                    return storage.Get(Strings.Token).ToString();
                }
                else
                {
                    GenerateToken().Wait();
                    return storage.Get(Strings.Token).ToString();

                }
            }

Soultion:

           var storage = new LocalStorage();
            if (storage.Exists(Strings.Token))
            {
                return storage.Get(Strings.Token).ToString();
            }
            else
            {
                GenerateToken().Wait();
                return storage.Get(Strings.Token).ToString();

            }

MuhammadAbdelsalam avatar Oct 20 '20 12:10 MuhammadAbdelsalam

@hanssens, I suggest we could convert the Storage property to be static.

MuhammadAbdelsalam avatar Oct 20 '20 14:10 MuhammadAbdelsalam

I can't reproduce the problem you mention. Is there any way you @ahmedsaidzahran or @MuhammadAbdelsalam can provide me with an example?

Something like for example this Clear() test would be great as a reusable example.

hanssens avatar Feb 20 '21 09:02 hanssens