Search code examples
iosxamarinhockeyapp

iOS app saved data lost after updating


I have an iPhone app that I developed with Xamarin and am publishing on HockeyApp. Whenever I put a new version of the app on HockeyApp and someone updates their current installation on their phone, they lose the saved data. Is there any way to prevent this?

EDIT

I have an entitlement that let's me share the data with my widget too. Could that be the problem? This is how I'm writing/reading the data:

this.nsUserDefaults = new NSUserDefaults("myGroupId", NSUserDefaultsType.SuiteName);

// Write data:
this.nsUserDefaults.SetString("myValue", "myKey");
this.nsUserDefaults.Synchronize();

// Read data:
string myValue = this.nsUserDefaults.StringForKey("myKey");

EDIT

After changing the above code to the following, it now persists saved data after updating:

// Write data:
NSUserDefaults.StandardUserDefaults.SetString("myValue", "myKey");
NSUserDefaults.StandardUserDefaults.Synchronize();
// Read data:
string myValue = NSUserDefaults.StandardUserDefaults.StringForKey("myKey");

But now I won't be able to share data with my widget...how can I solve this while still being able to share the data with my widget?


Solution

  • If you try to read the data before calling this.nsUserDefaults.Synchronize();, you won't get the data.

    So if you do:

    this.nsUserDefaults = new NSUserDefaults("myGroupId", NSUserDefaultsType.SuiteName);
    
    // Read data:
    string myValue = this.nsUserDefaults.StringForKey("myKey");
    

    You won't get the data. But if you call the Synchronize() method before the read you will get the data:

    this.nsUserDefaults = new NSUserDefaults("myGroupId", NSUserDefaultsType.SuiteName);
    this.nsUserDefaults.Synchronize();
    
    // Read data:
    string myValue = this.nsUserDefaults.StringForKey("myKey");