我编写了一个写入文件的C ++代码,并尝试将其刷新到物理磁盘。在代码结束时,我想知道该文件是否已写入物理磁盘并提交即使有人拔掉机器也处于稳定状态。然而,当我在执行以下所有行后立即拔出电源时,文件丢失,即它还没有写入物理磁盘,尽管我试图刷新并使用FILE_FLAG_WRITE_THROUGH。
import UIKit
import MapKit
import CoreLocation
class ViewController: UIViewController, CLLocationManagerDelegate {
var locationManager = CLLocationManager()
var location: CLLocation!
var newAnnotation: String?
@IBOutlet weak var map: MKMapView!
@IBAction func refreshMap(sender: AnyObject) {
locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.startUpdatingLocation()
location = locationManager.location
locationManager.stopUpdatingLocation() //not my question right now but I'm pretty sure this is dumb
let center = CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)
let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01))
self.map.setRegion(region, animated: true)
}
override func viewDidLoad() {
super.viewDidLoad()
refreshMap(self.map)
let uilpgr = UILongPressGestureRecognizer(target: self, action: "addPlace:")
uilpgr.minimumPressDuration = 1
map.addGestureRecognizer(uilpgr)
}
//this is the method in question
func addPlace(gestureRecognizer:UIGestureRecognizer) {
if gestureRecognizer.state == UIGestureRecognizerState.Began {
let touchPoint = gestureRecognizer.locationInView(self.map)
let newCoordinate: CLLocationCoordinate2D = map.convertPoint(touchPoint, toCoordinateFromView: self.map)
let annotation = MKPointAnnotation()
annotation.coordinate = newCoordinate
let alert = UIAlertController(title: "New Memorable Place", message: "Enter a description for this location", preferredStyle: .Alert)
alert.addTextFieldWithConfigurationHandler({ (textField) -> Void in
textField.text = "Description"
})
alert.addAction(UIAlertAction(title: "OK", style: .Default, handler: { (action) -> Void in
self.newAnnotation = alert.textFields![0].text
}))
self.presentViewController(alert, animated: true, completion: nil)
annotation.title = newAnnotation
map.addAnnotation(annotation)
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let location = locations.last! as CLLocation
let center = CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)
let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01))
self.map.setRegion(region, animated: true)
}
}
如何确保该文件已存在于磁盘上,以便我可以提交更改?