ios - Cancel NSOperationsQueue -
i trying cancel nsoperationsqueue , remove operations queue. called method "cancelalloperations()" doesnt remove operation queue, aware method adds flag operation.
how go removing operations queue? or prevent operation queue executing operations have flag?
private let mediadownloadoperationqueue: nsoperationqueue = { let queue = nsoperationqueue() queue.maxconcurrentoperationcount = 1 queue.qualityofservice = .background return queue }() func startdownloadprocess() { guard downloadswitchstate == true else { return } let context = databasemanager.sharedinstance.mainmanagedobjectcontext let mediastodownload = self.listofmediatodownload(context) media in mediastodownload { downloadmedia(media) } } private func downloadmedia(media: media) { //check if operation exist operation in mediadownloadoperationqueue.operations { let operation = operation as! mediadownloadoperation if operation.media.objectid == media.objectid { return } } //here adding operation queue mediadownloadoperationqueue.addoperation(mediadownloadoperation(media: media)) }
//edit: here mediasdownloadoperation
import foundation import alamofire class mediadownloadoperation: baseasyncoperation { let media: media init(media: media) { self.media = media super.init() } override func main() { guard let mediasourceuri = media.sourceuri else { self.completeoperation() return } var filepath: nsurl? let destination: (nsurl, nshttpurlresponse) -> (nsurl) = { (temporaryurl, response) in if let directoryurl = nsfilemanager.defaultmanager().urlsfordirectory(.documentdirectory, indomains: .userdomainmask).first, let suggestedfilename = response.suggestedfilename { filepath = directoryurl.urlbyappendingpathcomponent("\(suggestedfilename)") return filepath! } return temporaryurl } requestmanager.sharedalamofiremanager.download(.get, mediasourceuri, destination: destination).response { (request, response, data, error) -> void in if let filepath = filepath error == nil { let savetocameraroll = self.media.savetocameraroll?.boolvalue == true self.media.filelocation = filepath.lastpathcomponent self.media.savetocameraroll = false databasemanager.sharedinstance.save() if savetocameraroll { self.media.savemediatocameraroll(nil) } } self.completeoperation() nsnotificationcenter.defaultcenter().postnotificationname(mediadownloadmanager.mediaisdownloadednote, object: nil) } } }
adding conditional test see if operation has cancelled seemed have solved problem:
requestmanager.sharedalamofiremanager.download(.get, mediasourceuri, destination: destination).response { (request, response, data, error) -> void in //only download if logged in if (self.cancelled == false){ print("ran operation!") if let filepath = filepath error == nil { let savetocameraroll = self.media.savetocameraroll?.boolvalue == true self.media.filelocation = filepath.lastpathcomponent self.media.savetocameraroll = false databasemanager.sharedinstance.save() if savetocameraroll { self.media.savemediatocameraroll(nil) } } self.completeoperation() nsnotificationcenter.defaultcenter().postnotificationname(mediadownloadmanager.mediaisdownloadednote, object: nil) }else{ print("did not run operation! cancelled!") self.completeoperation() } }
Comments
Post a Comment