Data Persistence

Richard Gaskin ambassador at fourthworld.com
Fri Jul 27 15:26:06 EDT 2018


Sannyasin Brahmanathaswami wrote:

 >   (rough draft code)
 >
 > Local sTempParamArray # may good to save a local,
 >
 > Command saveTempParams
 >
 > 		Put fld "fuelNeed" into  pTempParamArray["fuelNeeded'}
 > 		Put fld "timeToArrival" into  pTempParamArray["timeToArrival""
 > 		# etc.
 > 		Put pTempParamArray into sTempParamArray
 > 		storeTempParams pTempParamArray
 >
 > End saveTempParams
 >
 > command storeTempParams pTempParamArray
 >
 > 	put   specialFolderPath("documents") & "/tempSavedParams" into 
tSavedPath
 > 	put sTempParamArray into url ("binfile:" & tSavedPath)
 >
 > end storeTempParams

A good outline, but missing a key step:  arrays are collections of 
pointers and in their native form are not a contiguous bytestream, so 
attempting to write one to disk directly with yield an empty file.

This is where serialization comes in, collecting all the disparate 
pointer contents and packing them into a bytestream that can be saved or 
transported, using arrayEncode:

command storeTempParams pTempParamArray
   put specialFolderPath("documents") & "/tempSavedParams" into \
      tSavedPath
   put arrayEncode(sTempParamArray) into url ("binfile:" & tSavedPath)
end storeTempParams


And because arrayDecode will throw a fatal error if you attempt to use 
it with anything other than actual LSON data, we can't just check "the 
result" afterward; better to wrap it in a try/catch:

Function getTempSavedParams
   Put specialFolderPath("documents") & "/tempSavedParams" into \
      tSavedPath
   put url ("binfile:" & tSavedPath) into pTempParamArray
   -- Always check I/O for errors:
   if the result is empty then
      return "Error in getTempSavedParams: "& the result for error
    end if
    -- Try as LSON:
    try
      put arrayDecode(pTempParamArray) into pTempParamArray
    catch tErr
      return "Not a valid array file: "& tSavedPath for error
    end try
    --
    return pTempParamArray
end getTempSavedParams


-- 
  Richard Gaskin
  Fourth World Systems
  Software Design and Development for the Desktop, Mobile, and the Web
  ____________________________________________________________________
  Ambassador at FourthWorld.com                http://www.FourthWorld.com




More information about the use-livecode mailing list