Problems with Application.Current.Properties...

FredyWengerFredyWenger CHInsider ✭✭✭✭✭

Hi all

I have XF 1.3.0.6286-Pre-4 installed.
With an earlier version of XF (1.3.0 Pre-release 1 without documentation), I have (not successfully -> app-crashes) tried, to store application-settings persistent via Application.Current.Properties….).
Now, I want to implement it correct according to the (small) documentation:
developer.xamarin.com/guides/cross-platform/xamarin-forms/working-with/app-lifecycle/#Properties_Dictionary

Problem-description:
As soon as I want to access the Properties, the app (on WP) crashes with “System.AccessViolationException - Attempted to read or write protected memory. This is often an indication, that other memory is corrupt”.
The crash happens by code-line:

if (!Application.Current.Properties.ContainsKey("iWebServerAuswahl"))

(note: the app also crashes on Android as soon as I activate the “if”)

If the key is found, I then want to overtake the value in an integer (public) variable.
If the key is not found I then want to set the key (and the public variable) with a default-variable.
However, as I wrote, the app crashes already by the If-statement (before I can read/write the value).

First, I thought that there are some permissions missing (in WMAppManifest.xml)...
However, I don’t see special permissions to that theme...

I think, the file/whatever in which the settings are stored, is corrupt (as negative side effect from my earlier tests).

Now I want to clear/delete the settings (file).

Strange wise, it don’t help, if I remove the whole app manually from the WP-Device (Lumia 930) and deploy it once again… so - for me - it looks like a problem in my project.

How can solve this problem?

Thanks for any feedback

Posts

  • FredyWengerFredyWenger CHInsider ✭✭✭✭✭

    Updated to 1.3.1 and still have the consistency-problem (some "dead cat" seems to be somewhere in my protect and I want to bury the cat).

    Nobody can help me..?

    Thanks

  • FredrikHaglundFredrikHaglund SEUniversity, Developer Group Leader ✭✭

    What is the value of the Current property on the Application class? Is it null?

    This can happen if you try to access Current in the constructor (or any method called within your constructor) of your Application object.

  • FredyWengerFredyWenger CHInsider ✭✭✭✭✭

    @FrederikHaglund:

    First - thanks for your posting.
    Second - sorry for my delay (we had serious action as a SAN had a massive problem - no data-access for a full day)

    Further I have other problems with my project after the update to 1.3.1:

    • WP crashes hard (not able to run) at Line:
      LoadApplication(new MatrixGuide.App());

    • All Platforms have a problem by start (I thinks this can be related to settings-"file/whatever")

      • But this only occurs if I activate all debugger-exceptions in VS with ctrl-alt-e
    • Debuging on Android don't work (VS hangs))

    • Master-Detail navigation don't work as it should (all platforms -> seems to be a logical-problem in my code)

    But I was able to access Application.Current.Settings-object and also the sub-properties (like .key.count) and to create an new key in memory (so that .count has changed from 0 to 1).

    In my understanding, the settings should be stored (saved persistent in a file/whatever on the device) automatically from XF, if a key is created or changed and then be loaded automatically by every start from XF ("transparent" for me).
    As I wrote - I believe (not sure) that there is a problem with the automatically loading by app-start (as the stored information’s are corrupt of cause my tests with the earlier XF-version) .
    Also the key’s should be loaded automatically by XF by start of the app on the device.

    Is that correct from your point of view?

    If yes, do you know where exactly the information’s are stored and how I can access / delete the file / whatever?

    Thanks once again

  • FredrikHaglundFredrikHaglund SEUniversity, Developer Group Leader ✭✭

    Your assumption that the properties collection is read from a file is correct and this is done automatically. As far as I can see by reverse engineering, an exception during reading or deserialization of that file will be catched and just leave you with an empty Properties collection.

    All values in the name-value dictionary are serialized to disk every time when the application goes to sleep. So nothing will be stored unless your App starts fine and you close it.

    On iOS the actual file location is /Documents/.config/.isolated-storage/PropertyStore.forms inside the Application Sandbox. You can look inside the Application Sandbox on a device with a tool like iExplorer.

    If you suspect this file is the issue, just delete the iOS app and deploy it again. All local files will be deleted with the application.

    File name will be the same on the other platforms but the path will be diffrent.

  • FredyWengerFredyWenger CHInsider ✭✭✭✭✭

    @FredrikHaglund:
    Thanks once again for your feedback.
    In the meantime, I was able to solve some of my problems (WP-Crash and MD-behavior) with help of Xamarin-Support (I have sent them my project).
    But I still have the crash by start on all platforms, if I set all exceptions with ctrl-alt-e.
    => Unfortunately, to delete the app on device don't solve the problem (I hope Xamarin-Support can help me further)
    I further have annoying problems with debugger on iOS and Android :-(.
    Right now, the only platform I am able to debug, is WP.

    In my app, in the constructor (new: public App()), I:

    • Initializes some global variables
    • query our webservice to load some lists that I need for the search-page (e.g. zip-codes)
    • load the master-detail-page

    One of the parameters, I want to store persistent, is, which webserver should be queried (for my in-house-tests the webserver has another (internal) IP as for queries over the internet). Parameter e.g. 0 = internal IP, 1 = external IP.
    Therefore, I need at least this parameter by app-start (so that the list's can be queried as soon as possible).

    At least in WP (can't debug iOS and Android right now), I can access the properties-collection from the constructor in app.cs (public App() in 1.3.1). Maybe this dont't work in iOS and Android...
    The problem is, that the data are not stored when the app is closed (in the meantime, I was able to start an close my app properly on WP).

    In the constructor (and also in OnStart-Event), I have the lines:

      if (!Application.Current.Properties.ContainsKey("iWebServerAuswahl"))
                { Application.Current.Properties["iWebServerAuswahl"] = 0; } 
    

    (check, if the key exists, if not, create key and set the default value).
    I then overtake the value in a global variable (first time: default value, settled value as soon a different value is settled).
    The code seems to work (the key is settled in the constructor and then in OnStart I can see, that it is settled).
    But If I close the app and run it once again, the key is still not found...
    Maybe this is related to the (still existing problem) by app-start (see on top).

    Further questions:
    You wrote:

    On iOS the actual file location is /Documents/.config/.isolated-storage/PropertyStore.forms inside the Application Sandbox. You can look inside the Application Sandbox on a device with a tool like iExplorer

    "tool like iExplorer":
    - With iExplorer, you mean the standard-browser?
    - If yes, how to look inside the Application Sandbox with it (can you please explain in detail)?

    If the (once) settled properties are stored in the Application Sandbox and are deleted, if the app is deleted from device:
    - What happens with the settled (persistent) properties , if the user installs a update from app-shop?
    => Are the properties overtaken automatically to the new version of the app (how it should be) or deleted?
    ==> If the properties are deleted, this is not usable (as the user-setting are lost by every update to the app)

    Thanks once again...

  • FredrikHaglundFredrikHaglund SEUniversity, Developer Group Leader ✭✭

    @FredyWenger‌

    No, the tool iExplorer (see http://www.macroplant.com/iexplorer/). It only works with Apple devices. It can mount parts of the file systems from a physical device (or backup). Read more: http://www.macroplant.com/iexplorer/tutorials/how-to-browse-files-on-iphone-ipad-ipod

    You can also use a free xcode tool to get the contents of an applications container. Read more: https://developer.apple.com/library/ios/recipes/xcode_help-devices_organizer/articles/manage_containers.html

    Yes, for iOS, if the user installs an update from the App Store all files in /Documents and /Library are moved to the new versions Sandbox so your settings would also be there.

    There are similar tools for Windows Phone and Android to help you peek inside the file system of a device.

  • FredyWengerFredyWenger CHInsider ✭✭✭✭✭

    @FredrikHaglund:

    Thank for your replay (I wonder how to inspect an app with a browser, now it's clear :wink: )
    I will have a look now at the "real" iExplorer (and also search for such tools to the other platforms).
    So.. once thanks for your time... I will post my findings here as soon as I (hopefully will) have...

  • FredyWengerFredyWenger CHInsider ✭✭✭✭✭

    @FredrikHaglund:
    I have installed iExplorer (and had to install also Apple QuickTime and iTunes as pre-requisites to IExplorer) on my Windows developer-machine. After doing that (and connect may iPhone to my windows machine), I was able to inspect my iPhone5 and also see the file with the settings.
    Thanks for the information.
    I then have implemented a small example in my App:
    Result:

    • You are right - it seems as the setting earliest can be accessed in the OnStart()-event, not in constructor (public App())
      -> That's (at least for me) "suboptimal", as I need a setting in the constructor

    • The persistent store (and automatically reload -> access from OnStart() ) works in my app in WP and iOS, but
      not in Android

    Does Android work in your implementation (have you tested it)?

  • FredyWengerFredyWenger CHInsider ✭✭✭✭✭

    @FredrikHaglund‌:
    I have investigated the problem more in deep in the meantime...
    On WP, all works like it should be, including debugger behaviour (only platform, that fully works like expected).
    On iOS, I cannot access the Application-Object in debugger (“Unknow identifier: Application”)
    -> but - "strange wise" - the persistent store of the properties seems to work (I also was able to see the file within iExplore)
    On Android, I cannot access the Application-Object in debugger (“Unknow identifier: Application”) like on iOS and the properties are not stored
    Note: In WP and iOS, it also works from the constructor...
    While the behavior in the (my;-) debugger is very strange, I can’t use the persistent store, as it don’t work on Android and I wonder, if it is a bug in XF to Android
    Therefore (findout if it is an bug to XF.android), I have opened another "Question-Thread".
    Hope this makes it clear...

    forums.xamarin.com/discussion/31447/xf-1-3-1-has-anybody-application-current-properties-working-on-android#latest

  • FredyWengerFredyWenger CHInsider ✭✭✭✭✭

    @FredrikHaglund‌:

    As I don't have received any feedback in the other thread (link above in my last posting), I have filled a bug in Bugzilla (and Jason had a look).
    https://bugzilla.xamarin.com/show_bug.cgi?id=26444
    Conclusion for now:

    • The strange behavior in the debugger (application unknown) - according to Jason - seems to be a bug in mono-debugger (as it works in WP with "Windows-debugger")
      -> Therefore, I have filled two additional bugs in bugzilla to Xamarin.iOS and Xamarin.Andorid (debugger)

    • The persistent store of properties don't work on some devices on Android

    • On my Samsung SM-T900 (tablet) it don't works, while it works on another Android-Handy
      => Jason has confirmed the bug - I hope it will be solved soon...

    Hope, this helps somebody...

  • TektonTekton USMember ✭✭✭

    This thread helped me, on Friday. :) At one point, I thought I that this was working, in my project, in the Android emulator. Then, all of a sudden, it wasn't working. Must have tricked myself into thinking it was working lol. In the meanwhile, using Xam.Plugins.Settings is a good workaround, if you just need storage of basic types.

  • DavidCanoraDavidCanora USMember

    Not sure if this is related. I'm getting an exception saying Application.Current is Null. This is the (sanitized) code, which is in the app's MainPage:

     private bool TokensExist()
            {
                // The following line throws the exception
                if (Application.Current.Properties.ContainsKey("First") && Application.Current.Properties.ContainsKey("Second"))
                {
                    if (Application.Current.Properties["First"] as string != null &&
                        Application.Current.Properties["Second"] as string != null)
                    return true;
                }
                else
        {
                return false;
        }
            }
    

    Any thoughts why I'm getting the exception? Thanks in advance!

  • powerdudepowerdude USMember ✭✭

    The method by itself isn't helpful. When is it called? Has the App class been instantiated?

  • FredyWengerFredyWenger CHInsider ✭✭✭✭✭

    @DavidCanora: (and to all readers of this thread)

    There are further bug's with the debugger that don't see the Application-object (in iOS and Android, where it is "seen" in WP).
    I have filled the bug some time ago:
    https://bugzilla.xamarin.com/show_bug.cgi?id=26492

    There additionally are further major-problems with the iOS- and Android-debugger, that seems to depend on the individual environment (I'm not able to debug iOS and Android for months now, whereby other users seems to have much less problems).
    => See the Visual-Studio-Forum.

    Do you have the exception only, when you debug your app or also when you run your app on the device (crash or no crash)?

    So the bottom line to the properties for now is:

    • It works in principle, but not on some Android-devices (like my Samsung SM-T900 tablet)
    • The actual implementation also can cause some problems with suspending / reactivating and is not logical/full usable
      -> The developers (that's us :-) want to write/read the settings when they want to do it

    • I have filled bugs for that (don't work on some device, debugger-problems)

    • Jason know that
    • According to Jason, they work on a new API right now, that should solve the problems and really should be usable
      => Full control to write and read the properties

    So.. I hope the new API will be released soon....

    If not, I will take an add-on specially for the settings (I think there are at least two in the NuGet).

    Hope this helps...

  • DavidCanoraDavidCanora USMember

    This helps, thanks. I'll take a different route for now and keep an eye out for the new API. Cheers!

  • RogerSchmidlinRogerSchmidlin CHUniversity ✭✭✭

    I have similar problems with properties. I even do a Application.Current.SavePropertiesAsync. Strange thing is that is works when I debug my app. But as soon as I deploy my app (release) the properties get never saved. At the moment I am testing on Android.

  • dnzprmkszdnzprmksz TRMember ✭✭

    @RogerSchmidlin said:
    I have similar problems with properties. I even do a Application.Current.SavePropertiesAsync. Strange thing is that is works when I debug my app. But as soon as I deploy my app (release) the properties get never saved. At the moment I am testing on Android.

    I am suffering from the same issue on Android. Everything is fine on iOS debug/release and Android debug, however, when it comes to Android release build, it does not save even if I force it to save as Roger said.

    Does anyone know a workaround for that?

  • RogerSchmidlinRogerSchmidlin CHUniversity ✭✭✭

    I used the Settings plugin instead: https://github.com/jamesmontemagno/Xamarin.Plugins/tree/master/Settings. It's basic but it works.

  • GaryBeasleyGaryBeasley USMember ✭✭

    I have the same problem. Just published my app on Google Play. Now the Properties key value save that works in debug mode doesn't save in released version. I've been looking at the permissions in the manifest for android, and selected a few more I thought might do the trick. Waiting for response from xamarin android support as to which permissions need setting. Looks like I will have to use my Config sql lite db to read/write those values or try the plugin from James. Wow!

  • dnzprmkszdnzprmksz TRMember ✭✭

    @RogerSchmidlin Thanks, but I have to rewrite all lines that use App Properties to use this plugin. However, it seems like it is the only way out. Actually I was planning to use my sqlite db for this purpose too as @GaryBeasley said, but the plugin seems better for performance. Just a little :smiley:

  • JamesMontemagnoJamesMontemagno USForum Administrator, Xamarin Team, Developer Group Leader Xamurai

    Looks like perhaps an issue with the linker as this only happens in Release + Link SDK assemblies only.

    Workaround for Android:
    Add "Xamarin.Forms.Core" to your Ignore Assemblies

    iOS add a --linkskip

  • MarcoTMarcoT USMember ✭✭

    Hi @JamesMontemagno,
    I tried your workaround, but it didn't work,
    Do you have other ideas?

    Do you know if your plugin has the same problem?

    Thanks

  • HGiritzerHGiritzer ATMember ✭✭

    Hi, I just ran into the same issue...
    Works as expected with Android Debug, but does not persist settings with Android Release.
    Linker ignore assemblies Xamarin.Forms.Core as suggested by @JamesMontemagno does not change anything.
    Linker behavior setting to None helps (persistence is ok then).
    Xamarin Forms 2.1.0.6529, Visual Studio 2015.

  • pfedotovskypfedotovsky RUMember ✭✭

    Any updates? The workaround suggested by @JamesMontemagno does not help.
    Linker behavior set to None helps, but it dramatically increase APK size

  • TheosTheos NLBeta ✭✭

    Issue still exists (on Android at least).
    Just tested it on the latest XS Beta with the latest XF (2.2.0.31).
    Using the workaround to ignore the "Xamarin.Forms.Core" assembly doesn't work (anymore). As @pfedotovsky said, only setting the Linker to None will work.

  • KorayemKorayem EGMember ✭✭

    Same exact problem here :(

  • TheosTheos NLBeta ✭✭

    I can really advise to use Xam.Plugins.Settings (https://www.nuget.org/packages/Xam.Plugins.Settings/) package instead. It's really easy to implement and to replace for the Properties-implementation of XF.
    It solved this problem already for me.

  • ThibaultDThibaultD SEMember ✭✭✭

    Obviously I haven't tested my Android app in Release mode thoroughly enough.

    I released my app five months ago.
    I thought I'd seen some inconsistencies but I hadn't dug into the issue more than that.
    I've stumbled upon the issue now that I'm doing quality testing for a bigger update.
    Also, the issue isn't critical to me because the only things I've been storing there affecting the user were:
    1. Whether the user had discovered the menu (effect: Menu will always show at start, not so disturbing)
    2. My app offers the possibility to change language. That won't be saved either so restarting the app will send you back at the default language (system language).

    The second could be more annoying to the user but doesn't affect the usability of my application.

    My application was released with Xamarin Forms 1.5.1

  • ThibaultDThibaultD SEMember ✭✭✭

    The following steps seems to fix it:

    1. In Xamarin Studio
    2. Right click the Android project
    3. Under Build > Android Build
    4. Under Linker tab
    5. In Linker behavior dropdown
      Standard is Link SDK assemblies only, when I change to Don't link the issue is fixed.

    But I wonder what are the consequences of doing so?

  • JohnHardmanJohnHardman GBUniversity mod

    @ThibaultImbert - your app will get bigger

  • jelopezjelopez ESMember
    edited May 2016

    Same comment as @ThibaultD: under Visual Studio, modifying the Linker behaviour to None fix the problem.

  • ThibaultDThibaultD SEMember ✭✭✭

    @JohnHardman said:
    @ThibaultImbert - your app will get bigger

    Ah. Maybe that's not worth it. I wrote a simple Dao class that saves the preferences as Json and loads them when the application starts.

Sign In or Register to comment.