sharadchauhan0504 / PrakrstaFileDownloader

2 stars 0 forks source link

didcomsplete with error issue #1

Closed mahadshahib closed 6 years ago

mahadshahib commented 6 years ago

wwhen i terminate app and relunch it again how should i call didcomplete cause i think it wont call on it self when i press start download again it ill start but it will start 2 ddownloads one from beginning one resumed

sharadchauhan0504 commented 6 years ago

It does. It will resume the download and once completed didComplete gets called. I suggest put print statement to get an idea what happens when you relaunch the app.

mahadshahib commented 6 years ago

i did actually and yet nothig show up in console unless i start download all over again this is my function

func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) { if error != nil { let err = error as NSError? let resumeData = err?.userInfo[NSURLSessionDownloadTaskResumeData] as? Data print("anotherone") let newtask = session.downloadTask(withResumeData: resumeData!) newtask.resume() } else { print("hichi") } }

mahadshahib commented 6 years ago

shouldn't i do something on app delegate didlucnchedwithoption function?

sharadchauhan0504 commented 6 years ago

You are doing it wrong in delegate. If you check the repo code you will notice that you resume the data in URLSessionDelegate and not in URLSessionTaskDelegate.

And there is commented code you need to put in AppDelegate.

In URLSessionDelegate you get callback when you relaunch the app and once download is completed you get completion call in URLSessionTaskDelegate.

mahadshahib commented 6 years ago

can describe whaat should i do now?

sharadchauhan0504 commented 6 years ago

Did you try using the class from repo ? I guess it's self explanatory. I suggest you to run the repo code and check if that's working for you or not.

And again please put resume code in URLSessionDelegate.

mahadshahib commented 6 years ago

I am trying to build my class from your code so yea it helps but I just don't get you when you say with code in urlsessiondelegate where is the urlsesssiondelegate i make my view controller inherit from urlsessiondelegate

mahadshahib commented 6 years ago

`import UIKit

class ViewController: UIViewController , URLSessionDelegate , URLSessionTaskDelegate {

var data  : Data? = nil
var task : URLSessionDownloadTask!
var session : URLSession!
let config = URLSessionConfiguration.background(withIdentifier: "\(Bundle.main.bundleIdentifier!).background")

@IBOutlet weak var label: UILabel!

override func viewDidLoad() {
    super.viewDidLoad()

}

@IBAction func start(_ sender: Any) {

    beingDownload()
}

@IBAction func stop(_ sender: Any) {
  task.suspend()

}
func beingDownload() {

    startfresh()

}
func startfresh()  {
     session = URLSession(configuration: config , delegate: self, delegateQueue: OperationQueue())
    let url = URL(string: "https://nava.ir/wp-content/uploads/2018/08/Gholamreza-Sanatgar-Dorooghe-Sefid-128.mp3")
     task = session.downloadTask(with: url!)
    task.resume()
}

func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) {

        let progress = Float(totalBytesWritten) / Float(totalBytesExpectedToWrite)
        debugPrint("Progress \(downloadTask) \(progress)")

}
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {
    debugPrint("Download finished: \(location)")
    try? FileManager.default.removeItem(at: location)
        }

func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
    if error != nil {
    let err = error as NSError?
        let resumeData = err?.userInfo[NSURLSessionDownloadTaskResumeData] as? Data
        print("anotherone")
        let newtask = session.downloadTask(withResumeData: resumeData!)
        newtask.resume()
    }
    else {
        print("hichi")
    }

}

}

` this is my viewcontroller

mahadshahib commented 6 years ago
                                                                                                                                                                    `class ViewController: UIViewController , URLSessionDelegate , URLSessionTaskDelegate , URLSessionDownloadDelegate {
var data  : Data? = nil
var task : URLSessionDownloadTask!
var session : URLSession!
let config = URLSessionConfiguration.background(withIdentifier: "\(Bundle.main.bundleIdentifier!).background")

@IBOutlet weak var label: UILabel!

override func viewDidLoad() {
    super.viewDidLoad()

    print("kbkjn")
}

@IBAction func start(_ sender: Any) {

    beingDownload()
}

@IBAction func stop(_ sender: Any) {
  task.suspend()

}
func beingDownload() {

    startfresh()

}
func startfresh()  {
     session = URLSession(configuration: config , delegate: self, delegateQueue: OperationQueue())
    let url = URL(string: "https://nava.ir/wp-content/uploads/2018/08/Gholamreza-Sanatgar-Dorooghe-Sefid-128.mp3")
     task = session.downloadTask(with: url!)
    task.resume()
}

func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) {

        let progress = Float(totalBytesWritten) / Float(totalBytesExpectedToWrite)
        debugPrint("Progress \(downloadTask) \(progress)")

}
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {
    debugPrint("Download finished: \(location)")
    try? FileManager.default.removeItem(at: location)
        }

func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
    if error != nil {
    let err = error as NSError?
        let resumeData = err?.userInfo[NSURLSessionDownloadTaskResumeData] as? Data
        print("anotherone")
        let newtask = session.downloadTask(withResumeData: resumeData!)
        newtask.resume()
    }
    else {
        print("hichi")
    }

}

}` i add 3 delegates and nothing can you edit my code

mahadshahib commented 6 years ago

i did but same my problem is that this didcomplete func wont run on idk viewdid load only runs when i hit start again and yea it resmes previos download but start a new one as well

sharadchauhan0504 commented 6 years ago

Yeah you should make a another singleton class and move code from controller. The problem I am guessing is in initializing the session. So better make another class to download your files and see the diff.

mahadshahib commented 6 years ago

howw caan i get download progress with your class?

sharadchauhan0504 commented 6 years ago

Please check usage I have written in Readme file. You need to assign callbacks in your view controller.

sharadchauhan0504 commented 6 years ago

@mahadshahib Did you try using my class ? If that is working you can close this issue.

sharadchauhan0504 commented 6 years ago

I am closing this issue, if problem exists try my repo as it is.