1. #1
    Sencha User
    Join Date
    Jun 2012
    Posts
    129
    Vote Rating
    0
    Answers
    7
    munder is on a distinguished road

      0  

    Default Answered: How to detect when a record copying operation completes?

    Answered: How to detect when a record copying operation completes?


    My app downloads data from a web service, which it then needs to copy into an offline store. The problem is that it progresses to the next stage before the copying operation completes and then tries to do something with records that don't exist in the local (SQLite) store..
    I tried this:
    Code:
    optionsOfflineStore.setData(optionsStore.getData());
    (where optionsOfflineStore is a variable pointing to the offline store and optionsStore refers to the store for data pulled from the web server), but maybe one or two records only get copied.
    So I tried this:
    Code:
            optionsStore.each(function (record) {
                optionsOfflineStore.add(record);
            });
    Same problem.
    It's only if I insert a statement to log output to the console that I can be sure the operation will complete:
    Code:
            optionsStore.each(function (record) {
                console.log('saving option locally');
                optionsOfflineStore.add(record);
            });
    I would expect copying 1500 records to take a while, but that console.log statement unsurprisingly consumes far more time than is acceptable, so I can't really use it as a kludge to make things work.
    How can I detect when all copy operations have completed?

  2. unless you need to wait for a sync operation, javascript is a sync language so you can know when it's done via after the each() call.

  3. #2
    Sencha - Senior Forum Manager mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    St. Louis, MO
    Posts
    33,714
    Vote Rating
    438
    Answers
    3113
    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


    unless you need to wait for a sync operation, javascript is a sync language so you can know when it's done via after the each() call.
    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.

  4. #3
    Sencha User
    Join Date
    Jun 2012
    Posts
    129
    Vote Rating
    0
    Answers
    7
    munder is on a distinguished road

      0  

    Default


    Thank you, Mitchell, but I'm afraid I haven't a clue what you mean. Old age, I think. What I did discover was that if I sync()ed the store within the loop after each add() operation, it worked OK. I had assumed that it was sufficient to sync() after the loop had done its stuff. Sorry to have wasted your time, and thank you for taking the trouble to reply.

  5. #4
    Sencha User
    Join Date
    Jun 2012
    Posts
    129
    Vote Rating
    0
    Answers
    7
    munder is on a distinguished road

      0  

    Default


    No, actually, I discovered that calling sync() within the loop after each copy operation didn't work, as I ended up with zillions of duplicate records. So I still need to know when the sync() operation completes. In the docs, I can only find a beforesync event. I need something like an aftersync. I seem to be truly stuck.

  6. #5
    Sencha User
    Join Date
    Jan 2011
    Posts
    99
    Vote Rating
    5
    Answers
    5
    JRS is on a distinguished road

      0  

    Default Here is how I do it

    Here is how I do it


    1. Delete everything from localstore
    2. sync(localstore)
    3. Then add everything from onlinestore - I am using the
    localStore.add(record.data)
    4. sync(localStore)

    I noticed you are using setData and I am using add.
    I have no idea of the difference or why - I just know it works - with Sencha - when something works - I thank god and pray it doesn't break - I have very little idea on how to fix something.

    All sencha documentation and comments are directed to experts!

    Hope this helps
    Regards

  7. #6
    Sencha User
    Join Date
    Jun 2012
    Posts
    129
    Vote Rating
    0
    Answers
    7
    munder is on a distinguished road

      0  

    Default


    Thanks, JRS. I am now using add in a loop. It looked to me as if x.setData(y.getData()) should do it in one lump, but apparently not. I have started to make some little progress, though I must admit I'm not quite sure how. It seems I have something working one day and it breaks the next. I assume that's my fault and not ST's, but it doesn't feel like it. As for the documentation, well, I've taken to reading the Critique of Pure Reason in the evenings as light relief from ST's docs.
    Thank you for your suggestion. I hope nothing breaks in your app.

  8. #7
    Sencha User
    Join Date
    Jun 2012
    Posts
    129
    Vote Rating
    0
    Answers
    7
    munder is on a distinguished road

      0  

    Default


    Incidentally, JRS, I am building an app to conduct surveys. Once the user has answered all their questions, they may choose to review/edit their answers. If they do, the app downloads their questions and their answers and moves on to the next page, which shows the questions with their answers. The problem seemed to be that the answers weren't fully available before that next page became visible and its controller started doing its stuff. Wrapping the transition to that page (i.e. hiding the current one and showing that next one) in
    Code:
    Ext.Function.defer(function(){
    //hide and show the pages here
    }, 1000);
    seems to have solved the problem, though it seems a bit naff to me. I'd rather the sync() on the store fired some kind of trappable event.
    Anyway, I mentioned it because I thought it might be of some use to you. It'll probably break tomorrow.