Thank you for reporting this bug. We will make it our priority to review this report.
  1. #1
    Sencha Premium Member
    Join Date
    Jun 2010
    Posts
    35
    Vote Rating
    0
    eguardiola is on a distinguished road

      0  

    Default store.clear() removes all items of the list returned by store.getAll()

    store.clear() removes all items of the list returned by store.getAll()


    I think this issue smells like a bug.

    Code:
    package local.test.client;
    
    import java.util.List;
    
    import com.google.gwt.core.client.EntryPoint;
    import com.google.gwt.user.client.Window;
    import com.sencha.gxt.data.shared.ListStore;
    import com.sencha.gxt.data.shared.ModelKeyProvider;
    
    
    public class Test implements EntryPoint {
    	
    	public class Cat {
    		String id;
    		private final String name;
    
    		public Cat(String id, String name) {
    			this.id = id;
    			this.name = name;
    		}
    
    		public String getId() {
    			return id;
    		}
    
    		public String getName() {
    			return name;
    		}
    	}
    
    	public void onModuleLoad() {
    		
    		ListStore<Cat> store = new  ListStore<Cat>(new ModelKeyProvider<Cat>() {
    
    			@Override
    			public String getKey(Cat item) {
    				return  item.getId();
    			}
    		});
    		
    		Cat cat1 = new Cat("1", "Cat1");
    		Cat cat2 = new Cat("2", "Cat2");
    		
    		store.add(cat1);
    		store.add(cat2);
    		
    		List<Cat> storeAll = store.getAll();
    		Window.alert("storeAll size is: " + storeAll.size()); // this prints 2
    		store.clear();
    		Window.alert("After store.clear() storeAll size is: " + storeAll.size()); // this prints 0
    	}
    My project code has suffered from it. Is not obvious that the list returned by store.getAll() will be cleared when an store.clear() be invoked. i'm right?

  2. #2
    Sencha - GXT Dev Team
    Join Date
    Feb 2009
    Location
    Minnesota
    Posts
    2,732
    Vote Rating
    90
    Colin Alworth is a glorious beacon of light Colin Alworth is a glorious beacon of light Colin Alworth is a glorious beacon of light Colin Alworth is a glorious beacon of light Colin Alworth is a glorious beacon of light

      0  

    Default


    I'm not sure I agree - If you want a copy of the list, shouldn't you make a copy yourself? Should the standard case cause a complete copy of the contents of the ListStore whenever getAll() is called?
    Code:
    List<Cat> storeAll = new ArrayList<Cat>(store.getAll());//explicitly copy only when required
    We can clarify the javadocs on that method, but I believe this is how 2.x behaved, and for most cases will be the best performing option. IE-browsers perform particularly poorly when creating and copying arrays, so we try to avoid array-copy wherever possible.

  3. #3
    Sencha Premium Member
    Join Date
    Jun 2010
    Posts
    35
    Vote Rating
    0
    eguardiola is on a distinguished road

      0  

    Default


    Javadocs seems to say that is indeed a copy.

    Returns a list of all items contained in the store. Modifying this list will not change the store, as this is a copy of the contents of the store. Note also that because this is a copy, this can be a expensive call to make.

  4. #4
    Ext GWT Premium Member
    Join Date
    Aug 2010
    Location
    Germany, Solingen
    Posts
    239
    Vote Rating
    2
    gishmo is on a distinguished road

      0  

    Default


    I agree with Colin.

    A store.clear(); should remove all items from the store.That is, what I would expect executing this statement.

  5. #5
    Sencha Premium Member
    Join Date
    Jun 2010
    Posts
    35
    Vote Rating
    0
    eguardiola is on a distinguished road

      0  

    Default


    gishmo, nobody says that store.clear() should not remove the items in the store. But should it remove the items of the list returned by store.getAll() ??

  6. #6
    Ext GWT Premium Member
    Join Date
    Aug 2010
    Location
    Germany, Solingen
    Posts
    239
    Vote Rating
    2
    gishmo is on a distinguished road

      0  

    Default


    Yes, of course!
    If you clear a store, it is empty. If you request data from an empty store, the result should also be empty.

  7. #7
    Sencha Premium Member
    Join Date
    Jun 2010
    Posts
    35
    Vote Rating
    0
    eguardiola is on a distinguished road

      0  

    Default


    of course, the store size after a clear should be zero. Be what about the size of the returned list before the store.clear() ?

    I think u are missing something.

  8. #8
    Ext GWT Premium Member
    Join Date
    Aug 2010
    Location
    Germany, Solingen
    Posts
    239
    Vote Rating
    2
    gishmo is on a distinguished road

      0  

    Default


    storeAll is the reference of the list and not a copy.
    So, clearing the store and calling getAll(), the size of the list is zero.

    If you need a copy of the list, create one with new ArrayList<T>(store.getAll()).

    JavaDoc might be not clear enough.

  9. #9
    Sencha Premium Member
    Join Date
    Jun 2010
    Posts
    35
    Vote Rating
    0
    eguardiola is on a distinguished road

      0  

    Default


    being a reference sure javadoc is confusing. I just want to make clear this detail.

  10. #10
    Sencha User
    Join Date
    Mar 2012
    Posts
    88
    Vote Rating
    3
    esag_dk is on a distinguished road

      0  

    Default


    It seems, that the Javadocs really is a little bit confusing:
    http://dev.sencha.com/deploy/gxt-3.0.0-rc2/javadoc/gxt/com/sencha/gxt/data/shared/Store.html#getAll%28%29

    That's the documentation for an (abstract) Store - so why does it even contain so much information, since the (List)Store implementation can do something completely different - and obviously is:
    Code:
    public List<M> getAll() {
        return Collections.unmodifiableList(visibleItems);
      }