I have problems in handling the persistence of some classes in the cache. This is the code I tried and that does not work at all either recovering the archived data or deleting it:
func cacheDirectoryURL()->NSURL?{
let fileManager=NSFileManager()
let urls = fileManager.URLsForDirectory(.CachesDirectory, inDomains: .UserDomainMask)
if urls.count>0{
return urls[0]
}
return nil
}
func palineCacheURL()->NSURL?{
let url=cacheDirectoryURL()?.URLByAppendingPathExtension("Paline.archive")
return url
}
public func getMapHandlerWithDelegate(delegate:MovableAnnotationDelegate)->MapDelegate{
let archiveURL=palineCacheURL()!
mapHandler=NSKeyedUnarchiver.unarchiveObjectWithFile(archiveURL.absoluteString) as? MapDelegate
if mapHandler != nil{
mapHandler!.delegate=delegate;
} else {
mapHandler=MapDelegate.sharedMapDelegate()
mapHandler!.delegate=delegate;
}
return mapHandler!
}
func clearMapArchives(){
do{
try NSFileManager.defaultManager().removeItemAtPath(palineCacheURL()!.absoluteString);
} catch let error as NSError {
print(error.localizedDescription+" "+(error.localizedFailureReason ?? ""));
let alert=UIAlertView(title:NSLocalizedString("Error", comment:""), message:NSLocalizedString("Unable to clean the cache; please reinstall the app in the case of problems", comment:""), cancelButtonTitle:NSLocalizedString("Dismiss", comment:""), otherButtonTitle:nil, onDismiss:nil, onCancel:nil)
alert.show()
}
}
func archive(){
//-NSLog(@"archivePath=%@ paline=%@", archivePath, self.palineArray);
if mapHandler != nil {
NSKeyedArchiver.archiveRootObject(mapHandler!, toFile: palineCacheURL()!.absoluteString)
print("archivio a \(palineCacheURL()!.absoluteString)")
}
}
I even tried retrieving the object soon after saving it and it come out nil:
NSKeyedArchiver.archiveRootObject(mapHandler!, toFile: palineCacheURL().absoluteString)
print("archivio a \(palineCacheURL().absoluteString)")
let restored=NSKeyedUnarchiver.unarchiveObjectWithFile(palineCacheURL().absoluteString) as? MapDelegate
print("restored \(restored)")
Thanking much fluidsonic for the hint, the code was fine with the exception of absoluteString to be replaced with path. Once replaced this part everything work seamlessly.