1. #61
    Sencha User
    Join Date
    Jan 2012
    Location
    London, UK
    Posts
    497
    Vote Rating
    51
    shepsii has a spectacular aura about shepsii has a spectacular aura about shepsii has a spectacular aura about

      0  

    Default


    I think this has something to do with your use of removeAll. A call to this function alone doesn't remove all the records from the database - you need to sync to do that, and that itself would need you to listen for when that first sync has finished.

    You shouldn't need two models for both the stores, my code is fine with just one.

  2. #62
    Sencha User
    Join Date
    May 2012
    Location
    Glasgow, UK
    Posts
    17
    Vote Rating
    0
    henry.wilson is on a distinguished road

      0  

    Default


    I don't think removeAll is the issue - I tried taking that line out, clearing the DB manually using SQLiteStudio and then running the sync again, and I get the same result - no data is viewable through the ClaimStore in the app, but if you check the DB manually the records have all been saved.

    When you say "You shouldn't need two models for both the stores" are you referring to the use of a different store to retrieve the data from the web service? I'm doing this deliberately because I only want the web service call to be made when an explicit data sync is requested by the user - the rest of the time the app should just go to the 'offline' store (i.e. the SQLite DB ). Not sure how you'd do this without two stores, unless you were to handle the web service call manually rather than using a store with a proxy to handle it.

    Still stumped as to why I've got data in the DB that isn't showing up in the app

  3. #63
    Sencha User
    Join Date
    Jan 2012
    Location
    London, UK
    Posts
    497
    Vote Rating
    51
    shepsii has a spectacular aura about shepsii has a spectacular aura about shepsii has a spectacular aura about

      0  

    Default


    It's really important to distinguish between "model" and "record" - a record is an instance of a model.

    You don't need to define two different models here.

    You may need two different records. I'd recommend using record.copy() when you get it from the first store's load.

    I'd try this as well when you know there's data in the db, and without all the syncing code, ie just try to start the app and straight away load data into the sqlite store. Do you have any luck then?

  4. #64
    Sencha Premium Member
    Join Date
    May 2012
    Posts
    1
    Vote Rating
    0
    henry888 is on a distinguished road

      0  

    Default


    [QUOTE=shepsii;804577]Ok so back to this...
    I have to everytime use 'sqlitedemo' as my database name and then do a find and replace on the generated file.


    Every time, I want to change the database name, I had to delete Databases.db and file_0 in ~/Library/Application Support/Ofi Labs/PhantomJS on my mac, then do the build again.

  5. #65
    Sencha User
    Join Date
    Mar 2012
    Posts
    28
    Vote Rating
    0
    gcw07 is on a distinguished road

      0  

    Default


    Henry, I'm doing something similar to you in my app. I copy all the data from an online source to the local sqlite database for offline usage. Whether I'm doing things correctly by the book or not, I'm not sure, but it does work for me.

    Stores
    Code:
    /**
     * @class People
     * @extends Ext.data.Store
     */
    Ext.define('App.store.People', {
        extend: 'Ext.data.Store',
    
        config: {
            model: 'App.model.Person',
            autoLoad: false,
            pageSize: 1000,
    
            sorters: [
                {
                    property: 'lastName',
                    direction: 'ASC'
                }
            ],
    
            //group the store using the first letter of the lastName field
            grouper: function(record) {
                return record.get('lastName')[0];
            },
        }
    });
    Code:
    /**
     * @class PeopleOnline
     * @extends Ext.data.Store
     */
    Ext.define('App.store.PeopleOnline', {
        extend: 'Ext.data.Store',
    
        requires: [
            'Ext.data.proxy.JsonP',
        ],
    
        config: {
            model: 'App.model.PersonOnline',
            autoLoad: false,
    
            //sort the store using the lastName field
            sorters: [
                {
                    property: 'lastName',
                    direction: 'ASC'
                }
            ],
    
            //group the store using the first letter of the lastName field
            grouper: function(record) {
                return record.get('lastName')[0];
            },
    
            proxy: {
                type: 'jsonp',
                url: 'http://www.url.com/api/people',
    
                startParam: '',
                pageParam: '',
                limitParam: '',
    
                extraParams: {
                    userId: false,
                    appKey: false,
                },
    
                reader: {
                    type: 'json',
                    rootProperty: 'people.items'
                }
            }
        }
    });
    Models
    Code:
    /**
     * @class Person
     * @extends Ext.data.Model
     */
    Ext.define('App.model.Person', {
        extend: 'Ext.data.Model',
    
        config: {
            //idProperty: 'id', // uniqueid // if we have field with name as id, conflicts happens with default idProperty(id) which always have value as ext-record-x
            clientIdProperty: 'userId',
    
            fields: [
                {name: 'userId',    type: 'int', fieldOption: 'PRIMARY KEY'},
                {name: 'firstName', type: 'string'},
                {name: 'lastName',  type: 'string'},
                {name: 'code',      type: 'int'}
            ],
    
            proxy: {
                type: 'sqlitestorage',
                dbConfig: {
                    tablename: 'people',
                    dbConn: App.util.InitSQLite.getConnection()
                    //dbConn: Ext.DbConnection
                },
                reader: {
                    type: 'array'
                }
            }
        },
        writer: {
            type: 'array'
        }
    });
    Code:
    /**
     * @class PersonOnline
     * @extends Ext.data.Model
     */
    Ext.define('App.model.PersonOnline', {
        extend: 'Ext.data.Model',
    
        config: {
            fields: [
                {name: 'userId',    type: 'int'},
                {name: 'firstName', type: 'string'},
                {name: 'lastName',  type: 'string'},
                {name: 'code',      type: 'int'}
            ],
        }
    });
    Sync Function
    Code:
    /**
     * Get the people data from the server
     */
        syncData: function() {
                var onlinePeopleStore = Ext.getStore('PeopleOnline');    
                var peopleStore       = Ext.getStore('People');
    
                        // Load the online store and then copy to offline
                        onlinePeopleStore.load({
                            callback: function(records, operation, success) {
                                // Clear out the offline people list
                                peopleStore.getProxy().truncate('people');
    
                                onlinePeopleStore.each(function(record) {
                                    var rec = Ext.create('App.model.Person', {userId: record.get('userId'), firstName: record.get('firstName'), lastName: record.get('lastName'), code: record.get('code')});
                                    rec.save();
                                });
                                peopleStore.load(); // Reload store
                            },
                            scope: this
                        });
        },
    That works for me. Now the saving 1 at a time part when you copy from the online to the offline is wrong (it should be batched), but I haven't had time to update it since I switched to the new advanced version of the sqlite proxy. Hope this helps some.

  6. #66
    Sencha User
    Join Date
    May 2012
    Location
    Glasgow, UK
    Posts
    17
    Vote Rating
    0
    henry.wilson is on a distinguished road

      0  

    Default


    shepsii, gcw07, thank you both for the advice - I'll have a play about and see if I can get the code to work correctly. It's good to see such a great community for Sencha, I really appreciate it!

  7. #67
    Sencha User
    Join Date
    Jan 2012
    Location
    London, UK
    Posts
    497
    Vote Rating
    51
    shepsii has a spectacular aura about shepsii has a spectacular aura about shepsii has a spectacular aura about

      0  

    Default


    Let us know how you get on Henry.

    Everyone who seems to use this proxy is using it to sync remote data to local.

    My code assumes those local records are not changed, and I suspect my app's code is susceptible to the bug originally pointed out by Stju. Otherwise I would have posted it by now!

    I'll look into some kind of system of adding in an extension of Ext.data.Store to this proxy, I'm thinking something like this would be useful:

    Let's call the store Sqlite.data.SyncStore.

    It's created and has a proxy set on it just like any other store - it also needs a sqliteProxy config with what currently goes into the proxy config on the sqlite store.

    Whenever data from this store is loaded and the proxy is the original, non-sqlite proxy, the store will automatically:
    - perform a sqlite transaction to remove everything that was previously in the database and insert all the new records
    - switch the store's proxy to sqlite.

    I'd also add a function to the store that would clarify the current proxy settings of the store. And another one to switch back to the original proxy would be good too.

  8. #68
    Sencha User
    Join Date
    May 2012
    Location
    Glasgow, UK
    Posts
    17
    Vote Rating
    0
    henry.wilson is on a distinguished road

      0  

    Default


    Sounds like a great idea shepsii!

    I have a question regarding something I've observed by looking at the database in SQLiteStudio - does the ordering of columns matter? For some reason there is one field in my Claim object which, despite being declared in the middle of the field list, always appears as the first column in the database, before the Id column. Would this be messing things up? I would hope that the order of columns is irrelevent, but it's weird that this column goes in at the front when all the other ones appear in the order they're declared in the code.

  9. #69
    Sencha User
    Join Date
    Mar 2012
    Posts
    28
    Vote Rating
    0
    gcw07 is on a distinguished road

      0  

    Default


    While it is a cool idea it could be somewhat automated, it should definitely be an extension or another library within the proxy. That way it doesn't add extra load to the proxy itself and bog down stores that aren't loading from remote sources. Especially, since I think it can already be done, just need to manually do a couple steps yourself.

  10. #70
    Sencha User
    Join Date
    Mar 2012
    Posts
    28
    Vote Rating
    0
    gcw07 is on a distinguished road

      0  

    Default


    Henry, the order of the columns really shouldn't matter. I have had some of my columns appear out of order too and I could never figure it out. Renaming them sometimes put them back into the correct location, but honestly it really doesn't make a huge difference outside of when your manually looking it over.