Results 1 to 8 of 8

Thread: localStorage.RemoveItem Not Removing Item

  1. #1

    Thread Starter
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    localStorage.RemoveItem Not Removing Item

    You've got just ONE job....

    This is actually Cordova running on an Android tablet, but I have no idea at this time whether that's relevant. It's just JS/JQuery, which happens to be hosted on a tablet. The application was originally a web page that was being used offline. I converted it to a Cordova app because I needed to be able to get access to hardware that web pages aren't allowed to. That change was remarkably pain-free, so the code remained the same. What I can't say for certain is whether or not this issue existed before the move to Cordova.

    I have an object with a key of 'Provisional'. At various times, I want to remove that item, so I just had:

    localStorage.removeItem('Provisional')

    People were able to run the application fine, so this issue was too small an issue for anybody to report it to me. However, somebody did tell me about one oddity that got me looking at the code, at which point I ran into this issue: That line isn't doing anything...eventually.

    What I found was that the 'Provisional' item was not actually being removed. The next time the application was run, it was still there. A bit of looking online showed that a few others had this issue. One person said that they had to wait a few seconds, others didn't have any real solution. I tried waiting, but that had no effect, even if I waited half an hour (which I did because I was snorfling down my lunch).

    So, I then tried reading the item back right after the call to removeItem. In other words, it looked like this:

    localStorage.removeItem('Provisional')
    var temp = localStorage.getItem('Provisional');

    As expected, temp ended up as null, which suggests that removeItem had worked. However, if I then stopped and re-started the app, Provisional was still there, unchanged.

    There's an easy workaround to this particular case, because I can just set the item to null rather than removing it. The problem is that I use removeItem in one other place, and see it being used in some js mods that aren't mine. I don't understand what it is doing, so I'm not sure when I can trust it and when I can't...or if I never can, which would be really bad, because it would mean that my application will keep gobbling up memory until the system crashes, since it will never be able to fully release any part of localStorage.
    My usual boring signature: Nothing

  2. #2

    Thread Starter
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    Re: localStorage.RemoveItem Not Removing Item

    Just tried out a different place, and it isn't working, either. Here's the code:

    Code:
        $.each(localStorage, function(index){
            var ky = localStorage.key(index);
            if (ky.startsWith('sID')) {
                localStorage.removeItem(ky); 
            }
        });
    This does find plenty of items that begin with 'sID', as a breakpoint on the removeItem line is hit every time. However, it really IS hit every time, because none of the items are actually removed, so I get to try it over and over.

    UPDATE: So, I had tried it repeatedly while stopping the application between attempts. This time I tried it repeatedly without stopping the app. I just called that method over and over and over, with a breakpoint on the removeItem line. This was instructive. There are 12 items that match the criteria on the current device. The first time I called the method, it hit the breakpoint 10 times. The next time I called the method it hit the breakpoint 5 times, the next was 2 times, then 1 time, 1 time again, and all subsequent attempts never hit the breakpoint. I've now tried that three times with the same results.

    So, it doesn't even appear to be visiting items reliably, and isn't deleting any of them at all.

    I'm open to any suggestions.
    Last edited by Shaggy Hiker; Nov 1st, 2017 at 02:15 PM.
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    Re: localStorage.RemoveItem Not Removing Item

    Ok, it's a caching issue. That makes it more tractable, but not solved....or, at least, the solution is an ugly one.

    I went looking on the device, and the items did, in fact, appear to be missing. They were still showing up in the application, but didn't appear to be showing on the device. That made me suspect that they were being cached. The deletion had (pretty much) worked, but it didn't matter, because the data was all remaining in memory until the app had a hard restart.

    My next thought was to try forcing a reload, which was accomplished (inelegantly) with this:
    Code:
    window.location.reload(true);
    The reason this is so inelegant is that this app takes a bit of time to reload. If I have to force a reload for a deletion to actually work....that'll be ugly, each and every time. So, now I'm looking for a way to dump the cache without needing to force a reload of the app.

    Also, I'm not sue what to make of the 10, 5, 2, 1, 1, 0 pattern in the deletions. Now that I have wiped the DB, that won't be easy to figure out.
    My usual boring signature: Nothing

  4. #4

    Thread Starter
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    Re: localStorage.RemoveItem Not Removing Item

    Strike that thought.

    I decided to test something around the original question. To do that, I closed the app on the device, then relaunched....and all the items were back. So, it appears that I can delete, then reload the window, and they will appear to be gone, but the next time the application runs, they are all back. There is some deeper level of caching going on. I can't figure out where the stuff is being stored, but since I can't delete it, I'm not sure that it even matters where it is being stored.
    My usual boring signature: Nothing

  5. #5
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: localStorage.RemoveItem Not Removing Item

    Any chance you're using Chrome? Is it possible that the data is being synch'd with another device, where the data is still being held? I have some of my form data synch'd so that it makes it easier for my to login on various sites from different devices. Just thinking out loud, wondering if something similar is going on. What happens if you run the app in private/incognito mode?

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  6. #6

    Thread Starter
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    Re: localStorage.RemoveItem Not Removing Item

    Well, it's always a possibility. I'm debugging from a PC to an Android device. I would assume that the webview behind Cordova on Android is Chrome, but I haven't seen it stated explicitly. I've been considering whether it could be getting cached...but where? I was thinking that it might be onto the PC, but that would be nuts. However, I think there's also a wireless connection, so I might be getting synched to some Google cloud. I'll have to look into that.

    Doesn't seem very likely, though.

    One thing I did determine is that, while removeItem didn't remove an item, clear() did exactly what it's supposed to do, and wiped the slate clean. That probably rules out any sync possibility.
    My usual boring signature: Nothing

  7. #7

    Thread Starter
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    Re: localStorage.RemoveItem Not Removing Item

    This is becoming a running log of my studies around this issue. The key function is pretty much here (it includes a bit of garbage used for testing):

    Code:
    function ClearDataTables() {
        //The point is to remove all survey items from the local store.
        //localStorage.clear();
        var count = 0;
        test = [];
        $.each(localStorage, function(index){
            var ky = localStorage.key(index);
            if (ky.startsWith('sID')) {
                test.push(ky);
                localStorage.removeItem(ky); 
                count += 1;
            }
        });
        $('#survey-data').empty();
        InitializeData();
        alert('Did whatever was done. Count: ' + count);
        //There may be a provisional record, if so, remove it.
        //localStorage.setItem('Provisional', null);
        //window.location.reload(true);
    }
    I was thinking about this, and realized that it was likely that I was actually seeing a mashup of a couple different issues. One of the issues was that I was displaying all the surveys, but wasn't refreshing that display when I deleted a survey. Therefore, even if the deletions worked, I wouldn't know. My first attempt to fix that was to clear the display and repopulate. I tested this by not deleting the surveys, but just logging which keys were encountered. That's the point behind the Test[] variable. That showed that I was seeing exactly the set that I expected. A breakpoint on the alert at the end of the method was always hit.

    I then uncommented the localStorate.removeItem line, but left it alone. A review of that showed some interesting things that I was only semi-aware of. The key point there was that the removeItem call is asynchronous. I would be able to clear the display and repopulate before any surveys had actually been deleted, even though an examination of the test[] variable showed that some WERE being deleted. Re-starting the app confirmed this, as the ones slated for deletion were deleted, even though they were still shown in the grid (because the grid was populated before the deletion could happen).

    Oddly, only a fraction of the items were slated for deletion. This is probably a result of one or two different things. I don't know how a JQuery iterator works. I'm deleting items from the collection that I'm iterating through, which pretty much means that ANYTHING is possible, depending on the exact implementation of the iterator method. That's an easy enough one to get around. This would account (partially) for some of the odd results I reported earlier. Specifically, it might account for the pattern of 10 surveys, then 5 surveys, then 2, then 1 then 1. It isn't quite clear to me why THAT particular pattern resulted, but a strange pattern could certainly result. Especially, that 2, 1, 1, is pretty much how I would expect the pattern to end, and it's how the pattern works if there are only 4 surveys to start with.

    So, that leaves just a couple mysteries:

    1) Why didn't the surveys go away the first time?

    I ran a version of that method over and over and saw that 10, 5, 2, 1, 1 pattern several times over. I've now tried this several more times with 4 surveys. I saw the 2, 1, 1 pattern, which I can understand, but I had to enter new surveys after each test, because the suckers really were being deleted. So, how was I able to run the loop repeatedly before, since that means that the surveys were not being deleted?

    2) When they came back, how did that happen?

    At one point, I thought I had the problem solved, because the surveys did go away. I then restarted the program on the tablet (not through the debugger), and all my deleted surveys were back as if they had never left. How did that happen? I haven't been able to replicate that behavior.

    In any case, my current understanding is this:

    1) localStorage.removeItem is asynchronous. It will happen....eventually, but until it happens, nothing has been removed, so it is free to be reloaded.
    2) Removing items from a collection in a loop through that collection has no better chance of working in JS/JQuery than it does in .NET, which is entirely reasonable. JS/JQuery won't error out the way .NET will, but the result is that not all items will be visited, and the pattern of what will/won't be visited is predictable.
    3) There is no guarantee that breakpoints in JS in VS will be hit. I put a breakpoint on the call to InitializeData. As long as there was more than one survey, that breakpoint was never hit, even though the line was executed. Once there was only one or zero surveys in the preceding loop, the breakpoint was always hit. Also, if the removeItem line was commented out, the breakpoint was always hit. Therefore, I would assume that asyncyhronous calls mess with VS and cause it to lose track of breakpoints downstream of the asynchronous call. This may be an incorrect understanding, as it may have to do with loops and collections being modified that messes with the breakpoints.

    There is also a further mystery that remains unsolved. This whole thing started because I was trying to remove ONE special item, and that item was never being removed. It would always come back to life. The asynchronous nature shouldn't have mattered, and there were no iterators to mess with. However, it is still possible that it was a mashup of those three items. I can't say for certain that the item wasn't still in memory and getting saved after the deletion happened. I don't care about that one, though, because setting it to null works just as well.
    My usual boring signature: Nothing

  8. #8
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343

    Re: localStorage.RemoveItem Not Removing Item

    Probably not much use and you've already thought it, but the pattern of the 10, 5, 2, 1, 1 suggests (to me) that on your deletion, its skipping one record. for example you loop through an collection forwards and on removal of an item the rest shuffle back. since you expected the current item to be gone, the pointer moves on, but leaves the data behind. Youd need to loop backwards... but that assumes the collection is in an order like an array. If not perhaps hold the ones marked for deletion in an array, loop that and remove the corresponding item in the collection. It might solve that problem.

    Its been interesting following how you are debugging it. Let us know if you manage to fix it

    Good luck


    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width