简体   繁体   English

在Swift中下载NSURLSession期间未调用URLSession委托

[英]URLSession delegate not called during NSURLSession download in Swift

I'm trying to use a delegate method to update a progress bar as a file is downloaded using NSURLSession , however I can't seem to get the Delegate method to call. 我正在尝试使用委托方法来更新进度条,因为使用NSURLSession下载文件时,但是似乎无法调用Delegate方法。

The delegate method I have is as follows in Swift (doesn't get called when file download is initiated): 在Swift中,我拥有的委托方法如下(启动文件下载时不会调用):

func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64!, totalBytesExpectedToWrite: Int64){

    println("delegate called")
    if (totalBytesExpectedToWrite == NSURLSessionTransferSizeUnknown) {
        println("Unknown transfer size")
    } else {
        let index: Int = self.getFileDownloadInfoIndexWithTaskIdentifier(downloadTask.taskIdentifier)
        let fdi: FileDownloadInfo = self.arrFileDownloadData.objectAtIndex(index) as FileDownloadInfo
        NSOperationQueue().addOperationWithBlock({

            //Calculate the progress
            fdi.downloadProgress = Double(totalBytesWritten) / Double(totalBytesExpectedToWrite)

            // Update the progressview bar
            self.progressView.progress = Float(fdi.downloadProgress)

        })

    }
}

The equivalent Objective-C call I'm trying to replicate in Swift above is (which DOES get called when file download is initiated): 我试图在上面的Swift中复制的等效Objective-C调用是(启动文件下载时会调用该调用):

-(void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite{

    NSLog(@"Delegate called");
    if (totalBytesExpectedToWrite == NSURLSessionTransferSizeUnknown) {
        NSLog(@"Unknown transfer size");
    }
    else{
        // Locate the FileDownloadInfo object among all based on the taskIdentifier property of the task.
        int index = [self getFileDownloadInfoIndexWithTaskIdentifier:downloadTask.taskIdentifier];
        FileDownloadInfo *fdi = [self.arrFileDownloadData objectAtIndex:index];

    [.............]
        }];
    }
}

I'm sensing that I am doing something wrong as although, in both cases, I don't get the autofill filling in with the variables I need access to (eg didWriteData , bytesWritten etc), in ObjectiveC, after I type -(void)URLSession:(NSURLSession *)session , I get downloadTask and didWriteData etc as an option. 我感觉到我做错了,尽管在这两种情况下,我在键入-(void)URLSession:(NSURLSession *)session )后都没有在ObjectiveC中自动填充需要访问的变量(例如didWriteDatabytesWritten等) -(void)URLSession:(NSURLSession *)session ,我可以选择downloadTaskdidWriteData等。 However, with Swift I don't get these so I presume I'm doing something wrong. 但是,使用Swift我没有得到这些,所以我想我做错了。

Thanks in advance for any help. 在此先感谢您的帮助。

I found it - the parameters were incorrect (totalBytesWritten: Int64! shouldn't have an ! at the end). 我发现了-参数不正确(totalBytesWritten:Int64!末尾不应带有!)。 The correct code is below: 正确的代码如下:

func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64){

.....
}

Also, it's advisable to assign NSURLSessionDownloadDelegate to the containing class. 另外,建议将NSURLSessionDownloadDelegate分配给包含的类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM