Flutter iOS App sqflite database data lost after app upgrade - ios

We have an app build entirely by flutter. When we send app upgrades to google store the sqflite database data is not lost when updating apk in Android device.
But in iOS Device after user updates app to new version all database data lost.
flutter v1.17.5
Package used for data persistence:
sqflite: ^1.3.1
path_provider: ^1.6.11
Can you please help to solve this problem. Data of user must not be deleted after app update to new version. Maybe I need to use other type of data persistence or how can I solve this.
Here are parts where I initialize Database in DatabaseHelper class:
class DatabaseHelper {
static DatabaseHelper _databaseHelper; // Singletone DatabaseHelper
static Database _database; // Singletone Database
String wifiSystemTable = 'wifi_system_table';
String colId = 'id';
String colDataType = 'data_type';
String colLocationName = 'location_name';
String colBackground = 'background';
String colLocationId = 'location_id';
String colDeviceType = 'device_type';
String colIp = 'ip';
String colName1 = 'name1';
String colName2 = 'name2';
String colName3 = 'name3';
String colRemotePort = 'remote_port';
DatabaseHelper._createInstance(); // Named constractor to create instance of Database Helper
factory DatabaseHelper() {
if (_databaseHelper == null) {
_databaseHelper = DatabaseHelper
._createInstance(); //This is execute only once, singletone object
}
return _databaseHelper;
}
Future<Database> get database async {
if (_database == null) {
_database = await initializeDatabase();
}
return _database;
}
Future<Database> initializeDatabase() async {
//Get the directory path for both Android and IOS to store Database
Directory directory = await getApplicationDocumentsDirectory();
String path = p.join(directory.toString(), 'wifi.db');
//Open/create the database at the given path
var wifiSystemDatabase =
await openDatabase(path, version: 1, onCreate: _createDb);
return wifiSystemDatabase;
}
void _createDb(Database db, int newVersion) async {
await db.execute(
'CREATE TABLE $wifiSystemTable($colId INTEGER PRIMARY KEY, $colDataType INTEGER, $colLocationName TEXT, $colBackground TEXT, $colLocationId INTEGER, $colDeviceType INTEGER, $colIp TEXT, $colName1 TEXT, $colName2 TEXT, $colName3 TEXT, $colRemotePort TEXT)');
}
Thanks very much

I think your DB path is getting changed. Try this.
Future<Database> initializeDatabase() async {
//Get the directory path for both Android and IOS to store Database
String databasesPath = await getDatabasesPath();
String path = p.join(databasesPath, 'wifi.db');
//Open/create the database at the given path
var wifiSystemDatabase = await openDatabase(path, version: 1, onCreate: _createDb);
return wifiSystemDatabase;
}
Hope it helps :)

Related

How to add new table to sqlite?

I need to add a new column name id INTEGER AUTOINCREMENT and a new table for the current database for my existing table. How to use 'onUpgrade'? Is it need to change the version number?
initDb() async {
io.Directory documentsDirectory = await getApplicationDocumentsDirectory();
String path = join(documentsDirectory.path, "HelperDatabase.db");
var theDb = await openDatabase(path, version: 1, onCreate: _onCreate, onUpgrade: _onUpgrade);
return theDb;
}
How to use _onUpgrade
void _onUpgrade(Database db, int oldVersion, int newVersion)async{
}
Is it need to add column also?
void _onCreate(Database db, int version) async {
await db.execute(
"""CREATE TABLE AssetAssemblyTable(e INTEGER, a INTEGER, c INTEGER)""");
To update your DB from old version, you should change version to 2.
You should change onCreate and onUpdate like below.
// This is called for new users who have no old db
void _onCreate(Database db, int version) async {
// if `AssetAssemblyTable` has a new column in version 2, add the column here.
await db.execute(
"""CREATE TABLE AssetAssemblyTable(e INTEGER, a INTEGER, c INTEGER)""");
)
await db.execute("CREATE TABLE NewTable...") // create new Table
}
// This is called for existing users who have old db(version 1)
void _onUpgrade(Database db, int oldVersion, int newVersion)async{
// In this case, oldVersion is 1, newVersion is 2
if (oldVersion == 1) {
await db.execute("ALTER TABLE AssetAssemblyTable...") // add new column to existing table.
await db.execute("CREATE TABLE NewTable...") // create new Table
}
}
more example is below
https://github.com/tekartik/sqflite/blob/master/sqflite/doc/migration_example.md
Here what I've done :
class SqliteDB {
static final SqliteDB _instance = new SqliteDB.internal();
factory SqliteDB() => _instance;
static Database? _db;
Future<Database?> get db async {
if (_db != null) {
return _db;
}
_db = await initDb();
return _db;
}
SqliteDB.internal();
/// Initialize DB
initDb() async {
io.Directory documentDirectory = await
getApplicationDocumentsDirectory();
String path = join(documentDirectory.path, "RadiosStations.db");
var taskDb = await openDatabase(
//open the database or create a database if there isn't any path,
version: 2, onCreate: (Database db, int version) async {
await db.execute(
"""CREATE TABLE AssetAssemblyTable(e INTEGER, a INTEGER, c
INTEGER)""");
)
await db.execute("CREATE TABLE NewTable...") // create new Table;
},
onUpgrade: (Database db, int oldVersion, int newVersion)async{
// In this case, oldVersion is 1, newVersion is 2
if (oldVersion == 1) {
await db.execute(
"""CREATE TABLE AssetAssemblyTable(e INTEGER, a INTEGER, c
INTEGER)""");
)
await db.execute("CREATE TABLE NewTable...") // create new Table;
}}
);
return taskDb;}

Flutter: shared preferences

I have this function:
Future<String> load(SharedPreferences prefs, String fileName) async {
prefs = await SharedPreferences.getInstance();
String jsonString = prefs.getString(fileName) ?? "";
if (jsonString.isNotEmpty) {
return jsonString;
}else{
return ...
}
}
What should I return in the else case? I tried with "" but it doesn't work.
Shared Preferences
In Flutter, Shared Preferences are used to store primitive data (int, double, bool, string, and stringList). This data is associated with the app, so when the user uninstalls your app, the data will also be deleted.
Get the plugin
The shared_preferences plugin from pub is a wrapper around Android SharedPreferences and iOS NSUserDefaults. You can get this plugin by adding the shared_preferences line to your pubspec.yaml file in the dependencies section.
dependencies:
shared_preferences: '>=0.5.12+2 <2.0.0'
You can change the version number to whatever the current one is, but anything less than 2.0 should be compatible.
Import the package
In whichever file you need the Shared Preferences, add the following import:
import 'package:shared_preferences/shared_preferences.dart';
Reading and writing data
To get the shared preferences object you can do the following:
final prefs = await SharedPreferences.getInstance();
This will be used for all of the following examples.
int
read: final myInt = prefs.getInt('my_int_key') ?? 0;
write: prefs.setInt('my_int_key', 42);
double
read: final myDouble = prefs.getDouble('my_double_key') ?? 0.0;
write: prefs.setDouble('my_double_key', 3.14);
bool
read: final myBool = prefs.getBool('my_bool_key') ?? false;
write: prefs.setBool('my_bool_key', true);
string
read: final myString = prefs.getString('my_string_key') ?? '';
write: prefs.setString('my_string_key', 'hello');
stringList
read: final myStringList = prefs.getStringList('my_string_list_key') ?? [];
write: prefs.setStringList('my_string_list_key', ['horse', 'cow', 'sheep']);
Removing data
You can remove any saved data by supplying the key name:
prefs.remove('my_int_key');
I rarely find a need to do that, though. I just overwrite the old data or ignore it. You shouldn't store any sensitive data in Shared Preferences.
See also
Shared Preferences Service in Flutter for Code Maintainability
Documentation: Storing key-value data on disk
What are the ?? double question marks in Dart?
How to create an empty list in Dart
The answer is "it depends". Namely, it depends on what exactly you are doing with the result of this function, and what a good empty default value means in that context.
Assuming you're decoding the returned JSON string into a Map<String, dynamic>, then a good default value might be the empty map. In that case, you could reformulate your function as follows:
Future<String> loadJSON(final String fileName) async {
final SharedPreferences prefs = await SharedPreferences.getInstance();
final String jsonString = prefs.getString(fileName);
if (jsonString != null && jsonString.isNotEmpty) {
return jsonString;
}
return "{}"; // default value
}
final String jsonString = await loadJSON("test.json");
final Map<String, dynamic> jsonData = json.decode(jsonString);
However, it probably makes more sense to reformulate this procedure as a slightly higher-level function returning actual map values:
Future<Map<String, dynamic>> loadData(final String fileName) async {
final SharedPreferences prefs = await SharedPreferences.getInstance();
final String jsonString = prefs.getString(fileName);
if (jsonString != null && jsonString.isNotEmpty) {
return json.decode(jsonString);
}
return Map(); // default value
}
final Map<String, dynamic> jsonData = await loadData("test.json");

Dart - is it possible to change a future.forEach. to a map?

So basically i have this piece of working code:
List<User> users = List();
await Future.forEach(querySnapshot.documents, (doc) async {
final snapshot = await doc['user'].get();
users.add(User(id: snapshot["id"], name: snapshot["mail"]));
});
return users;
It's working fine and does exactly what I need but I was wondering if there was a way to somehow change it to a map, such as:
return querySnapshot.documents.map((doc) async {
final snapshot = await doc['user'].get();
User(id: snapshot["id"], name: snapshot["mail"]);
}).toList{growable: true};
The problem when I do that is that it says: a value of type List< Future< Null>> can't be assigned to a variable of type List< User>.
So I was wondering if it was possible or if the only way is with a Future.forEach
To turn a list of Futures into a single Future, use Future.wait from dart:async. (Also don't forget to return the user object).
final List<User> = await Future.wait(querySnapshot.documents.map((doc) async {
final snapshot = await doc['user'].get();
return User(id: snapshot["id"], name: snapshot["mail"]);
}));

Element value based writing to Google Cloud Storage using Dataflow

I'm trying to build a dataflow process to help archive data by storing data into Google Cloud Storage. I have a PubSub stream of Event data which contains the client_id and some metadata. This process should archive all incoming events, so this needs to be a streaming pipeline.
I'd like to be able to handle archiving the events by putting each Event I receive inside a bucket that looks like gs://archive/client_id/eventdata.json . Is that possible to do within dataflow/apache beam, specifically being able to assign the file name differently for each Event in the PCollection?
EDIT:
So my code currently looks like:
public static class PerWindowFiles extends FileBasedSink.FilenamePolicy {
private String customerId;
public PerWindowFiles(String customerId) {
this.customerId = customerId;
}
#Override
public ResourceId windowedFilename(ResourceId outputDirectory, WindowedContext context, String extension) {
String filename = bucket+"/"+customerId;
return outputDirectory.resolve(filename, ResolveOptions.StandardResolveOptions.RESOLVE_FILE);
}
#Override
public ResourceId unwindowedFilename(
ResourceId outputDirectory, Context context, String extension) {
throw new UnsupportedOperationException("Unsupported.");
}
}
public static void main(String[] args) throws IOException {
DataflowPipelineOptions options = PipelineOptionsFactory.fromArgs(args)
.withValidation()
.as(DataflowPipelineOptions.class);
options.setRunner(DataflowRunner.class);
options.setStreaming(true);
Pipeline p = Pipeline.create(options);
PCollection<Event> set = p.apply(PubsubIO.readStrings()
.fromTopic("topic"))
.apply(new ConvertToEvent()));
PCollection<KV<String, Event>> events = labelEvents(set);
PCollection<KV<String, EventGroup>> sessions = groupEvents(events);
String customers = System.getProperty("CUSTOMERS");
JSONArray custList = new JSONArray(customers);
for (Object cust : custList) {
if (cust instanceof String) {
String customerId = (String) cust;
PCollection<KV<String, EventGroup>> custCol = sessions.apply(new FilterByCustomer(customerId));
stringifyEvents(custCol)
.apply(TextIO.write()
.to("gs://archive/")
.withFilenamePolicy(new PerWindowFiles(customerId))
.withWindowedWrites()
.withNumShards(3));
} else {
LOG.info("Failed to create TextIO: customerId was not String");
}
}
p.run()
.waitUntilFinish();
}
This code is ugly because I need to redeploy every time a new client happens in order to be able to save their data. I would prefer to be able to assign customer data to an appropriate bucket dynamically.
"Dynamic destinations" - choosing the file name based on the elements being written - will be a new feature available in Beam 2.1.0, which has not yet been released.

Database not persisted between builds with xamarin ios

I'm creating a simple sqlite driven app for ios using xamarin studio on a mac.
The sqlite file is created in the "personal" folder and is persisted between builds but when i run the app the tables i created in the previous debug session is gone?
In my code, after checking that the file exists, i connect using a sqliteconnection and create a table and insert a row with the executenonquery method from the command object. While in the same context i can query the table using a second command object but if i stop the debugger and restart the table i gone?
Should i have the file in a different folder, is it a setting in xamarin or ios to keep the tables? Am i unintentionally using temp tables in sqlite or what could be the problem?
Note: so far i'm only using starter version of xamarin and debugging on iphone simulator.
public class BaseHandler
{
private static bool DbIsUpToDate { get; set; }
const int DB_VERSION = 1; //Created DB
const string DB_NAME = "mydb.db3";
protected const string CNN_STRING = "Data Source=" + DB_NAME + ";Version=3";
public BaseHandler ()
{
//No need to validate database more than once on each restart.
if (DbIsUpToDate)
return;
CheckAndCreateDatabase(DB_NAME);
int userVersion = GetUserVersion();
UpdateDBToVersion(userVersion);
DbIsUpToDate = true;
}
int GetUserVersion()
{
int version = 0;
using (var cnn = new SqliteConnection(CNN_STRING))
{
cnn.Open();
using (var cmd = cnn.CreateCommand())
{
cmd.CommandText = "CREATE TABLE UVERSION (VERSION INTEGER);" +
"INSERT INTO UVERSION (VERSION) VALUES(1);";
cmd.ExecuteNonQuery();
}
using (var cmd = cnn.CreateCommand())
{
cmd.CommandText = "SELECT VERSION FROM UVERSION;";
var pragma = cmd.ExecuteScalar();
version = Convert.ToInt32((long)pragma);
}
}
return version;
}
void UpdateDBToVersion(int userVersion)
{
//Prepare the sql statements depending on the users current verion
var sqls = new List<string> ();
if (userVersion < 1)
{
sqls.Add("CREATE TABLE IF NOT EXISTS MYTABLE ("
+ " ID INTEGER PRIMARY KEY, "
+ " NAME TEXT, "
+ " DESC TEXT "
+ ");");
}
//Execute the update statements
using (var cnn = new SqliteConnection(CNN_STRING))
{
cnn.Open();
using (var trans = cnn.BeginTransaction(System.Data.IsolationLevel.ReadCommitted))
{
foreach(string sql in sqls)
{
using (var cmd = cnn.CreateCommand())
{
cmd.CommandText = sql;
cmd.ExecuteNonQuery();
}
}
trans.Commit();
//SetUserVersion(DB_VERSION);
}
}
}
protected string GetDBPath (string dbName)
{
// get a reference to the documents folder
var documents = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
// create the db path
string db = Path.Combine (documents, dbName);
return db;
}
protected void CheckAndCreateDatabase (string dbName)
{
var dbPath = GetDBPath(dbName);
// determine whether or not the database exists
bool dbExists = File.Exists(dbPath);
if (!dbExists)
SqliteConnection.CreateFile(dbPath);
}
}
Again, my problem is that every time I run the debugger it runs GetUserVersion but the table UVERSION is not persisted between sessions. The "File.Exists(dbPath)" returns true so CreateFile is not run. Why is the db empty?
This is a code snippet I've used to save my databases in the iOS simulator and the data seems to persist between app compiles just fine:
string documentsPath = Environment.GetFolderPath (Environment.SpecialFolder.Personal);
string libraryPath = Path.Combine (documentsPath, "../Library/");
var path = Path.Combine (libraryPath, "MyDatabase.db3");
You may also want to check out the SQLite class for Xamarin off of Github:
https://github.com/praeclarum/sqlite-net/tree/master/src
Here's a tutorial on how to use said class:
http://docs.xamarin.com/recipes/ios/data/sqlite/create_a_database_with_sqlitenet
Turns out that I was creating the connection object using the CNN_STRING which just had the db-name instead of the full path to the db. Apperantly the connection object creates the database if the file doesn't exist so the File.Exists(...) might not be needed. I'm not really sure if it should be a temporary db if the complete path is not supplied but it seems to be the case. Changing the creation of the connection object to "datasource=" solved the problem.

Resources