nsmanagedobjectcontext don't save certain url file

I am using a URL Protocol to save the cache of UIWebView in order to call it back when an internet connection is not available.

My app is also using a link "https://custom-link-to-no-add-to-cache.php" that is used by Reachability to check for connection.

What I am trying to avoid is for the

context.save(&error)
to not save or cache the URL used by Reachability. I have been trying several way to do that such as:


if (request.URL != "https://custom-link-to-no-add-to-cache.php") {...save code here... }


the saving function is:


    func saveCachedResponse () {
        println("Saving cached response")
     
        let delegate = UIApplication.sharedApplication().delegate as! AppDelegate
        let context = delegate.managedObjectContext!
     
        //
        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")
     
        // check if URL == "https://custom-link-to-no-add-to-cache.php"
        var error: NSError?
        let success = context.save(&error)
        if !success {
            println("Could not cache the response")
        }
     
    }


How can Avoid to cache the link used in Reachability?

First up, I moved your question to Core OS > Networking because it seems more about networking than about Swift.

Second, I'm having a hard time understanding your actual question. At first blush it seems like you're simply trying to test URLs for equality, that is, you're trying to see if

request.URL
matches some fixed URL. Is that right?

If so, "==" should work. Swift translates it to

-[NSURL isEqual:]
. For example:
import Foundation

let u1 = NSURL(string: "http://foo.example.com")!
let u2 = NSURL(string: "http://bar.example.com")!
let u3 = NSURL(string: "http://foo.example.com")!

println(u1 == u2)       // prints "false"
println(u1 == u3)       // prints "true"
println(u1 === u3)      // prints "false"

Note that

u1
and
u3
are not the same object, so "===" returns false:

[I tested this with Xcode 6.4 on OS X 10.10.4, although I expect it'd be the same for Swift 2.]

println(u1 === u3)      // prints "false"

IMPORTANT URL equality is more complex than you might think. Consider the following:

let uR = NSURL(string: "http://example.com")
let uR1 = NSURL(string: "index.html", relativeToURL: uR)!
let uR2 = NSURL(string: "http://example.com/index.html")!

println(uR1 == uR2)                             // prints "false"
println(uR1.absoluteURL! == uR2.absoluteURL!)   // prints "true"
uR1
and
uR2
are not equal because the
uR1
is relative and
uR2
is absolute. If you want them to compare as equal, you have to resolve them by calling
-absoluteURL
.

Share and Enjoy

Quinn "The Eskimo!"
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"
nsmanagedobjectcontext don't save certain url file
 
 
Q