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

Working with SQLite - how can I crease my app performance

$
0
0

In my application, there is a table called customers, so it has a corresponding Model that looks like this:

exports.definition = {
    config: {
        "columns": {
            "CustomerId":"INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE",
            "MagentoId":"INTEGER",
            "GroupId":"INTEGER",
            "DefaultBillingId":"INTEGER",
            "DefaultShippingId":"INTEGER",
            "Email":"TEXT",
            "Prefix":"TEXT",
            "Firstname":"TEXT",
            "Middlename":"TEXT",
            "Lastname":"TEXT",
            "Suffix":"TEXT",
            "Gender":"TEXT",
            "DateOfBirth":"TEXT",
            "TaxVATNumber":"TEXT",
            "CustomerAttributes":"TEXT", // Json Encoded String of Array ('Key' => 'Value') Pair
            "CreatedFrom":"TEXT",
            "CreatedAt":"TEXT",
            "UpdatedAt":"TEXT"
        },
        "adapter": {
            "type":"sql",
            "collection_name":"customers",
            "db_name":"MyApp",
            "idAttribute":"CustomerId"
        }
    },
    extendModel : function(Model) {
        _.extend(Model.prototype, {
        });
        return Model;
    },
    extendCollection : function(Collection) {
        _.extend(Collection.prototype, {
            executeQuery : function(sqlStatement) {
                db = Ti.Database.open(this.config.adapter.db_name);
                db.execute(sqlStatement);
                db.close(); db = null;
                this.trigger('sync');
            },
            deleteAll : function() {
                db = Ti.Database.open(this.config.adapter.db_name);
                db.execute('DELETE FROM '+ this.config.adapter.collection_name);
                db.execute('DELETE FROM sqlite_sequence where name = \''+ this.config.adapter.collection_name +'\'');
                db.close(); db = null;
                this.trigger('sync');
            }
        });
        return Collection;
    }
};
We then load the model collection like this:
var customerCollection = Alloy.Collections.instance("customer");
Then we query an external api to load a list of customers in JSON format and sync each customer like this:
// Decode Customer Info
var apiCustomer = JSON.parse(customerInfo.Data);
 
// Load Customer From Collection
customerCollection.fetch({
    query: {
        statement: 'SELECT * FROM customers WHERE Email = ?',
        params: [apiCustomer.Email]
    }
});
var dbCustomer = customerCollection.at(0);
 
// Define Customer Data From API Response
var apiCustomerData = {
    MagentoId:          apiCustomer.MagentoId,
    GroupId:            apiCustomer.GroupId,
    DefaultBillingId:   apiCustomer.DefaultBillingId,
    DefaultShippingId:  apiCustomer.DefaultShippingId,
    Email:              apiCustomer.Email,
    Prefix:             apiCustomer.Prefix,
    Firstname:          apiCustomer.Firstname,
    Middlename:         apiCustomer.Middlename,
    Lastname:           apiCustomer.Lastname,
    Suffix:             apiCustomer.Suffix,
    Gender:             apiCustomer.Gender,
    DateOfBirth:        apiCustomer.DateOfBirth,
    TaxVATNumber:       apiCustomer.TaxVATNumber,
    CustomerAttributes: apiCustomer.CustomerAttributes,
    CreatedFrom:        apiCustomer.CreatedFrom,
    CreatedAt:          apiCustomer.CreatedAt,
    UpdatedAt:          apiCustomer.UpdatedAt
};
 
// If Customer Already Exists
if (dbCustomer)
{
    // Update Customer In Local DB
    dbCustomer.set(apiCustomerData);
    dbCustomer.save();
    dbCustomer = dbCustomer.toJSON();
    console.log('Existing Customer #'+ dbCustomer.CustomerId +' ['+ dbCustomer.Firstname +' '+ dbCustomer.Lastname  +'] Updated In Local DB');
}
else
{
    // Delete Any Previous Records For This Account
    customerCollection.executeQuery("DELETE FROM customers WHERE MagentoId = "+ apiCustomer.MagentoId);
 
    // Create New Customer In Local DB
    dbCustomer = Alloy.createModel('customer', apiCustomerData);
    dbCustomer.save();
    console.log('New Customer ['+ apiCustomer.Firstname +' '+ apiCustomer.Lastname  +'] Created In Local DB');
}
This works fine, except it's quite slow, sometimes it can take upto 1 ~ 2 second per customer record insert. Any ideas where is the bottleneck in my app? How can I speed up my application?

Viewing all articles
Browse latest Browse all 8068

Trending Articles



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