Quantcast
Channel: Appcelerator Developer Center Q&A Unanswered Questions 20
Viewing all articles
Browse latest Browse all 8068

Alloy Model with SQLite not persisting

$
0
0

Hi,

I'm currently building an Alloy application using Titanium Studio, build: 3.4.1.201410281727 and using SQLite for DB storage. I'm having some trouble with saving the data through Alloy model.

This is my model declaration

exports.definition = {
    config: {
 
        adapter: {
            type: "sql",
            collection_name: "IntegrationUser",
            db_file: "/db.sqlite",
            db_name: "dbname",
            idAttribute: "id",
            remoteBackup: false
        }
    }
};
 
SQL Create Statement
CREATE TABLE "IntegrationUser" (
    "id" INTEGER PRIMARY KEY ,
    "accessTokenId" TEXT DEFAULT (null) ,
    "tokenType" TEXT,
    "accessToken" TEXT,
    "instanceUrl" TEXT,
    "issuedAt" TEXT)
And I declare the collection in alloy.js
Alloy.Collections.IntegrationUser = Alloy.createCollection('IntegrationUser');
I send a GET request to a remote server to retrieve the data and save the data to model.
var httpClient = Titanium.Network.createHTTPClient({
    timeout : 1000000,
    onerror: function(e) {
        Ti.API.error('Bad Server =>'+e.error);
    },
    onload: function(e) {
        if(this.status == '200'){
            Ti.API.info('got my response, http status code ' + this.status);
 
                if(this.readyState == 4){               
                    var response = JSON.parse(this.responseText);
 
                    var intUserCollection = Alloy.Collections.User;
                    var userArgs = {
                        "id" : 1,
                        "accessTokenId" : response.id,
                        "tokenType" : response.token_type,
                        "accessToken" : response.access_token,
                        "issuedAt" : response.issued_at,
                        "instanceUrl" : response.instance_url
                    };
 
                    var intUser = Alloy.createModel("IntegrationUser", userArgs);
                intUserCollection.add(intUser);
                intUser.save();
 
                } else {
                    Ti.API.error('HTTP Ready State != 4');
                }           
        } else {
                Ti.API.error('HTTP Error Response Status Code = ' + this.status);
                Ti.API.error("Error =>" + this.response);
        }
    }
});
The problem is the data does not save to the SQLite DB. After I restart the app, the data is not persisted and the db appears to be empty.

Am I doing something wrong with the model?


Viewing all articles
Browse latest Browse all 8068

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>