Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

Load Data for TableView from JSON (Saved in File)

If a user is offline, I'd like to load tableview data from a cached file (saved to the filesystem the last time the user was online and fetched the data via XHR).

I use an array of rows (each row has 3 labels) as the data for the tableview. How can I serialize this array to a string, store it in a file, and later pull it out and use it to populate the data for a tableview?

This does not work:


//Save to file
var json = JSON.stringify(arrayTableData);
var fileName = 'test.json';
var file = Titanium.Filesystem.getFile(Titanium.Filesystem.resourcesDirectory, fileName);

file.write(json);

//Load from file
var fileName = 'test.json';
var file = Titanium.Filesystem.getFile(Titanium.Filesystem.resourcesDirectory, fileName);

if (file.exists()) {
  var json = file.read();
  tableView.setData(JSON.parse(json));
}
— asked June 28th 2010 by Slim McKinsley
  • data
  • file
  • filesystem
  • json
  • setdata
  • tableview
2 Comments
  • An array of TableViewRows is not JSON serializable.

    — commented June 28th 2010 by Alan McConnell
  • @Alan Thanks, I figured based on my testing but it's nice to confirm. So it seems like the only way to achieve this is to save the raw XML to the file and then when I pull it out the file I have to do all of the createTableViewRow processing again, correct? I was hoping that by caching to a file I could avoid having to do all of this processing again but it looks like that's not possible.

    — commented June 28th 2010 by Slim McKinsley

1 Answer

  • Hi Slim McKinsley,
    I'd love to know how is the possibility of your question :)
    You want share your piece of that code please?

    Thanks a lot!!!

    — answered September 8th 2010 by Nuno Costa
    permalink
    0 Comments
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.