Well, in the previous entry I explain how to install the plugin and how decide according the system the origin of data and the destination. Every OS has a different file system and we must adapt us at your characteristics. In the two cases the origin it's similar and use applicationStorageDirectory property for access both of them. The difference is in the directory that contains the database, Library/LocalDatabase/ in iOS and databases/ in Android.
In the case of the destination it's where the differences are more big. In Android I think the best place is the root of the external storage, either in the root or in a directory for the app in the root. The access for this location is easy and the user just have to plug your device and has already access to the files and directories.
iOS is another thing. Reading the File System Programming Guide from Apple Developers
here we can see that recommends
Documents/ in the Data Container inside the sandbox for store the user-generated content. The contents of this directory is accessible with iTunes and iCloud when making a backup or through file sharing but not as in Android with a file manager when plugs the device in a computer.
Having said this already we can implement our methods for make a backup and restore it. For the destination path we use two different properties externalRootDirectory for Android and documentsDirectory for iOS, you can see this in the previous entry.
The idea for the implementation is easy, first we check if file exist in the destination path, if it exist we remove it because the plugin cannot rewrite it. After of remove the file or if the file doesn't exist we copy it . If the operation have success shows a success message and if it is rejected shows an error message.
The code for the backup
backup() {
let path: string;
let destPath: string;
let dirMessage: string;
if (
this.
iOSDir.
length >
1) {
destPath =
this.
iOSDir;
path =
this.
file.
applicationStorageDirectory +
'Library/LocalDatabase/';
this.
translate.
get(
'IOSDOCUMENTSDIRECTORY')
.
subscribe(
value => {
dirMessage =
value;
});
}
else {
destPath =
this.
AndroidDir;<
path =
this.
file.
applicationStorageDirectory +
'databases/';
this.
translate.
get(
'ANDROIDDOCUMENTSDIRECTORY')
.
subscribe(
value => {
dirMessage =
value;
});
}
//check if file exist, if it exist remove it and copy new file.
//If the file doesn't exist copy it
this.
file.
checkFile(
destPath,
this.
fileName)
.
then(
success => {
//if file exist first remove it
this.
file.
removeFile(
destPath,
this.
fileName)
.
then(
success => {
//Copy new instance of the file
this.
file.
copyFile(
path,
this.
fileName,
destPath,
this.
fileName).
then(
success => {
//translations
this.
translate.
get(
'BACKUPSUCCESS')
.
subscribe(
value => {
this.
alertMes =
value;
this.
alertMessage(
this.
alertMes +
dirMessage);
});
}, error => {
this.translate.get('BACKUPUNSUCCESS')
.subscribe(value => {
this.alertMes = value;
this.alertMessage(this.alertMes);
});
});
}, error => {
this.translate.get('BACKUPUNSUCCESS')
.subscribe(value => {
this.alertMes = value;
this.alertMessage(this.alertMes);
});
});
}, error => {
//first copy of file
this.file.copyFile(path, this.fileName, destPath,
this.fileName).then(success => {
this.translate.get('BACKUPSUCCESS')
.subscribe(value => {
this.alertMes = value;
this.alertMessage(this.alertMes + dirMessage);
});
}, error => {
this.translate.get('BACKUPUNSUCCESS')
.subscribe(value => {
this.alertMes = value;
this.alertMessage(this.alertMes);
})
});
});
}
And for restore
restoreBackup() {
let path: string;
let destPath: string;
let dirMessage: string;
if (this.iOSDir.length > 1) {
path = this.iOSDir;
destPath = this.file.applicationStorageDirectory +
'Library/LocalDatabase/';
} else {
path = this.AndroidDir;
destPath = this.file.applicationStorageDirectory +
'databases/';
}
//check if backup file exist
this.file.checkFile(path, this.fileName).then(success => {
//check if file exist, if it exist remove it and
//copy new file. If the file doesn't exist copy it
this.file.checkFile(destPath, this.fileName)
.then(success => {
//if file exist first remove it
this.file.removeFile(destPath, this.fileName)
.then(success => {
//Copy new instance of the file
this.file.copyFile(path, this.fileName, destPath,
this.fileName).then(success => {
//Close and reopen database
this.dbService.closeDatabase().then(success => {
this.dbService.openDatabase();
this.translate.get('RESTOREBACKUPSUCCESS')
.subscribe(value => {
this.alertMes = value;
this.alertMessage(this.alertMes);
});
});
}, error => {
this.translate.get('RESTOREBACKUPUNSUCCESS')
.subscribe(value => {
this.alertMes = value;
this.alertMessage(this.alertMes);
});
});
}, error => {
this.translate.get('RESTOREBACKUPUNSUCCESS')
.subscribe(value => {
this.alertMes = value;
this.alertMessage(this.alertMes);
});
});
}, error => {
this.translate.get('RESTOREBACKUPUNSUCCESS')
.subscribe(value => {
this.alertMes = value;
this.alertMessage(this.alertMes);
});
});
}, error => {
this.translate.get('BACKUPUNOTEXIST')
.subscribe(value => {
this.alertMes = value;
this.alertMessage(this.alertMes + path);
});
});
}
Basically for restore we do the same changing the origin and the destination of the file. The last operation when the file are copied is close the database and reopen it for what the changes are efectives in the app.
And that would be all for iOS and Android before to Marshmallow. Since Marshmallow it is necessary to check the permissions and this I leave it for the next entry.