# Saving app data between updates

**URL:** <https://forum.xojo.com/t/saving-app-data-between-updates/29940>\
**Category:** iOS\
**Created:** [February 23, 2016, 7:00pm UTC](https://forum.xojo.com/t/saving-app-data-between-updates/29940 "2016-02-23T19:00:37Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![Art\_Gorski](https://forum.xojo.com/user_avatar/forum.xojo.com/art_gorski/32/704_2.png) [@Art\_Gorski](https://forum.xojo.com/u/Art_Gorski)\
**Post date:** [February 23, 2016, 7:00pm UTC](https://forum.xojo.com/t/saving-app-data-between-updates/29940/1 "2016-02-23T19:00:37Z")

</div>

I save app data to the Documents folder for restore between app launches, but what happens if the app is updated with a new version? Is this data preserved or just lost?

Do I need to enable the feature to allow the user to backup the data manually via the iTunes app as a workaround?

---

<div class="post-metadata">

**Author:** ![Paul\_Lefebvre](https://forum.xojo.com/user_avatar/forum.xojo.com/paul_lefebvre/32/3152_2.png) [@Paul\_Lefebvre](https://forum.xojo.com/u/Paul_Lefebvre)\
**Post date:** [February 23, 2016, 7:05pm UTC](https://forum.xojo.com/t/saving-app-data-between-updates/29940/2 "2016-02-23T19:05:56Z")

</div>

In my experience, updating an app does not change the contents of its Documents folder. Deleting it will remove the Documents folder, however.

I have not found an official Apple doc on this, but several [StackOverflow questions](http://stackoverflow.com/questions/9311748/when-you-update-an-ios-app-what-happens-to-the-documents-folder-contents) about this seem to agree.

---

<div class="post-metadata">

**Author:** ![DerkJ](https://forum.xojo.com/letter_avatar_proxy/v4/letter/d/e495f1/32.png) [@DerkJ](https://forum.xojo.com/u/DerkJ)\
**Post date:** [February 23, 2016, 7:08pm UTC](https://forum.xojo.com/t/saving-app-data-between-updates/29940/3 "2016-02-23T19:08:46Z")

</div>

I think you need NSUserDefaults, i’m working on testing that part of code just now from iOSLib.

You can read it on apple here: [Apple UserDefaults Documentation](https://developer.apple.com/library/prerelease/ios/documentation/Cocoa/Conceptual/UserDefaults/Introduction/Introduction.html)

If you think it is useful and working for your app let me (or Ulrich) know.

---

<div class="post-metadata">

**Author:** ![Jason\_King](https://forum.xojo.com/letter_avatar_proxy/v4/letter/j/e480ec/32.png) [@Jason\_King](https://forum.xojo.com/u/Jason_King)\
**Post date:** [February 23, 2016, 7:17pm UTC](https://forum.xojo.com/t/saving-app-data-between-updates/29940/4 "2016-02-23T19:17:39Z")

</div>

NSUserDefaults works between updates and is how you should save preferences and other data that aren’t documents/files. It’s also available in the Foundation module of iOSKit.

Edit: aren’t not are

---

<div class="post-metadata">

**Author:** ![Art\_Gorski](https://forum.xojo.com/user_avatar/forum.xojo.com/art_gorski/32/704_2.png) [@Art\_Gorski](https://forum.xojo.com/u/Art_Gorski)\
**Post date:** [February 23, 2016, 7:25pm UTC](https://forum.xojo.com/t/saving-app-data-between-updates/29940/5 "2016-02-23T19:25:37Z")

</div>

Thanks, everyone!

---

<div class="post-metadata">

**Author:** ![Michel\_Bujardet](https://forum.xojo.com/user_avatar/forum.xojo.com/michel_bujardet/32/252_2.png) [@Michel\_Bujardet](https://forum.xojo.com/u/Michel_Bujardet)\
**Post date:** [February 23, 2016, 7:32pm UTC](https://forum.xojo.com/t/saving-app-data-between-updates/29940/6 "2016-02-23T19:32:26Z")

</div>

As long as you keep the same subfolder and file structure for your save, it will just work the same in all updates.

---

<div class="post-metadata">

**Author:** ![DerkJ](https://forum.xojo.com/letter_avatar_proxy/v4/letter/d/e495f1/32.png) [@DerkJ](https://forum.xojo.com/u/DerkJ)\
**Post date:** [February 23, 2016, 8:02pm UTC](https://forum.xojo.com/t/saving-app-data-between-updates/29940/7 "2016-02-23T20:02:47Z")

</div>

I’ve tested the working of the AppleUserDefaults in (iOSLib) it works good.  
It’s undocumented for now so if you need help call out.

---

<div class="post-metadata">

**Author:** ![Jason\_King](https://forum.xojo.com/letter_avatar_proxy/v4/letter/j/e480ec/32.png) [@Jason\_King](https://forum.xojo.com/u/Jason_King)\
**Post date:** [February 23, 2016, 8:07pm UTC](https://forum.xojo.com/t/saving-app-data-between-updates/29940/8 "2016-02-23T20:07:22Z")

</div>

NSUserDefaults (or AppleUserDefaults in iOSLib) is pretty straightforward to work with. The only “gotcha” is to make sure you call the synchronize method after you save/change any values because they will not be saved automatically unless you synchronize.

---

<div class="post-metadata">

**Author:** ![Jason\_Tait](https://forum.xojo.com/user_avatar/forum.xojo.com/jason_tait/32/5896_2.png) [@Jason\_Tait](https://forum.xojo.com/u/Jason_Tait)\
**Post date:** [February 23, 2016, 8:31pm UTC](https://forum.xojo.com/t/saving-app-data-between-updates/29940/9 "2016-02-23T20:31:29Z")

</div>

> [@249036:@Jason King](#):
>
> NSUserDefaults (or AppleUserDefaults in iOSLib) is pretty straightforward to work with. The only “gotcha” is to make sure you call the synchronize method after you save/change any values because they will not be saved automatically unless you synchronize.

Jason are you sure about this? I’ve been using your NSUserDefaults since it first appeared in iOSKIt and I’ve never called, nor needed to call, the synchronize method.

---

<div class="post-metadata">

**Author:** ![Jason\_King](https://forum.xojo.com/letter_avatar_proxy/v4/letter/j/e480ec/32.png) [@Jason\_King](https://forum.xojo.com/u/Jason_King)\
**Post date:** [February 23, 2016, 8:34pm UTC](https://forum.xojo.com/t/saving-app-data-between-updates/29940/10 "2016-02-23T20:34:10Z")

</div>

Well synchronize is called automatically by iOS periodically after a change. But if the app quits before it is called then the changes will be lost. So although it works, you could come across a situation where it is not saved (I came across this when testing it originally) and this is why I say that.

---

<div class="post-metadata">

**Author:** ![Jason\_Tait](https://forum.xojo.com/user_avatar/forum.xojo.com/jason_tait/32/5896_2.png) [@Jason\_Tait](https://forum.xojo.com/u/Jason_Tait)\
**Post date:** [February 23, 2016, 8:37pm UTC](https://forum.xojo.com/t/saving-app-data-between-updates/29940/11 "2016-02-23T20:37:48Z")

</div>

I see. I only use it for settings in a series of views similar to the iOS Settings app, and as these are rarely changed then presumably nobody has experienced this issue. I’ll update it to call synchronize when leaving the views.

---

<div class="post-metadata">

**Author:** ![Jason\_King](https://forum.xojo.com/letter_avatar_proxy/v4/letter/j/e480ec/32.png) [@Jason\_King](https://forum.xojo.com/u/Jason_King)\
**Post date:** [February 23, 2016, 8:46pm UTC](https://forum.xojo.com/t/saving-app-data-between-updates/29940/12 "2016-02-23T20:46:16Z")

</div>

That sounds like you should be fine. Its most important if you are saving something in response to the app quitting where you will lose changes. The apple docs are fairly shady on this.

---

<div class="post-metadata">

**Author:** ![Antonio\_Rinaldi](https://forum.xojo.com/user_avatar/forum.xojo.com/antonio_rinaldi/32/331_2.png) [@Antonio\_Rinaldi](https://forum.xojo.com/u/Antonio_Rinaldi)\
**Post date:** [February 23, 2016, 11:28pm UTC](https://forum.xojo.com/t/saving-app-data-between-updates/29940/13 "2016-02-23T23:28:04Z")

</div>

Documents folder remain untouched and all the backup will save it.  
Cache folder should be emptied and is not part of the backup  
NSUserDefault should be used for preference and settings, not for data.

---

<div class="post-metadata">

**Author:** ![C\_T\_Baumgartner](https://forum.xojo.com/user_avatar/forum.xojo.com/c_t_baumgartner/32/780_2.png) [@C\_T\_Baumgartner](https://forum.xojo.com/u/C_T_Baumgartner)\
**Post date:** [February 26, 2016, 3:47pm UTC](https://forum.xojo.com/t/saving-app-data-between-updates/29940/14 "2016-02-26T15:47:51Z")

</div>

Derk Jochems said [quote]I’ve tested the working of the AppleUserDefaults in (iOSLib) it works good.  
It’s undocumented for now so if you need help call out.[/quote]  
Jason King said [quote]NSUserDefaults (or AppleUserDefaults in iOSLib) is pretty straightforward to work with.[/quote]

I spent a many hours trying to figure out how to make NSUserDefaults work with iOSLib. I just don’t have the knowledge to do it. I got stopped when trying to create the defaults Dictionary. If someone would post sample code there would probably be many grateful developers, myself among them.

---

<div class="post-metadata">

**Author:** ![DaveS](https://forum.xojo.com/letter_avatar_proxy/v4/letter/d/848f3c/32.png) [@DaveS](https://forum.xojo.com/u/DaveS)\
**Post date:** [February 26, 2016, 4:27pm UTC](https://forum.xojo.com/t/saving-app-data-between-updates/29940/15 "2016-02-26T16:27:19Z")

</div>

if it might help anyone… here is the SWIFT code I wrote for loading/saving optinos

```auto
let userDefaults = NSUserDefaults.standardUserDefaults()

func putOption(key:String,_ optionValue:Any) {
    switch optionValue {
    case let someBool as Bool:
        userDefaults.setBool(someBool, forKey: key)
    case let someDouble as Double:
        userDefaults.setDouble(someDouble, forKey: key)
    case let someFloat as Float:
        userDefaults.setFloat(someFloat, forKey: key)
    case let someInt as Int:
        userDefaults.setInteger(someInt, forKey: key)
    case let someString as String:
        userDefaults.setObject(someString, forKey: key)
    case let someColor as UIColor:
        userDefaults.setInteger(Int(someColor.IntValue()), forKey: key)
    default:
        break
    }
}
 func getOption(key:String, defaultValue:AnyObject) -> AnyObject {
    if let retValue:AnyObject = userDefaults.objectForKey(key) {
        return retValue
    } else {
        putOption(key,defaultValue) // insure the option exists in pList
        return defaultValue
    }
}
```

---

<div class="post-metadata">

**Author:** ![Jason\_King](https://forum.xojo.com/letter_avatar_proxy/v4/letter/j/e480ec/32.png) [@Jason\_King](https://forum.xojo.com/u/Jason_King)\
**Post date:** [February 26, 2016, 4:28pm UTC](https://forum.xojo.com/t/saving-app-data-between-updates/29940/16 "2016-02-26T16:28:34Z")

</div>

(For iOSKit) you call NSUserDefaults.StandardUserDefaults. This gives you the “dictionary” to work with. Then you can call the different methods to save/restore values. For example SetDoubleForKey sets a double value for a string/text key as the name implies. The other functions work the same. If you have more specific questions, just ask.

---

<div class="post-metadata">

**Author:** ![system](https://forum.xojo.com/uploads/default/original/1X/455b4dcc0e61630e9f81940004ddffa49c432a46.png) [@system](https://forum.xojo.com/u/system)\
**Post date:** [October 29, 2020, 6:12pm UTC](https://forum.xojo.com/t/saving-app-data-between-updates/29940/17 "2020-10-29T18:12:14Z")

</div>


