Titanium Community Questions & Answer Archive

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

Best way to combine persistent and dynamic data in a sports app

Hi all,

I'm building an iPhone app for a sports team. One of the features is the schedule for the upcoming season.

What would be the best way to implement the games schedule so that past game scores would remain on the phone and that only new data would be downloaded?

The list of games would also have to be dynamic so that play-off games could be added on the fly.

Thank you for your tips in advance.

— asked September 11th 2010 by Antti K.
  • data
  • iphone
  • local
0 Comments

3 Answers

  • I do something similar for the app I have shipping now. It contains a local database, and once every few hours it checks a server to see if it should download new ones to add to the database.

    The server returns an XML file that has elements full of data to be stored or updated locally (if any exist). It also gives the client a hint as to when the data will be updated again (nextupdate timestamp).

    Integer properties are used to store the last time the db was checked for update. I've trimmed the code to make it readable, hope it helps.

    var currentTS = parseInt(((new Date().getTime()) / 1000),10);
    var xhrURL = 'http://example.com/feed.php';
    
    // User to determine if cache is out of date
    function checkNeedsUpdating(nowTS) {
        var retval = false;
        var lastUpdatedTS = Titanium.App.Properties.getInt('lastUpdatedTS');
        var nextCheckTS = Titanium.App.Properties.getInt('nextCheckTS');
        var timeDiff = nowTS - lastUpdatedTS;
        if (timeDiff > 86400 || timeDiff < 0 || (nowTS > nextCheckTS)) {
            retval = true;
        } 
        return retval;
    };
    
    // load the cache contents from remote and store locally
    function updateCache(remoteURL,nowTS) {
        var xhr = Titanium.Network.createHTTPClient();
    
        xhr.onload = function() {
            try {
                var doc = Ti.XML.parseString(this.responseData.text);
                // note status is an actual XML element returned, it is not part of the protocol itself
                if (doc.getElementsByTagName("status").item(0).text=='OK') {
                    var nextCheckTS=doc.getElementsByTagName("nextupdate").item(0).text;
                    var items = doc.getElementsByTagName("item");
                    var db = Titanium.Database.open('localDB');
                    for (var c=0;c<items.length;c++)
                    {
                        var item = items.item(c);
                        var id = item.getElementsByTagName("id").item(0).text;
                        var title = item.getElementsByTagName("title").item(0).text;
                        db.execute('REPLACE INTO table (id, title) VALUES (?,?)', id, title);
                    }
                    db.close();
                    if (items.length > 0) {
                        Titanium.App.Properties.setInt('lastUpdatedTS',nowTS);
                    }
                    Titanium.App.Properties.setInt('nextCheckTS',nextCheckTS);
                }
            }
            catch(e) {
            }
        };
    
        xhr.onerror = function(e) {
        };
    
        // open the client and get the data
        var lastUpdatedTS = Titanium.App.Properties.getInt('lastUpdatedTS');
        remoteURL += "?since=" + lastUpdatedTS;
        xhr.setTimeout(15000);
        xhr.open('GET',remoteURL);
        xhr.setRequestHeader('User-Agent','Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420+ (KHTML, like Gecko) Version/3.0 Mobile/1A537a Safari/419.3');
        xhr.send(); 
    };
    
    // see if database should be refreshed
    if (checkNeedsUpdating(currentTS) && (Titanium.Network.networkType != Titanium.Network.NETWORK_NONE)) {
        updateCache(xhrURL,currentTS);
    }
    
    — answered September 12th 2010 by Todd Trann
    permalink
    1 Comment
    • Hey Todd, looks cool! Maybe you can give us a small sample of your feed.php ? Thx :-)

      — commented October 13th 2010 by Seb S.
  • A few questions, are the scores being
    1) entered and saved locally on the phone or
    2) coming from a server?

    If 1, then simplest might be a local SQLite database
    if 2, then either create a rss feed of your results and have the phone download that file each time the window opens and pass the info int a tableview.

    Hope it gives you some ideas.

    Wayne

    — answered September 11th 2010 by Wayne Buchner
    permalink
    1 Comment
    • @Wayne

      The results would be coming from a server. Because the results of the past games don't change, I'd like to figure out a way to save them on the device, because even if it's not a lot of data, I wouldn't like to be downloading the file over and over again.

      — commented September 12th 2010 by Antti K.
  • A few questions, are the scores being
    1) entered and saved locally on the phone or
    2) coming from a server?

    If 1, then simplest might be a local SQLite database
    if 2, then either create a rss feed of your results and have the phone download that file each time the window opens and pass the info int a tableview.

    Hope it gives you some ideas.

    Wayne

    — answered September 11th 2010 by Wayne Buchner
    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.