1. #1
    Sencha User
    Join Date
    Oct 2013
    Posts
    5
    Vote Rating
    0
    davetbo is on a distinguished road

      0  

    Default Android native local storage?

    Android native local storage?


    I have an app that works great in the browser using St 2.3.1 and the localstorage proxy. When I do sencha app build native it compiles fine, I install it, run it, and it behaves differently than in the browser, failing when I try to create a new record. By using adb debug and filtering for errors and Web console, I see that it's having problems with my store variable. Is there an issue using localstorage with Sencha Touch, cmd, and Android native?

    Thanks in advance
    Dave

  2. #2
    Sencha - Senior Forum Manager mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    37,327
    Vote Rating
    850
    mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute

      0  

    Default


    Shouldn't be any issue. Sencha Touch is using HTML5 standard for localstorage so the only issue would be platform implementation.
    Mitchell Simoens @SenchaMitch
    Sencha Inc, Senior Forum Manager
    ________________
    Check out my GitHub, lots of nice things for Ext JS 4 and Sencha Touch 2
    https://github.com/mitchellsimoens

    Think my support is good? Get more personalized support via a support subscription. https://www.sencha.com/store/

    Need more help with your app? Hire Sencha Services services@sencha.com

    Want to learn Sencha Touch 2? Check out Sencha Touch in Action that is in print!

    When posting code, please use BBCode's CODE tags.

  3. #3
    Sencha User
    Join Date
    Oct 2013
    Posts
    5
    Vote Rating
    0
    davetbo is on a distinguished road

      0  

    Default


    So here are more details. This app works in my browser (Chrome on Mac). Then I package it for Android using the following steps:

    sencha app build native
    cd build/native-package-mobile/SFA/packager.json/
    adb install -r SuperFamilyApp.apk

    Those generate no errors, so it all looks good. However, when I use the app on my Android phone and I click Save to save a record, nothing happens.

    Digging further into the debugging options on Android, I discovered that I could watch for errors on my running app by doing this :
    adb logcat *:E | grep "Web Console"

    Now, when I click on the Save button in my app, I see these errors popping up:
    E/Web Console(19029): Uncaught TypeError: Cannot call method 'findRecord' of undefined:1

    I found this section in the code of my controller's onSavePersonCommand function:
    Code:
    onSavePersonCommand: function () {
       console.log('onSavePersonCommand');
       var personEditorView = this.getBcPersonEditorView();
       var currentPerson = personEditorView.getRecord();
       var newValues = personEditorView.getValues();
       var keys = Object.keys(newValues);
       for (var i = 0; i < keys.length; i++) {
          newValues[keys[i]] = SFA.app.trim(newValues[keys[i]]);
       }
       var peopleStore = Ext.getStore('bcpeople');
       currentPerson.set('name',newValues.name);
       currentPerson.set('agetype', newValues.agetype);
    
    
       var errors = currentPerson.validate();
       if (!errors.isValid()) {
          Ext.Msg.alert('Error saving person!');
          currentPerson.reject();
          return;
       }
    		
    
    
       if(null == peopleStore.findRecord('_id', currentPerson.data._id)) {
          peopleStore.add(currentPerson);
       }
       else {
          console.log('Person with _id ' , currentPerson.data._id, ' is already in the database.');
       }
       peopleStore.sync();
    },
    Now if I comment out the if/else block above the peopleStore.sync() line, recompile, reinstall on my phone, and hit the same Save button in the app, I get the following error:


    E/Web Console(22186): Uncaught TypeError: Cannot call method 'sync' of undefined:1

    Therefore, it's clearly saying that the peopleStore variable is undefined when running on my phone but not in Chrome on my Mac.

    Here's the code for the PeopleStore class:
    Code:
    Ext.define('SFA.store.bc.People', {
       extend: 'Ext.data.Store',
       storeId: 'bcpeople',
       requires: ['Ext.data.proxy.LocalStorage'],
       config: {
             model: 'SFA.model.bc.Person',
             proxy : {
             type: 'localstorage',
              id: 'sfa'
          },
          autoLoad: true,
          sorters: [{property: 'name'}],
          filters: [{property: 'doctype', value: 'person'}]
       }
    });
    And here's the code for the model used by the store:

    Code:
     
    
    Ext.define('SFA.model.bc.Person', {
       extend: 'Ext.data.Model',
       config: {
       idProperty: '_id',
       fields: [
         {  name: '_id', type: 'string' },
    		     {  name: 'name', type: 'string'},
    		     {  name: 'agetype', type: 'string'},
         {  name: '_rev', type: 'string'},
         {  name: 'doctype', type: 'string', defaultValue: 'person'}
    		   
       ],
       validations : [
          { type: 'presence', field: '_id'},
          { type: 'presence', field: 'name', message: 'Please enter a name for this person'},
          { type: 'presence', field: 'agetype'}
       ]	
       },
       constructor : function(config) {
          this.callParent(arguments);
          if (this.get('name') === null) {
          var uid = SFA.app.getGUID();
          console.log("UID: ", uid);
          this.setId(uid);
       }
     }
    });
    Any ideas why this store ends up undefined on my phone and not on the web browser? Maybe something to do with permissions on Android? I also granted these permissions in packager.json:
    Code:
    "permissions":[
    		"INTERNET",
    		"ACCESS_NETWORK_STATE",
    		"READ_EXTERNAL_STORAGE",
    		"WRITE_EXTERNAL_STORAGE"
    	],
    Thanks in advance for any help!

    Best,
    Dave

Thread Participants: 1