I've had an odd experience this afternoon and I can't put my finger on what the issue could be. I have been developing an iPhone app using Swift and I took it out on the road for a test run today. Up until now I've solely tested it whilst connected to the Mac.
The app tracks locations and saves this to Core Data as the user drives along. Everything was fine and the data was being saved. That was until the phone crashed and the Core Data seems to have emptied. The data was definitely being saved before the phone crashed, as I overlay the saved route on a map view and it was definitely displaying. And the function to delete the data was definitely not called as I didn't access the only view controller which calls it.
My question is, has anyone else experienced this or is there a known issue whereby Core Data can be deleted if a phone crashes?
Edit:
This is the code I've been using to save to Core Data:
var context = (UIApplication.sharedApplication().delegate as AppDelegate).managedObjectContext!
var savePosition = NSEntityDescription.insertNewObjectForEntityForName("Positions", inManagedObjectContext: context) as Positions
savePosition.poi = false
savePosition.time = currentTime
savePosition.direction = currentDirection
savePosition.latitude = currentLatitude
savePosition.longitude = currentLongitude
savePosition.game = currentGame
context.save(nil)
Am I missing a step?
With this function in AppDelegate you catch errors and display them in an UIAlertController
. This may help to track down what happened to your data.
func saveContext() {
var error: NSError? = nil
if !managedObjectContext.hasChanges {
return
}
if managedObjectContext.save(&error) {
return
}
// Error occured
let alertController = UIAlertController(
title: NSLocalizedString("Error", comment: ""),
message: "\(error?.localizedDescription)\n\(error?.userInfo)",
preferredStyle: .Alert
)
let defaultAction = UIAlertAction(
title: "Ok",
style: .Default,
handler: nil)
alertController.addAction(defaultAction)
self.navigationController?.topViewController.presentViewController(alertController, animated: true, completion: nil)
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.