swift - When should we clear Core Data cache entity? -
i using code in tutorial http://www.raywenderlich.com/76735/using-nsurlprotocol-swift
in tutorial code caching every single request.
my question when , how can clear entity of caching (core data)? or dont need clear caching entity?
caching entity entity save data requested nsconnection. if need clear cache in core data how can choose data stay , others deleted.
for example , making news app. every vc has label such title, date, category , uiwebview. , when app closed want delete caching links except "saved news". "saved news" category users choose tapping button "plus".
i thinking create new column named "istosave". so, when user taps button save. need parse content text , pull out links (src=) , in coredata set column "istosave "to true. when app closed delete links column "istosave" = false
so shortly: practice clear entity "cacheurls" , if yes how can clear urls selection ?
here code:
import uikit import coredata var requestcount = 0 class myurlprotocol: nsurlprotocol { var connection: nsurlconnection! var mutabledata: nsmutabledata! var response: nsurlresponse! override class func caninitwithrequest(request: nsurlrequest) -> bool { println("request #\(requestcount++): url = \(request.url.absolutestring)") if nsurlprotocol.propertyforkey("myurlprotocolhandledkey", inrequest: request) != nil { return false } return true } override class func canonicalrequestforrequest(request: nsurlrequest) -> nsurlrequest { return request } override class func requestiscacheequivalent(arequest: nsurlrequest, torequest brequest: nsurlrequest) -> bool { return super.requestiscacheequivalent(arequest, torequest:brequest) } override func startloading() { // 1 let possiblecachedresponse = self.cachedresponseforcurrentrequest() if let cachedresponse = possiblecachedresponse { println("serving response cache") // 2 let data = cachedresponse.valueforkey("data") nsdata! let mimetype = cachedresponse.valueforkey("mimetype") string! let encoding = cachedresponse.valueforkey("encoding") string! // 3 let response = nsurlresponse(url: self.request.url, mimetype: mimetype, expectedcontentlength: data.length, textencodingname: encoding) // 4 self.client!.urlprotocol(self, didreceiveresponse: response, cachestoragepolicy: .notallowed) self.client!.urlprotocol(self, didloaddata: data) self.client!.urlprotocoldidfinishloading(self) } else { // 5 println("serving response nsurlconnection") var newrequest = self.request.mutablecopy() nsmutableurlrequest nsurlprotocol.setproperty(true, forkey: "myurlprotocolhandledkey", inrequest: newrequest) self.connection = nsurlconnection(request: newrequest, delegate: self) } } override func stoploading() { if self.connection != nil { self.connection.cancel() } self.connection = nil } func connection(connection: nsurlconnection!, didreceiveresponse response: nsurlresponse!) { self.client!.urlprotocol(self, didreceiveresponse: response, cachestoragepolicy: .notallowed) self.response = response self.mutabledata = nsmutabledata() } func connection(connection: nsurlconnection!, didreceivedata data: nsdata!) { self.client!.urlprotocol(self, didloaddata: data) self.mutabledata.appenddata(data) } func connectiondidfinishloading(connection: nsurlconnection!) { self.client!.urlprotocoldidfinishloading(self) self.savecachedresponse() } func connection(connection: nsurlconnection!, didfailwitherror error: nserror!) { self.client!.urlprotocol(self, didfailwitherror: error) } func savecachedresponse () { println("saving cached response") // 1 let delegate = uiapplication.sharedapplication().delegate appdelegate let context = delegate.managedobjectcontext! // 2 let cachedresponse = nsentitydescription.insertnewobjectforentityforname("cachedurlresponse", inmanagedobjectcontext: context) 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 { println("could not cache response") } } func cachedresponseforcurrentrequest() -> nsmanagedobject? { // 1 let delegate = uiapplication.sharedapplication().delegate appdelegate let context = delegate.managedobjectcontext! // 2 let fetchrequest = nsfetchrequest() let entity = nsentitydescription.entityforname("cachedurlresponse", inmanagedobjectcontext: context) fetchrequest.entity = entity // 3 let predicate = nspredicate(format:"url == %@", self.request.url.absolutestring!) fetchrequest.predicate = predicate // 4 var error: nserror? let possibleresult = context.executefetchrequest(fetchrequest, error: &error) array<nsmanagedobject>? // 5 if let result = possibleresult { if !result.isempty { return result[0] } } return nil } }
Comments
Post a Comment