I am trying to follow this tutorial in order to save the Local Cache of UIWebView in my app.
I had to convert a few lines to swift 2 but i cannot find the way to solve the issue when add to NSManagedObjectContext save an argument of type NSError.
from the picture you can understand better what I mean.

How could I fix this? The code I'm using is:
func saveCachedResponse () {
    print("Saving cached response")
    // 1
    let delegate = UIApplication.sharedApplication().delegate as! AppDelegate
    let context = delegate.managedObjectContext!
    // 2
    let cachedResponse = NSEntityDescription.insertNewObjectForEntityForName("CachedURLResponse", inManagedObjectContext: context) as NSManagedObject
    cachedResponse.setValue(self.mutableData, forKey: "data")
    cachedResponse.setValue(self.request.URL!.absoluteString, forKey: "url")
    cachedResponse.setValue(NSDate(), forKey: "timestamp")
    cachedResponse.setValue(self.response.MIMEType, forKey: "mimeType")
    cachedResponse.setValue(self.response.textEncodingName, forKey: "encoding")
    // 3
    var error: NSError?
    let success = context.save(&error)
    if !success {
        print("Could not cache the response")
    }
}
                Surface Error
Handle print("Could not cache the response") not listed in the other answers:
do {
    try context.save()
} catch let error {
    print("Could not cache the response \(error)")
}
Avoid DB corruption, use perform block for all MOC access
As I like code examples to be somewhat robust, here is a more exhaustive solution:
func saveCachedResponse (context: NSManagedObjectContext) {
    context.performBlockAndWait({ () -> Void in
        let cachedResponse = NSEntityDescription.insertNewObjectForEntityForName("CachedURLResponse", inManagedObjectContext: context) as NSManagedObject
        cachedResponse.setValue(self.mutableData, forKey: "data")
        // etc.
        do {
            try context.save()
        } catch let error {
            print("Could not cache the response \(error)")
        }
    })
}
                        To save context, you have to use do { try ...} and catch {...} your error. 
// Save the context.
    do {
        try context.save()
    } catch {
        // Replace this implementation with code to handle the error appropriately.
        // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
        //print("Unresolved error \(error), \(error.userInfo)")
    }
let entity = NSEntityDescription.entityForName("CachedURLResponse", inManagedObjectContext: context)
let newCacheUrl = NSEntityDescription.insertNewObjectForEntityForName(entity.name!, inManagedObjectContext: context)
newCacheUrl.setValue(NSDate(), forKey: "timeStamp")
I use something like this with Xcode 7 and I have not fatal error.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With