[SOLVED] iOS, block application while opening database


#1

Hi,

I’m working on an ionic app. I use PouchDB to store my data in order to have an offline mode.
I’m currently having trouble with the iOS version.

When I open my dataBase with this command :

new PouchDB('XXXXXXDB', {adapter : 'websql', auto_compaction : true, revs_limit : 1, location:'default', iosDatabaseLocation:'default'})

I have this in my logs :

 2016-05-20 18:14:47.225 SGNews[6944:187465] OPEN database: _pouch_laMatinaleDB
 2016-05-20 18:14:47.225 SGNews[6944:187465] new transaction is waiting for open operation
 2016-05-20 18:14:47.227 SGNews[6944:187495] Good news: SQLite is thread safe!
 2016-05-20 18:14:47.230 SGNews[6944:187465] DB opened: _pouch_laMatinaleDB

This is telling me that the database was opened, but I have no return from the promise. This makes my app blocked, even my spinner is not moving.

To reproduce this issue, I open and close my app until I get it.

I know that is a pretty awful way, but some people who use it normally have the same problem.

I use the 5.3.2 release version of PouchDB. (Everything is great on Android)


#2

Are you using the SQLite plugin? If so, make sure your not making calls to the DB before $ionicPlatform.ready. You may be creating a race condition where the plugin is not yet initialized when you make the first query.


#3

I thought I did it in the way, but when I looked furthermore it wasn’t really the case.

Thank you for your answer.

I will make some tests and I will be back to validate your answer.


#4

The error was from this. But the $ionicPlatform.ready wasn’t sufficient enough, I had to add a $timeout in order to not have this issue anymore.

Thanks for your help.


#5

I got an answer from issue for Cordova-sqlite-storage I use. (Cordova-sqlite-storage issue opened)

They told me to look after location and iosDatabaseLocation options. After reading more about it, I found that location for iOS is still available even if it’s deprecated. So, I think there is some troubles when you set location and iosDatabaseLocation in the same options. I did this in order to not have to test the platform and just have one line of code for opening my database.

After I separate the location and iosDatabaseLocation, it seems to be great.


#6

That wasn’t really a good answer. Even if the results are better, It’s not concluant.
I’m waiting for almost 5 seconds after the deviceready event to be sure to not freeze my app.

if someone can help me out, I will be grateful.


#8

Hi,
I m stuck with same issue as mentioned above, since from week,anyone please help me
I’m working on an ionic1 app…
I’m currently having trouble with the iOS version.
and iam using cordova sqlite plugin,and here db is getting opened but cordovasqlite.execute function(), not working instead iam getting error code:5,message:"no such table:table-name"
for opening database iam using,

if(ionic.Platform.isIOS())
{
console.log(‘database is iso’);
var db = $cordovaSQLite.openDB({name: ‘olarcniapp.db’, iosDatabaseLocation: ‘Library’}); //ios device

	}
	else
	{
			console.log('database is android');
		var	db = $cordovaSQLite.openDB({name:'olarcniapp.db',location:'default'});  // android device

	}


#9

Hello,

Did you try this answer : https://stackoverflow.com/a/41249951
It says : Add transaction around your sql query.


#11

Hi ,
Thank you for your response, how to add transaction for select insert queries, here iam doing seperate service for select and insert, my code looks like this,

olaapp.service(’$databaseinsertService’,[’$dataBaseInsertOrUpdateFactory’,’$q’, function( $dataBaseInsertOrUpdateFactory, $q){

this.saveLoginInfo=function($scope, $cordovaSQLite, username, response){

    var defferedReturn = $q.defer();
    var currenttime = new Date().getTime();
    var selectQuery = "SELECT username FROM user_details WHERE username='"+username+"'";

    var insertQuery = "INSERT INTO user_details (username, secretkey, firstname, lastname, lastsyncdate, firstlogintime, lastlogintime) VALUES ('"+username+"', '"+response.token+"', '"+response.firstname+"', '"+response.lastname+"', "+0+", "+currenttime+", "+currenttime+")";

    var updateQuery = "UPDATE user_details SET secretkey='"+response.token+"' and lastlogintime="+currenttime+" WHERE username='"+username+"'";

    var promise = $dataBaseInsertOrUpdateFactory.dataInsertOrUpdate($scope, $cordovaSQLite, selectQuery, insertQuery, updateQuery);

    promise.then(function(successFlag) {

        defferedReturn.resolve(successFlag);
    }, function(error) {
        defferedReturn.reject(error);
    });
    return defferedReturn.promise;
}

}])


#12

where did you create the user_details table ?


#14

In seperate service.js file

olaapp.service(’$sqliteDB’,[’$q’, function( $q){
this.createOLADB=function($cordovaSQLite, $scope){

	var defferedReturn = $q.defer();
	 try{
			dbinstance = $scope.OLADBINSTANCE;
			//window.alert("After db initilized ->"+JSON.stringify(dbinstance));
            //transaction
           
            dbinstance.transaction(function(tx) {

            **tx.execute( "CREATE TABLE user_details (username VARCHAR(100) NOT NULL , secretkey VARCHAR(100) NOT NULL , firstname VARCHAR(50), lastname VARCHAR(50), lastsyncdate INT(20) , firstlogintime INT(20) , lastlogintime INT(20) , PRIMARY KEY (username) )");**

}, function(error) {
console.log('Transaction ERROR: ’ + error.message);
}, function() {
console.log(‘Created tables OK’);
});

			defferedReturn.resolve(true);
		}catch(e){
			defferedReturn.reject(false);
		}
	return defferedReturn.promise;
}

}])