0

I am storing persistent data in my application using NSCoder like below:

class UserNotification: NSObject, NSCoding {

    var message: String!

    init(message: String) {
        self.message = message
    }

    required convenience init(coder aDecoder: NSCoder) {
        let message = aDecoder.decodeObject(forKey: "message") as! String

        self.init(message: message)
    }

    func encode(with aCoder: NSCoder) {
        aCoder.encode(message, forKey: "message")
    }
}

This is how they are loaded into the application:

let defaults = UserDefaults.standard

if (defaults.object(forKey: "userNotifications") != nil) {
        let decoded = defaults.object(forKey: "userNotifications") as! Data
        let decodedUserNotifications = NSKeyedUnarchiver.unarchiveObject(with: decoded as Data) as! [UserNotification]
        userNotifications = decodedUserNotifications
}

I also have included a way for the user to clear all the saved data. For clearing, I have implemented:

let newListData = NSKeyedArchiver.archivedData(withRootObject: userNotifications)
let defaults = UserDefaults.standard
defaults.set(newListData, forKey: "userNotifications")
defaults.synchronize()

All user notification objects are stored in a globally accessible array:

userNotifications = [UserNotification]()

I would like to make sure that all the persistent data is removed once the user has cleared them. I was looking through the folder of my simulator (~/Library/Developer/CoreSimulator/Devices) in order to find the persistent data and to see if it gets removed, but I have not managed to find it anywhere.

Everything seems to work when running the application (saving data, erasing it etc.) but I would like to make sure that all data get erased correctly on the device. There are some thumbnails being saved and therefore I want to make sure it really becomes erased and not taking up unnecessary space.

So my question is where the data is stored when using NSCoder? I am able to find locally stored images from using FileManager, but the encoded data is nowhere to be found.

drante
  • 129
  • 9
  • How are you actually saving your objects once you've encoded them using NSCoder? – Andy Apr 26 '18 at 12:52
  • I added more details to the code now. After having encoded the data, it is stored in the userNotifications array, which is then saved to the device. – drante Apr 26 '18 at 13:40
  • As far as I can see you're not actually saving the data anywhere. Try using the `UserDefaults.set(...)` method to store your data if that's where you want to save it. NSCoder is for encoding data, it doesn't actually do any saving or archiving for you. – Andy Apr 26 '18 at 13:48
  • Oh, my bad, I think my question is a bit unclear. The data gets saved correctly and everything seems to work. The only issue I have is being able to access the files in Finder. The reason I want to access them is to make sure all persistent data gets erased whenever the user clears the notifications. – drante Apr 26 '18 at 13:50

0 Answers0