How do I force unloadAll() to execute?

Hi everyone,

I’m trying to use store.unloadAll('model-name') before calling a subsequent store.query('model-name', params).

Sometimes the unloadAll method works as I expect it to, which is to unload all the models in the store, which then updates my view. However, sometimes the previous models stick around. I know the unloadAll method only schedules an unload for the next run loop.

I’ve tried using Ember#run() but that didn’t work either.

Is there a particular way to do this? Perhaps some sort of hook I’m unaware of?

Thanks in advance!

This isn’t really a great answer, and hopefully someone else out there will know more about this, but I think there are a lot of issues around unloadAll. In fact I opened one back in April about it not freeing memory. Your best bets might be some heavy googling, reading a lot of those github issues, or :scream: digging into the source. Hopefully you can find something out, or at least a reliable workaround. Good luck!

2 Likes

Thanks for your input, @dknutsen.

runspired on the Slack channel gave me some help, and ultimately the reality of the the DS.Store#unloadAll being unreliable when trying to use it for specific models (e.g., unloadAll('model-name')). He said there are only two ways to produce the outcome I was desiring, and they were:

  1. Figure out how to disconnect the records of interest from all other models. In other words, remove all references to the records so that it can be garbage collected.
  2. Unload all the models in the store (e.g., simply call unloadAll()).

I opted for the latter as it was the most simplest approach for the time-being, and the case in which I’m using it is pretty isolated. However, I imagine this wouldn’t be the most efficient solution for other people.

To further ensure my app would produce the desired results, I wrapped the unloadAll() call in an Ember.run() block.

TL;DR.

Ember.run(function(){
  store.unloadAll(); // UNLOAD ALL THE THINGS!
});
1 Like

Yeah that sounds like pretty much the same issue we ran into. Unfortunately in our case unloading the entire store doesn’t really do what we need. Glad you found a workaround that fits your usecase!

1 Like

I’m running into the same issue, four years later. Are there any known updates to this?

unloadAll always works the first time, and fails subsequent times.

I found this workaround, which I run in beforeModel(),

store.peekAll('model').map(model => model.unloadRecord());

here: this.get('store').unloadAll(type) works only once · Issue #5447 · emberjs/data · GitHub