Andre M
Andre M

Reputation: 7554

Files removed from app, but still occupying same space?

When I remove my app's files via iTunes, the space is still occupied until I delete and reinstall the app. If I list the contents of my app's document directory it indicates there are no files, so I am wondering if there is the equivalent of a trash folder. If not, what should I be doing to recover this space?

This app is making use of the UIImagePickerController to record videos, which I then save in my app's local storage space. It is these recordings that are exposed to iTune's ("Application supports iTunes file sharing" in plist), so that I can add and delete files during development time.

The code I use to save the file to the app's storage space:

extension VideoRecorderViewController: UIImagePickerControllerDelegate {

    func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {

        let mediaType = info[UIImagePickerControllerMediaType]
        if mediaType!.isEqualToString(kUTTypeMovie as String) {
            let documentsUrl =  NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask).first!

            if let mediaURL = info[UIImagePickerControllerMediaURL] as? NSURL {

                let dateFormatter = NSDateFormatter()
                dateFormatter.timeZone = NSTimeZone(name: "UTC")
                dateFormatter.dateFormat = "yyyyMMddHHmmss"
                let baseFilename = dateFormatter.stringFromDate(NSDate())

                // Write out to application's storage area
                let videoData = NSData(contentsOfURL: mediaURL)
                let dataPath = documentsUrl.filePathURL?.path?.stringByAppendingPathComponent("\(baseFilename).MOV")
                videoData?.writeToFile(dataPath!, atomically: true)
            }
        }
    }
}

The code I am using to list the files in the app's storage space:

let directoryUrls = try  NSFileManager.defaultManager().contentsOfDirectoryAtURL(documentsUrl, includingPropertiesForKeys: nil, options: NSDirectoryEnumerationOptions())

print ("all files:")
for entry in directoryUrls {
    let directoryItem : NSURL = entry
    print("   ", directoryItem.absoluteString)
}

Also tried deleting from the TemporaryDirectory:

func clearTmpDirectory() {

    do {
        let tmpDirectoryFiles  =  try NSFileManager.defaultManager().contentsOfDirectoryAtPath(NSTemporaryDirectory())
        print ("all files:")
        for entry in tmpDirectoryFiles {
            print("Removing: ", entry)
            do {
                try NSFileManager.defaultManager().removeItemAtPath(entry)
            } catch let error as NSError {
                print("error", error)
            }
        }
    } catch let error as NSError {
        print("error", error)
    }
}

I have tried looking around for answers, but I haven't found anything yet. Can anyone suggest what I might have overlooked?

BTW I did check both the CachesDirectory and the MoviesDirectory. The former exists, but no files taking space and the latter does not exist. Just also checked the UserDirectory, but that returns a nil list.

Upvotes: 0

Views: 68

Answers (1)

Andre M
Andre M

Reputation: 7554

While I am still not sure why clearing the TemporaryDirectory did not free the space I as hoping for (insight would still be appreciated), changing the code for storing the code that persists the video capture helped. I changed the writeToFile approach to a file move, such that:

do {

    try NSFileManager.defaultManager().moveItemAtPath(mediaURL.path!, toPath: dataPath!)

} catch let error as NSError {
    print("Error: ", error)
}

Upvotes: 0

Related Questions