我正在创建一个任务,以在函数内部的后台下载图像,并且在同一上下文中有一个notification
对象。我想在调用委托notification
时检索此didFinishDownloadingToURL
对象。有什么办法可以做到吗?
我创建一个像这样的任务:
NSURLSessionConfiguration *sessionConfig = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:@"imageDownloadSession"];
[sessionConfig setDiscretionary:YES];
[sessionConfig setSessionSendsLaunchEvents:YES];
NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfig delegate:self delegateQueue:nil];
NSURLSessionDownloadTask *getImageTask = [session downloadTaskWithURL:notification.imageRemoteURL];
这是我的代表:
-(void)URLSession:(NSURLSession *)session
downloadTask:(NSURLSessionDownloadTask *)downloadTask
didFinishDownloadingToURL:(NSURL *)location {
// I want to access the notification object here
}
答案 0 :(得分:0)
您可以简单地在通知中的NSURLSessionDownloadDelegate类中的一个属性中进行存储。因此,当调用downloadTaskWithURL时,您可以访问该属性。 或者,您可以创建另一个类,该类将通知保存为实例属性并充当委托。如果您有多个下载任务,则可以将它们放在一个数组中。
例如:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="https://unpkg.com/flickity@2/dist/flickity.min.css">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="https://unpkg.com/flickity@2/dist/flickity.pkgd.min.js"></script>
</head>
<body>
<!-- Flickity HTML init -->
<div class="gallery js-flickity">
<div class="gallery-cell">
<img src="https://seeklogo.com/images/H/huawei-logo-B8D40C4904-seeklogo.com.png" />
</div>
<div class="gallery-cell">
<img src="https://seeklogo.com/images/T/tsm-logo-8DCBE4D37B-seeklogo.com.png" />
</div>
<div class="gallery-cell">
<img src="https://seeklogo.com/images/H/huawei-logo-B8D40C4904-seeklogo.com.png" />
</div>
<div class="gallery-cell">
<img src="https://seeklogo.com/images/T/tsm-logo-8DCBE4D37B-seeklogo.com.png" />
</div>
<div class="gallery-cell">
<img src="https://seeklogo.com/images/H/huawei-logo-B8D40C4904-seeklogo.com.png" />
</div>
</div>
</body>
</html>