Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift 2 - CoreData - NSManagedObjectContext Cannot invoke 'save' with an argument list of type '(inout NSError?)'

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.

enter image description here

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")
    }
}
like image 924
SNos Avatar asked Dec 25 '22 16:12

SNos


2 Answers

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)")
        }
    })
}
like image 112
SwiftArchitect Avatar answered Feb 09 '23 00:02

SwiftArchitect


In Swift 2.0

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)")
    }

Edit

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.

like image 33
Antonio Avatar answered Feb 08 '23 23:02

Antonio