user7427067
user7427067

Reputation: 1

Data in my iOS app vanishes after a couple of days

I have an iPhone app that stores data locally in an array. I was testing the app on my actual iPhone and it works fine except that the data vanishes after a couple of days. I am curious to see if this has happened to anyone else and if a solution was found. Here is a sample of the code that stores the data:

var doctorsArray = [Doctors]()

func storeDoctorsArray() {
    doctorsArray.sort(by: {(a: Doctors, b: Doctors) -> Bool in
        return a.firstName < b.firstName
    })

    let defaults = UserDefaults.standard
    let data = NSKeyedArchiver.archivedData(withRootObject:     doctorsArray)
    defaults.set(data,forKey: "stored_doctors_data")
    defaults.synchronize()
} 

And to get data:

func loadDoctorsArray() {
    if let storedArray = UserDefaults.standard.object(forKey: "stored_doctors_data") as? Data {
        doctorsArray = NSKeyedUnarchiver.unarchiveObject(with: storedArray) as! [Doctors]

Again as mentioned, the app works fine and the data stores and displays as expected but vanishes after a couple of days.

Upvotes: 0

Views: 44

Answers (1)

Duncan C
Duncan C

Reputation: 131426

If you delete the app it clears the contents of UserDefaults. Other than that, or resetting the simulator, UserDefaults should persist between runs.

How/when are you calling your loadDoctorsArray() function

Upvotes: 1

Related Questions