Tejaswi Yerukalapudi
Tejaswi Yerukalapudi

Reputation: 9157

NSKeyedUnarchiver - delete decoded data?

I'm encoding a few complex objects with NSKeyedArchiver and saving it to disk. Say, something like -

Class member {
    int *id;
    NSString *name;
    NSMutableArray *array;
    TempClass *object;
}

The functionality I'm trying to build is for the user to be able to save his work, lets say, while creating a new member and come back to it later. When the user finishes up, he clicks post and the data will be transmitted to a web service. If not, he just clicks save and leaves the screen and the data is persisted, so that the app can resume from that point when the user comes back. Now, once I've posted the data to the web service, I do not want to keep the data in the disk anymore and I can't really find a way to delete it.

Now, my encoding and decoding classes are functioning fine. I can use NSKeyedArchiver to save the data to disk and retrieve it using NSKeyedUnarchiver. But, my question is, how can I delete the data that I don't need anymore? Do I have to manually delete the file on the disk? Is there any way to get NSKeyedUnarchiver to delete the data that's it's returning?

Upvotes: 18

Views: 7125

Answers (5)

MhmdRizk
MhmdRizk

Reputation: 1721

For Swift 3.0 -> 4.1:

let fileManager = FileManager()
let fileName = "your_file_name"

//In Order to get your file path correctly
getFileURL(_ fileName: String) -> String? {
    let fileURL = fileManager.urls(for: fileManager.SearchPathDirectory.documentDirectory, in: fileManager.SearchPathDomainMask.userDomainMask).first
    return (fileURL?.appendingPathComponent(fileName).path)
}


//Persist Data
func persistData(_ data : Data) -> Bool{
     return NSKeyedArchiver.archiveRootObject(data, toFile: getFileURL(fileName)!)
}

//Get Persisted Data
func getArchivedData() -> Data?{
    return NSKeyedUnarchiver.unarchiveObject(withFile: getFileURL(fileName)!) as? Data
}

//Delete Persisted Data 
func deleteArchivedUser() -> Bool{
    do {
        try fileManager.removeItem(atPath: getFileURL(fileName)!)
        return true
    } catch _ {
        return false
    }
}

Upvotes: 4

mrc5
mrc5

Reputation: 79

A Swift3 example:

do {
 try FileManager.default.removeItem(atPath: path)
} catch {
 // catch potential error
}

Upvotes: 8

Jegadees Waran
Jegadees Waran

Reputation: 51

For Swift 2.0:

func deleteFile(path: String) -> Bool{
    let exists = NSFileManager.defaultManager().fileExistsAtPath(path)
    if exists {
        do {
            try NSFileManager.defaultManager().removeItemAtPath(path)
        }catch let error as NSError {
            print("error: \(error.localizedDescription)")
            return false
        }
    }
    return exists
}

Upvotes: 5

Phil
Phil

Reputation: 4870

For Swift 2.0:

do {
  try NSFileManager.defaultManager().removeItemAtPath("Your_PATH")
} catch {

}

Upvotes: 2

Frank C.
Frank C.

Reputation: 8088

A very simple way to just delete it programmatically once you have posted the data:

- (BOOL) deleteFile:(NSString *) pathOfFileToDelete error:(NSError *)err {
    BOOL exists = [[NSFileManager defaultManager] fileExistsAtPath: pathOfFileToDelete];
    if(exists) { 
        [[NSFileManager defaultManager]removeItemAtPath: pathOfFileToDelete error:err];
    }
    return exists;
}

Upvotes: 24

Related Questions