Swift 2 - CoreData - NSManagedObjectContext Unable to invoke save using argument list of type (inout NSError?) "

I am trying to follow this tutorial to save the local UIWebView cache in my application.

I had to convert several lines to swift 2, but I can’t find a way to solve the problem if I add an argument of type NSError to NSManagedObjectContext.

from an image that you can understand better what I mean.

enter image description here

How can 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")
    }
}
+4
source share
4 answers

Surface error

The handle is print("Could not cache the response")not listed in other answers:

do {
    try context.save()
} catch let error {
    print("Could not cache the response \(error)")
}

Avoid DB corruption, use runtime block for all MOC access

, , :

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

Swift 2.0

, do { try ...} catch {...} .

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

- Xcode 7, fatal error.

+3
do {
    try managedObjectContext.save()
} catch let error as NSError
{
    NSLog("Unresolved error \(error), \(error.userInfo)")
}
+2
do {
    let success = try context.save()
} catch {

}

. Xcode 7.

+1

Source: https://habr.com/ru/post/1599747/


All Articles