1

Overview: I am developing my first application. It collects user location information for tracking a bicycle ride. I save the contents to a .txt file (coordinate information and attribute information) The user can then export their data out of the iOS application for further analysis. I have successfully added a save text file function to store the information.

App while in use: A user enters a custom title and then that name is appended to a tableview that lists all recorded rides. In the ride creation I call NSUserDefaults, this is critical, without this call to NSUserDefaults, the ride name does not get appended to the tableview listing all the rides.

let saveAction = UIAlertAction(title: "Save", style: .Default,
        handler: { (action:UIAlertAction) -> Void in

            // Allow for text to be added and appended into the RideTableViewController
            let textField = alert.textFields!.first
            rideContent.append(textField!.text!)
            // Update permanent storage after deleting a ride
            NSUserDefaults.standardUserDefaults().setObject(rideContent, forKey: "rideContent")

Now when a user is viewing past rides they can select an old ride and this loads information, everything is good there!

Problem: Somehow what happens after a while, is a ride will get overwritten/lost? When testing on an iPhone, I can record a ride, then several hours later I check past rides in the tableview and the ride is there, but if I record a new ride and then check past rides the ride recorded several hours earlier is gone, but the new ride is there.

The only other times I call NSUserDefaults is in the tableview. The following instances where I call it are here in the viewDidLoad:

override func viewDidLoad() {
    super.viewDidLoad()

    // Set up an if statement so that if there is no saved content it does not throw an error, if there is, populate the information with rideContent, if not leave the list as empty
    if NSUserDefaults.standardUserDefaults().objectForKey("rideContent") != nil {

        // Save content of rideContent as permanent storage so the ride is saved even when the app has been closed, this restores the old data
        rideContent = NSUserDefaults.standardUserDefaults().objectForKey("rideContent") as! [String]

    }

}

And further down when I call it if a user swipes to delete a file:

 // This method will be called when a user tries to delete an item in the table by swiping to the left
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {

    // create an if statement to select the delete function after a user swipes to the left on a cell in the table
    if editingStyle == UITableViewCellEditingStyle.Delete {

        //Delete an item at the row index level
        rideContent.removeAtIndex(indexPath.row)

        // Update permanent storage after deleting a ride
        NSUserDefaults.standardUserDefaults().setObject(rideContent, forKey: "rideContent")

        // This is called so that when a file has been deleted, it will reload the view to reflect this change
        savedRideTable.reloadData()

    }

}

I guess if the following code seems normal, does this just mean that saving to the user documents does not offer true permanent storage of files? Is there a better place to store these files? I hope I am not too vague, I have a hard time tracking down why the app does this, or what exactly is triggering this to happen. Thank you stackExchange for your help!

Nikolai
  • 243
  • 3
  • 15
  • `NSUserDefaults` is really supposed to be used for storing settings, not app data. I would use CoreData for what you're trying to do. – dan Mar 04 '16 at 22:27

1 Answers1

1

This is the normal behaviour of NSUserDefaults. You aren't saving a txt file, you are storing a String in NSUserDefaults, these are two different things even if the final result can be similar.
When you call:

NSUserDefaults.standardUserDefaults().setObject(rideContent, forKey: "rideContent")

a second time the previous value is replaced with the new one.
You have three options:

Community
  • 1
  • 1
LorenzOliveto
  • 7,796
  • 1
  • 20
  • 47