Application properties data is lost

Hi,

I have an application that uses application properties to store data. Everything works fine on android but on WP the data is lost when I restart the app. Any ideas why this happens?

I am referring to this Working with the App Lifecycle... In the chapter "persistence" it is quite clearly stated that "Data added to the dictionary will be available when the application returns from the background or even after it is restarted". For me it only works on resume but on restart...

Thanks in advance.

Answers

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    I always use @JamesMontemagno Settings plugin. You can take a look.

  • Dave81Dave81 FIMember

    Thanks but I need to store complex objects.. in a list. Other suggestions? Or a solution maybe? ;)

  • Dave81Dave81 FIMember

    Or maybe I'll just store json strings?

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    Yes, JSon it's a good solution

  • Dave81Dave81 FIMember

    Hi again,

    I've been testing the settings plugin for a while now and...

    Good news
    It seems to fix the problem on windows phone 8.1.
    Bad news
    It breaks the previously working settings on android devices.

  • ThomasBurkhartThomasBurkhart DEMember ✭✭✭✭

    I always use JSON.net for complex objects. Together with PCLStorage you have all you need

  • Dave81Dave81 FIMember
    edited February 2016

    I made my application to store everything in json. That wasn't the problem though.

    The problem now with android is the same it was with windows phone before I switched to the @JamesMontemagno's plugin... The settings are stored but only as long as the application is running. If I kill the app and start it again, all the stored settings are lost.

    Isn't PCLStorage a bit heavy to store just a bunch of strings?

  • ThomasBurkhartThomasBurkhart DEMember ✭✭✭✭

    Ok, you spoke of complex objects. But how do you manage file IO cross platform without PCLStorage?

  • Dave81Dave81 FIMember
    edited February 2016

    As I said. I convert everything into json strings before storing. So basically I have just a few (very long) strings. The xamarin forms has Application.Current.Properties that I first used (worked fine with android) and then I switched to Montemagno's plugin. Both solutions are cross platform ready directly from the PCL project.

    Or maybe I didn't understand your question..? Want to rephrase? :)

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    without PCLStorage you have to manage files natively
    BTW, I suggest to take a look at @JamesMontemagno plugin... I use it in four project (iOS, Android... not WP) and for me it works without problem

  • Dave81Dave81 FIMember

    Ok. I take it all back. I tested both solutions and it seems that both ways all the settings are lost for both platforms.. Any ideas?

  • Dave81Dave81 FIMember
    edited February 2016

    @AlessandroCaliaro said:
    without PCLStorage you have to manage files natively
    BTW, I suggest to take a look at @JamesMontemagno plugin... I use it in four project (iOS, Android... not WP) and for me it works without problem

    What do you mean by I have to manage files natively? I don't quite get the idea.

    I am using @JamesMontemagno plugin currently and it does not work for me.

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    I think that System.IO is not full working in PCL because file access is different from Android to iOS, so you should create your IO methods in iOS and in Android and call it in PCL with DependencyService. So, use PCLStorage because the work is already done.

  • Dave81Dave81 FIMember

    Right. I get your point... sort of. But what does file IO have to do with android shared preferences or windows phone isolated storage settings? I though @JamesMontemagno plugin would work from the PCL. At least there is no mention about what you just said... But if it is really like that then I will switch to PCLStorage. Can you point out a good example of how to use the PCLStorage for storing settings?

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    PCLStorage writes/reads files. Here you can find some informations

    https://github.com/dsplaisted/PCLStorage

  • Dave81Dave81 FIMember

    Thanks. I read that. What understand is that it will create files. Are these files accessible through the app only? Because I don't want users to get access to my data from outside my app.

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    I don't know... BTW, you could encrypt your strings

  • Dave81Dave81 FIMember

    And should Montemagno's plugin work without the dependency service?

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    Yes, all plugins works without dependency service. They are all PCL

  • Dave81Dave81 FIMember

    @AlessandroCaliaro said:
    Yes, all plugins works without dependency service. They are all PCL

    I wonder why it doesn't work for me. I must be doing something wrong. I just don't know what. Do I need to give the app an extra permission maybe?

  • Dave81Dave81 FIMember

    This is so odd... Montemagno's plugin seems to work if I use it directly from my app.cs. I will figure this out and share my findings later.

    Thanks.

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

    You just have to install the plugins' NuGet into all projects. I even put a Helpers/Settings.cs file to show you how to use it properly. It is simply Key/Value pairs of data that are stored in the underlying preferences of each system. I have used it for 4 years in over 60 apps.

  • Dave81Dave81 FIMember

    That's what I did. It is super simple to use. For me it just loses the stored data in some cases. I'm trying to figure out why that happens.

  • Dave81Dave81 FIMember

    Ok. It seems that there something wrong with my app that I can't explain. I did some cleaning+refactoring and it started to work.

    Sorry and thank you.

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    @Dave81 no problem. It's important that now it works

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    @Dave81 no problem. It's important that now it works

  • IT-AleXIT-AleX RUMember ✭✭

    if you put in Properties not simple type data - on restart - you lost it!
    if you put in poroperties same class info - its life while app dont restart!...
    I think if you remove on app force close not simpe type property - your simple type info not will be lost!

Sign In or Register to comment.