-
16 Feb 2013 6:53 AM #1
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:
(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.Code:optionsOfflineStore.setData(optionsStore.getData());
So I tried this:
Same problem.Code:optionsStore.each(function (record) { optionsOfflineStore.add(record); });
It's only if I insert a statement to log output to the console that I can be sure the operation will complete:
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.Code:optionsStore.each(function (record) { console.log('saving option locally'); optionsOfflineStore.add(record); });
How can I detect when all copy operations have completed?
-
Best Answer Posted by mitchellsimoens
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.
-
18 Feb 2013 9:20 AM #2Sencha - Senior Forum Manager
- Join Date
- Mar 2007
- Location
- St. Louis, MO
- Posts
- 33,714
- Vote Rating
- 438
- Answers
- 3113
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.
-
18 Feb 2013 10:11 AM #3
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.
-
19 Feb 2013 11:20 AM #4
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.
-
20 Feb 2013 10:02 AM #5
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
-
20 Feb 2013 10:25 AM #6
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.
-
20 Feb 2013 5:34 PM #7
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
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.Code:Ext.Function.defer(function(){ //hide and show the pages here }, 1000);
Anyway, I mentioned it because I thought it might be of some use to you. It'll probably break tomorrow.


Reply With Quote