Jump to content

Final processing code placement


Recommended Posts

I have been trying to write out data to storage when the form closes or the application terminates and have NOT been successful.

 

I first tried the Form's

 

 

procedure TForm1.FinalizeObject; 
begin 
  inherited; 
  SaveData; 
end; 

procedure TForm1.FinalizeObject; 
begin 
 SaveData;  
 inherited; 
end; 

 

neither of these attempts worked, therefore I rrearranged my code and tried it from the project\'s unit

 

 
procedure TApplication.ApplicationClosing; 
begin 
 SaveData; 
 inherited; 
end; 

procedure TApplication.ApplicationClosing; 
begin 
 inherited; 
 SaveData; 
end; 

 

I have a w3_showmessage as the first line of SaveData, and it never gets called ever

 

What am i doing wrong? Thanks

 

Shane

Link to post
Share on other sites
  • Administrators

I couldn't figure this out either :-(

 

I think Eric Grange saves data on application exit in the MissileCommand demo. (By overriding the ApplicationClosing method).

 

I'll tell Lennart to have a look at this. In the meantime, you could ask the question at stackoverflow.com and tag it with smart-mobile-studio. I know there are a few experienced users who pay attention to SO rather than this forum...

(http://stackoverflow.com/questions/tagged/smart-mobile-studio).

 

 

 

 

I'll tell Lennart to have a look at this

Link to post
Share on other sites

This should do the trick :)

 

 
procedure TForm1.InitializeObject; 
var 
  mWindow:  THandle; 
begin 
  inherited; 
  {$I 'Form1:impl'} 

  // Get the browser window handle 
  mWindow:=w3system.BrowserAPI.getWindow; 

  // attach to the event we need 
  mWindow.onbeforeunload:=@HandleBrowserQuit; 
end; 

procedure TForm1.FinalizeObject; 
begin 
  // Save data 
  SaveData; 
  inherited; 
end; 

Procedure TForm1.HandleBrowserQuit; 
Begin 
  // Invoke recursive shutdown procedure 
  if not application.Terminated then 
  application.Terminate; 
end; 

Procedure TForm1.SaveData; 
Begin 
  writeln('Saving data'); 
end; 

Link to post
Share on other sites

Had a look at it today, and it seems the browser returns a variant rather than a string from the "Storage.getKeyStr" method. So when you test for a '' then the boolean logic fails. By altering it to:

 

 
procedure TForm1.LoadScores; 
var 
  mTemp:  variant; 
begin 
 mTemp:= TApplication(Application).Storage.getKeyStr('HighScores',''); 
 if varIsValidRef(mTemp) then 
 begin 
  writeln('Found data'); 
  asm 
  (@HighScoresRec) = JSON.parse(@mTemp); 
  end; 
  if HighScoresRec.fKey <> $CAFEBABE then 
  w3_showmessage('Corrupt data') else 
  writeln(HighScoresRec); 
 end; 
end; 

 

It works fine.

 

Link to post
Share on other sites

I get an error

 

Syntax Error: Unexpected end of of inout

 

I found that it is this line:

 

 
(@HighScoresRec) = JSON.parse(@mTemp); 

 

that causes it.

 

I originally thought i needed to clear the cache, so i did that, and it didn't work.

 

I even changed names from hightscores

 

 
mTemp := TApplication(Application).Storage.getKeyStr('HighScores',''); 

to test

 

 
mTemp :=  TApplication(Application).Storage.getKeyStr('Test',''); 

still get the error

 

Shane

Link to post
Share on other sites

also, aside from the error i receive, i still can't get the project to run my finalization code.

 

whether I do this

 

 
procedure TForm1.FinalizeObject; 
begin 
  SaveScores; 
  inherited; 
end; 

or

 

 
procedure TForm1.FinalizeObject; 
begin 
  inherited; 
  SaveScores; 
end; 

Link to post
Share on other sites

There is a HTML rule that no dialogs are allowed during the "close" events. If you switch to using writeln() only, run the project in safari - and activate the developer tools and log window -- i think that will help.

I got it to work perfectly here in both chrome and safari, but you must omit the showmessage stuff.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...