application-settings not working as expected - nativescript

Wrote a small app and saved a string via application-settings module.
I can read the value fine within that same session.
If I close the app in the emulator and restart it in the emulator, I can read the value also.
But if I rebuild the project in the IDE (VS Code - pressing "refresh/reload" icon) - the app restarts and the stored value is no longer there.
Seriously?
Is application-settings stored data not persistent across sessions.

If you rebuild your application, it will delete the old application on your smartphones + all the datas that was stored in application settings.
If you don't want that the stored datas disappear meanwhile you are changing the code you should use the livesync options.
In order to do that, go with a terminal at the root of your app projet and type the following command :
tns livesync ios --watch
For IOS or
tns livesync android --watch
For Android
I hope this will help you

Related

Create and restore snapshot in Xcode 13 just like Android Studio with Android Emulator

I use Android Studio and Xcode to develop Android and iOS apps respectively.
When I use Android Studio, I can use an option to create a snapshot on android emulator. This way I can restore the snapshot and continue the development using the same data that stored in Sqlite database and SharedPreferences.
I use Xcode and want to replicate the behavior do the same process on the iOS simulator because I want to retain data in UserDefaults and the Sqlite database.
I see some options to do that in a very old version of Xcode, but can't find it in the Xcode 13 version.
Is it possible to create and restore a snapshot on Xcode 13 and iOS Simulator just like in Android Studio with Android Emulator ?
If is not possible, there is any other way or workaround to do something similar ?
I found a workaround since I can't find a way to use snapshot with Xcode and
iOS Simulator.
I use the following code in my app:
print("home directory: \(NSHomeDirectory())" )
This code shows where the iOS simulator folder of the app is located
With that information, I can find the database.sqlite file. I can copy and backup the file or edit the info inside direct in the database.sqlite file.
This way I can use the app and restore the database.sqlite when I want to.
After I edit some info in this file, I just need to remember to run the app again using Ctrl+R.
The same is valid with the file that contains the UserDefaults data stored by the app. This file is located inside
HomeDirectory/Library/Preferences/<app_bundle_id>.plist
You can edit and backup the file if you want, but if you modify the file, remember to run the app again with Xcode.

Unable to apply changes on device: XXXXX. Error is: Socket connection timed out

I have a problem when I want to build my Nativescript app on an Android emulator.
So the first thing I've done is tns create my app with an --ng flag for the angular framework. After that i try to run it on an emulator and the last message I've get before the error is that it is "Succesfully installed on device with identifier 'emulator'".
I've tried the whole https://docs.nativescript.org/releases/upgrade-instructions and got stuck at upgrading typescript and angular dependencies because it says it does not contain a package.json file.
I've run some NScore applications succesfully on the emulator but this is my first NG app and I've hit a wall. I've saw the firebase thread too about this particular theme but I don't use firebase for my app.
I had the same error trying to run nativescript on a particular device. check the storage of the device you are trying to build on.
In my own case it was storage issues that lead to that error.
In my case, the problem was a secondary space in Mi Note 7. For some reason, nativescript was installing the app in the second space (even when first space is active)
I got this error, by Updating App ID only in package.json
To resolve the issue, I manually updated the App ID in the following file.
App_Resources\Android\settings.json
{"appId":"org.nativescript.appid","minSdkVersion":null,"targetSdkVersion":null}

Where to save app info to survive reinstallation after building?

I currently use App.Current.Properties dictionary to save some basic data needed for the app to connect to a database.
Unfortunately, when building and reinstalling the app on the Android simulator (didn't tried on the iOS one), app data seems to be wiped, so every time I test the app I've to re-enter connection info.
How can I avoid that?
Does it work if you start the app without redeploying it? If you just click the app icon on the emulator. If it does not you have propbarly forgot to call the save method.
Application.Current.SavePropertiesAsync();

Xamarin.Forms application properties not saved between sessions with Android

In my cross-platform app I save application properties in the following way:
Application.Current.Properties["myValue"] = "Test";
await Application.Current.SavePropertiesAsync();
I then retrieve them later like this:
string myVal = Application.Current.Properties["myValue"];
This works perfectly when using the Android emulator. I can close the emulator down, and when I restart it again, my app data is preserved.
However, when I deploy this app to a physical Android device, data preservation works as expected until I close the app and open it again. At this point I retrieve an empty value. It's as though the data is only saved to memory (and not to file).
Furthermore, the iOS version of the exact same app works faultlessly. When I open the app up, all previous values have been saved and I can retrieve them.
What am I doing wrong on the Android version? Is there an Android setting, permission, or option which I need to enable to have the values saved between app sessions when actually deployed to a phone?
I had a similar problem where only the debug version saved Properties. I think we're still waiting for the bug fix, and this is the workaround on Xamarin Forms bugzilla.
In short: go to .Droid project settings -> Android Options -> Linker
and enter/add "System.Runtime.Serialization" to Skip linking assemblies.
Wow! After lots of research, I have getting solution for same issue in mine. Problem is that, when i store List or DataTable in Application.Current.Properties then all properties are removed after reopen app but if i store List or DataTable as serialize, it working fine.
In VS you need to enable the option:
Preserve application data/cache on device between deploys
You can find this at Tools > Options > Xamarin > Android Settings.
I don't know if this has stopped in the latest updates of Xamarin Forms, the data is available until the application is running similar to session but persistent storage does not work.
The best api I think available for this is Akavache https://github.com/akavache/Akavache.
You can read this blog https://codemilltech.com/akavache-is-aka-awesome/.
Update
There is a bug filed in bugzilla:
https://bugzilla.xamarin.com/show_bug.cgi?id=26444
I don't remember exactly which part of this solved the issue, but when I followed the instructions to prepare the Android application for release from start to finish, the issue went away. The problem occurred when debug builds were used on an Android device.
https://developer.xamarin.com/guides/android/deployment,_testing,_and_metrics/publishing_an_application/part_1_-_preparing_an_application_for_release/
The link details the suggested settings for the Android project when releasing to device. Following these instructions should solve the issue.

Xamarin Android losing keystore after each deploy in emulator

I'm discovering Xamarin Android with Visual Studio and I'm working with the keystore.
All my code is based on Xamarin's https://github.com/xamarin/Xamarin.Auth
When I deploy and use the keystore, everything is working fine. Data are persisted and I can save, delete and retrieve them without a problem.
I can turn off the emulator or kill the application, the keystore is still here with the data in it.
But if I modify the code and redeploy the application without stopping the emulator, my keystore is lost.
Any idea on where I should start digging to troubleshoot this problem. I don't have any exception.
Looks like you need to set the setting to Preserve application data/cache on device between deploys.
Check this answer for details:
SharedPreferences not saving values properly

Resources