One of my student is working in with intermittent internet - he wonders if the above functions can return a error code if, for example, the connection timed out?
The documentation doesn’t specify anyway for an error to be returned. I did the experiment of what happens when you lose internet for a while and it looks like the callback function just waits, potentially forever, for the connection to return. When it does reconnect any callback functions waiting to execute run in rapid succession. I didn’t check to see if they run in a strict order.
@i15904503668i You can receive a timeout function in two ways:
- If there is a timeout function written in the callback function then the process will timeout when the timer is over. That is a safe guard to prevent the program from waiting for a response from the data indefinitely.
- The browser could hang and give an unresponsive error. That largely depends on how the callback is written.
Otherwise the program will just wait until the server/data responds with the requested information.
Yeah that appears to be the current situation - said request just waits, indefinitely. Also there’s no apparent timeout functionality built-in - one way my student used is to rapidly check (say, every 1s) a list which readRecords should read, and if it’s empty for too many times then there’s a timeout - but this only works for readRecords, not update/delete/create.
Hey thanks for the reply!
I did some experiment with setTimeOut() and clearTimeOut(), but according to the documentation they are supposed to be used as a countdown timer (execute the code after the timer expires) rather than an alarm clock (execute the code then terminate it at the end).
If possible, could you please show me how such a timeout function can be written? The said student already submitted his AP stuff, so it should be okay.
@i15904503668i The documentation you found is exactly how a timeout function works. It doesn’t operate as an alarm clock.
Please remember that students currently working on the Create Performance Task according to the AP CS Principles Course and Exam Description (CED) cannot receive debugging help from teachers. They can from their peers through collaboration.
Page 83:
Teachers may not:
- assign, provide, or distribute to students specific topics or a program students are todevelop;
- write, revise, amend, or correct student work, including debugging the program, writing or designing functionality in the program, testing the program, or making revisions to the program;
- allow students to submit computational artifacts from practice performance task as a submission for AP assessment scoring;
- suggest answers or provide feedback on answers to prompts; or
- allow students to collaborate during the creation of their video or completing written responses.
Welp his not working on CPT anymore - he submitted it weeks ago - I can confirm from my AP portal that he did submit all tasks. He’s continuing coding because he likes it . But sure, I’ll tell him to figure out on his own.
He solved it - I have his permission to share some of his thought and find on mitigating rough connections.
Basically, if update/delete/create fails to work, so won’t readRecord, vice versa. He created something like this, and it worked.
In a nutshell, if the program can’t retrieve the small test record, it can’t possibly complete any update/create/delete task. If the program detects such error, it’s better to fail the program gracefully and prompt the user immediately.
function firstUpdate(){
checkConnection();
updateRecord(//insert your stuff);
}
Var tempList = 0;
Function checkConnection(){
readRecord("create a short test record here", {},function(value){
tempList = value;
});
timedLoop(1000,checkTest);
}
function checkTest(){
if (tempList === 0){
//there is an error! Don't worry, it will recheck a second later!
}else{
//there is no error! don't forget to -
tempList = 0;
stopTimedLoop();
}}
There are, however, no conceivable way (at least to him) to make update/delete/create return a status. It’s not something in the API. This is the closest he can get.
I see what he is trying to do there. I would process the records in the callback as normal and set a flag to true when that happens. You can start the timedLoop
to check that flag and report when the records are processed or warn when they have not.
One problem is if your user starts pressing buttons wildly. That is not an absurd assumption. You might then need an array of flags adding one every time a callback is passed to the database interface. You can create the callback function adding a unique index to each one with a closure.
Indeed - in his program, sync is triggered by user actions - this includes certain buttons. He wrote something more than the code above - he added a counter that will get a +1 every fail (and reset after success), and will only prompt the user when that counter is above, say 4.
Since the test list is extremely small (just a single row), it should take just 1 loop (at most 2) to finish the operation.
Another way he used to mitigate fast clickers is to add manual delays - he’s pretty good at UX
Here is what I would do if internet connections become a problem.
The pertinent section is:
var connections,waiting;
connections = 0;
waiting = false;
function startConnection () {
connections++;
if (!waiting) {
waiting = true;
timedLoop(500,waitingForConnection);
}
}
function endConnection () {
connections--;
if (connections === 0) {
waiting = false;
stopTimedLoop();
hideWaitMessage();
}
}
function waitingForConnection () {
showWaitMessage();
}
Call startConnection();
before every database action. Then in the callback call endConnection();
. The only additional thing is to add showWaitMessage
and hideWaitMessage
to pop up an appropriate label or screen.
This looks interesting, it’s like the “please don’t turn off your game when you see the autosave icon” part of a game. I’ll hand this to my student sometime after the submission deadline of AP.
Thanks for your help! Btw do you happens to know why/how a work can violate code.org TOS? Is it copyright related?
I just got word that the project is unblocked. It could have been something as simple as naming a database column “name”.
Wow I remember a couple of my student used “name” as column… Just checked, yep. Intriguing why naming it “name” is unacceptable, but I’ll tell em to change the name (quite literally, sry for the bad joke) if they want to publish their project…