ios - swift: URLSessionConfiguration -
i have firstviewcontroller
button move in secondtableviewcontroller
. in secondtableviewcontroller
have cell , if click on cell downloading starts.
problem: if move in secondtableviewcontroller
firstviewcontroller
start downloading , return in firstviewcontroller
, after move in secondtableviewcontroller
this:
a background urlsession identifier com.example.downloadtaskexample.background exists!
and can not download files. how fix it?
my code in secondtableviewcontroller
:
var backgroundsession: urlsession! var index = 0 override func viewdidload() { super.viewdidload() let sessionconfig = urlsessionconfiguration.background(withidentifier: "com.example.downloadtaskexample.background") backgroundsession = urlsession(configuration: sessionconfig, delegate: self, delegatequeue: operationqueue()) }
code download files:
let url = url(string: "link")! let downloadtasklocal = viewcontroller.backgroundsession.downloadtask(with: url) downloadtasklocal.resume()
new code:
class networking { static let shared = networking() var backgroundsession = urlsession(configuration: urlsessionconfiguration.background(withidentifier: "com.example.downloadtaskexample.background"), delegate: urlsession() as? urlsessiondelegate, delegatequeue: operationqueue()) } class viewcontroller: uitableviewcontroller, urlsessiondownloaddelegate { override func tableview(_ tableview: uitableview, didselectrowat indexpath: indexpath) { let url = url(string: "link")! let downloadtasklocal = networking.shared.backgroundsession.downloadtask(with: url) downloadtasklocal.resume() } }
upd
class backgroundsession: nsobject { static let shared = backgroundsession() static let identifier = "com.example.downloadtaskexample.background" var session: urlsession! private override init() { super.init() let configuration = urlsessionconfiguration.background(withidentifier: backgroundsession.identifier) session = urlsession(configuration: configuration, delegate: self as? urlsessiondelegate, delegatequeue: operationqueue()) } } class viewcontroller: uitableviewcontroller, urlsessiondownloaddelegate{ override func tableview(_ tableview: uitableview, didselectrowat indexpath: indexpath) { let url = url(string: "http:link\(indexpath.row + 1)")! let downloadtasklocal = backgroundsession.shared.session.downloadtask(with: url) downloadtasklocal.resume() } }
if you're using background session, should make sure instantiate once. need make single urlsession
instance available not multiple instances of second view controller, need have app delegate able reference (e.g. handleeventsforbackgroundurlsession
has save completion handler, called session delegate's urlsessiondidfinishevents(forbackgroundurlsession:)
).
one approach have app delegate instantiate , pass along. easier, can use singleton pattern (as shown in https://stackoverflow.com/a/44140059/1271826).
the trick in decoupling background session particular instance of second view controller, how want inform second view controller of events background session. might want use notificationcenter
. or give background session closure properties second view controller set (and reset every new instance). it's hard precisely without knowing second view controller doing.
but key make sure have 1 instance of background session during lifetime of app.
Comments
Post a Comment