Is there any particular reason behind data loss on app close in iOS? I am using sqlite plugin to store data for the app. It creates table and pre-populates data but when I close app and restart it, all the data is gone. I am using same set up for android and it is working fine. Am I missing any specific things that I need to do for iOS.
Check your CREATE TABLE scripts (if any) and ensure that you are using CREATE TABLE IF NOT EXISTS.
Also, ensure that your db file is being created in the Documents folder, and not the /tmp folder, which gets flushed routinely by iOS (not always, but when it runs garbage collection from time to time).
Post your SQL initialisation code here so we can see how the create is done.
I would also highly recommend Nic Raboy’s blog on setting up SQLite with Ionic - I used it as a guide to get several SQLite + Ionic projects up and running with great success: https://blog.nraboy.com/2014/11/use-sqlite-instead-local-storage-ionic-framework/
This the part of code I am using. This is working great on Android devices and chrome.
if(window.sqlitePlugin){
self.db = window.sqlitePlugin.openDatabase(config.name, '1.0', 'database', -1);
if(self.db){
self.tableExists("user").then(function(data){
if(data){
def.resolve({success: true, type:""});
}else{
var columns = config.tables['user'];
var defs=[];
for (var columnName in columns) {
var type = columns[columnName];
defs.push(columnName + ' ' + type);
}
var sql = 'CREATE TABLE IF NOT EXISTS user (' + defs.join(', ') + ')';
self.query(sql).then(function(){
self.query("SELECT count(*) as total, id as user_id from user").then(function(data){
if(data){
var data = self.fetchAll(data);
if(data[0].total>0){
self.checkSetUp().then(function(data){
var success = true;
var ts = [];
for(var d in data){
if(!data[d].success){
ts.push(data[d].table);
success = false;
}
}
if(!success){
def.resolve({success: false, type:"wrongSetUp", message:ts.join(" ")});
}else{
def.resolve({success: true, type:"correct", message:ts.join(" "), id: data.id});
}
})
}else{
def.resolve({success: false, type:"noUser", message:"No user exists"});
}
}else{
def.resolve({success: false, type:"noUser", message:"No user exists"});
}
});
});
}
})
}
Just curious as to what the contents of config.name
is? Actually, the whole config
object, if at all possible?
Sure. Here it is
angular.module('starter.configs', [])
.constant('config', {
name: 'YouGRE.db',
tables:{
user:{
id:'int primary key ',
name:'text',
email: 'text',
password:'text',
settings:'text'
}
}
})