Hybrid View

  1. #1
    Sencha User
    Join Date
    Aug 2012
    Posts
    36
    Vote Rating
    1
    Answers
    1
    arael78 is on a distinguished road

      0  

    Default Unanswered: Store id entries duplicated

    Unanswered: Store id entries duplicated


    I have a problem with the stores. Whenever I update an entry it the id are duplicated.

    Here is my model.

    Code:
    Ext.define('MoodleMobApp.model.Course', {
            extend: 'Ext.data.Model',
            
            config: {
                    fields: [
                            {name: 'id', type: 'auto'},
                            {name: 'name', type: 'string'},
                            {name: 'timemodified', type: 'int'},
                            {name: 'token', type: 'string'},
                            {name: 'modules', type: 'int', defaultValue: 0},
                            {name: 'isnew', type: 'boolean'},
                            {name: 'newmodules', type: 'int', defaultValue: 0},
                            {name: 'modulestatus', type: 'string', defaultValue: 'counting modules...'},
                    ]
            }
    });
    Here is the store:
    Code:
    Ext.define('MoodleMobApp.store.Courses', {
            extend: 'Ext.data.Store',
    
    
            requires: [
                    'MoodleMobApp.model.Course',
                    'Ext.data.proxy.LocalStorage'
                    ],
    
    
            config: {
                    storeId: 'courses',
                    model: 'MoodleMobApp.model.Course',
                    autoLoad: true,
                    proxy : {
                            id: 'course',
                            type: 'localstorage',
                    }
            }
    });
    Here is the code that adds/updates new entries:

    Code:
    // update local courses store
    this.each(
             function(entry) {
                  if(courses_store.find('id', entry.getData().id) == -1) {
                                  entry.getData().isnew = true;
                                  courses_store.add( entry );
                          } else {
                                  entry.getData().isnew = false;
                                  // update the entry
                                  courses_store.remove( entry );
                                  courses_store.add( entry );
                          }
                  }
            );
    
    
    
    
          // prepare to write
          courses_store.each(
                  function() { 
                          this.setDirty();
                  }
          );
    
    
    
    
          // store data
          courses_store.sync();
    When I execute this code the number of the records is the same but the ids are duplicated.

    So when I have three records courses_store.getCount() === 3 but if I check in the localstorage the course key contains this: 3,5,8,3,5,8.

    If I execute this code one more time I get: 3,5,8,3,5,8,3,5,8.

    Everything works but the course value keeps growing.

    How can I fix this issue?

    Thank you very much for any suggestions.

  2. #2
    Sencha - Senior Forum Manager mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    St. Louis, MO
    Posts
    33,641
    Vote Rating
    434
    Answers
    3107
    mitchellsimoens has much to be proud of mitchellsimoens has much to be proud of mitchellsimoens has much to be proud of mitchellsimoens has much to be proud of mitchellsimoens has much to be proud of mitchellsimoens has much to be proud of mitchellsimoens has much to be proud of mitchellsimoens has much to be proud of mitchellsimoens has much to be proud of

      0  

    Default


    When you update, why are you removing the record and then adding it? Why not use the set method on the record?
    Mitchell Simoens @SenchaMitch
    Sencha Inc, Senior Forum Manager
    ________________
    http://www.JSONPLint.com - Source to lint your JSONP!

    Check out my GitHub, lots of nice things for Ext JS 4 and Sencha Touch 2
    https://github.com/mitchellsimoens

    Think my support is good? Get more personalized support via a support subscription. https://www.sencha.com/store/

    Need more help with your app? Hire Sencha Services services@sencha.com

    Want to learn Sencha Touch 2? Check out Sencha Touch in Action that is almost in print!

    When posting code, please use BBCode's CODE tags.

  3. #3
    Sencha User
    Join Date
    Aug 2012
    Posts
    36
    Vote Rating
    1
    Answers
    1
    arael78 is on a distinguished road

      0  

    Default


    Quote Originally Posted by mitchellsimoens View Post
    When you update, why are you removing the record and then adding it? Why not use the set method on the record?
    I didn't use set() because I'm still learning this framework and I overlooked it. Thank you for your suggestion.

    I found out what was the problem. I invoked setDirty() on all of my records. Invoking the sync() then duplicated the entries. Now I invoke setDirty() only on the records I pass to add() or set() and there are no duplicates anymore.

    Why is it so setDirty() + sync() on the existing entries generates duplicates?
    The same does not happens when I use set() or add().

    Example: The "module" key contains 3,7,8 in the localstorage

    This duplicates the entry 3:

    Code:
    modules_store.getById(3).setDirty();
    modules_store.sync();
    The value of "module" key is 3,7,8,3.

    This does not duplicate the entry:
    Code:
    // here the updated_entry is a record with the id 3 and new values
    updated_entry.setDirty();
    modules_store.set(updated_entry);
    modules_store.sync();
    Here the value of "module" key is 3,7,8

  4. #4
    Sencha User
    Join Date
    Aug 2012
    Posts
    36
    Vote Rating
    1
    Answers
    1
    arael78 is on a distinguished road

      0  

    Default


    Well. I was wrong. Every setDirty() and set() function after the sync duplicates values of the 'course' key and I don't know how to solve this problem.

    What is the proper way to update the localestorage without having duplicates?

    Every time I use sync() my storage ends up with duplicated values. My id keys are defined as integers and the values returned by getData() are integer but if I check in the web inspector the raw.id is a string. Is this normal?

  5. #5
    Sencha User
    Join Date
    Aug 2012
    Posts
    36
    Vote Rating
    1
    Answers
    1
    arael78 is on a distinguished road

      0  

    Default


    Anyone has suggestions on how to solve this problem? =(

  6. #6
    Sencha Premium Member
    Join Date
    Jul 2012
    Posts
    24
    Vote Rating
    1
    alile is on a distinguished road

      0  

    Default


    I am having an identical issue (chrome, touch 2.1). I have a 'filter' field in my model that I am updating (so the user can flag items as 'saved'). When I sync (to local storage), the id fields in the chrome inspector are duplicated, but at no point does the getCount() call return other than the correct number.

    A possibly important point - If I reload the page, without making any adjustments, and then use as directed I do not get the duplicating behavior. Possibly I have some sort of object persistence as I am loading in data from the server, duplicating the records to a local store, then unregistering the remote store. As I sync on unload, if I have not made any changes to the records, then there is no issue. But if I make changes, perhaps there is some lingering reference and that is what is getting stored. I tried to do a sync() and then a load() on the local store to replicate the reload behavior, to no avail. Merge/reload code below.

    Note - I tried removing the setDirty() call, and the sync() saved nothing. So I am loading in the data from local storage the second time, but no way of knowing what is going on in memory.

    - added removeAll() in an attempt to clean the data further. It does not fix anything, but it does prove that I am pulling from local storage only (updating the display without the 2nd load shows nothing and getCount() == 0).

    PHP Code:
    Ext.getStore("ServerNotificationStore").each(function(record)    
    {
            if(
    record.get("publishDate") != undefined)        
            {            
    this.getStore().insert(0,record);            
                          
    record.setDirty(true);
            }    
    }, 
    this);

    Ext.StoreManager.unregister("ServerNotificationStore");
    this.getStore().sync();
    this.getStore().removeAll();   
    this.getStore().on("load"this.handleStoreReloadedthis);    
    this.getStore().load(); 
    Last edited by alile; 3 Jan 2013 at 3:02 PM. Reason: added removeAll

Tags for this Thread