I stuck from last 2 days on this issue. please help me out.
My requirement is, if I downloading a file in background in second view controller and after download starting I pop to 1st view controller and If I again push to 2nd view controller then that progress should be continue.
Here what happening :
- I push to second view controller.
- I started Download using
NSMutableURLRequest
,NSURLConnection
. - If I pop to 1st view controller and again go to 2nd view controller then all are showing 0. Means If progress is 34% during pop and again push time its 0% showing in my UI, but download is continuing as usual.
My log also showing perfect but UI its not showing. I tried all SO answers. All are in same problem.
This is my whole code :
In App Delegate Method :
CTAppDelegate.h
@property (copy) void (^backgroundSessionCompletionHandler)();
CTAppDelegate.m
- (void)application:(UIApplication *)application handleEventsForBackgroundURLSession:(NSString *)identifier completionHandler:(void (^)())completionHandler
{
self.backgroundSessionCompletionHandler = completionHandler;
}
2ndViewController.h
- (IBAction)downloadBackground:(id)sender;
- (IBAction)downloadImage:(id)sender;
@property (strong, nonatomic) IBOutlet UIImageView *downloadedImage;
@property (strong, nonatomic) IBOutlet UIProgressView *progressView;
@end
2ndViewController.m
#import "CTViewController.h"
#import "CTSessionOperation.h"
#import "CTAppDelegate.h"
static NSString *downloadUrl = @"http://www.nasa.gov/sites/default/files/ladee_9.4.13_nasa_edge_0.jpg";
@interface CTViewController ()
@property (nonatomic, strong) NSOperation *downloadOperation;
@end
@implementation CTViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.progressView.progress = 0;
self.downloadedImage.hidden = NO;
self.progressView.hidden = YES;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)downloadBackground:(id)sender {
// [self downloadImageInBackground:YES];
[self.navigationController popViewControllerAnimated:YES];
}
- (IBAction)downloadImage:(id)sender {
[self downloadImageInBackground:NO];
}
- (void)downloadImageInBackground:(BOOL)background{
if (self.downloadOperation){
return;
}
CTSessionOperation *operation = [CTSessionOperation new];
operation.downloadUrl = downloadUrl;
operation.progressAction = ^(double bytesWritten, double bytesExpected){
double progress = bytesWritten / bytesExpected;
dispatch_async(dispatch_get_main_queue(), ^{
self.progressView.progress = (float) progress;
});
};
operation.completionAction = ^(NSURL *imageUrl, BOOL success){
dispatch_async(dispatch_get_main_queue(), ^{
if (success){
UIImage *image = [UIImage imageWithContentsOfFile:[imageUrl path]];
self.downloadedImage.image = image;
NSLog(@"Done.......");
}
self.downloadedImage.hidden = NO;
self.progressView.progress = 0;
self.progressView.hidden = YES;
self.downloadOperation = nil;
});
};
operation.isBackground = background;
[operation enqueueOperation];
self.downloadedImage.hidden = YES;
self.progressView.hidden = NO;
self.downloadOperation = operation;
}
@end
Operation.h
#import <Foundation/Foundation.h>
typedef void (^CTProgressBlock)(double totalBytesWritten, double bytesExpected);
typedef void (^CTCompletionBlock)(NSURL *imageUrl, BOOL success);
@interface CTSessionOperation : NSOperation<NSURLSessionDelegate, NSURLSessionTaskDelegate, NSURLSessionDownloadDelegate>
@property (nonatomic) NSURLSessionDownloadTask *downloadTask;
@property (nonatomic) NSURLSession *session;
@property (nonatomic, strong) NSString *downloadUrl;
@property (strong) CTProgressBlock progressAction;
@property (strong) CTCompletionBlock completionAction;
@property (nonatomic, assign) BOOL isBackground;
- (void)enqueueOperation;
@end
Operation.m
#import "CTSessionOperation.h"
#import "CTAppDelegate.h"
@implementation CTSessionOperation
- (NSOperationQueue *)operationQueue{
static NSOperationQueue *operationQueue = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
operationQueue = [NSOperationQueue new];
[operationQueue setMaxConcurrentOperationCount:NSOperationQueueDefaultMaxConcurrentOperationCount];
});
return operationQueue;
}
- (NSURLSession *)session {
static NSURLSession *session = nil;
static NSURLSession *backgroundSession = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
NSURLSessionConfiguration *backgroundConfiguration = [NSURLSessionConfiguration
backgroundSessionConfiguration:@"com.captech.NSURLSample.BackgroundSession"];
backgroundSession = [NSURLSession sessionWithConfiguration:backgroundConfiguration
delegate:self
delegateQueue:nil];
});
return self.isBackground ? backgroundSession : session;
}
- (void)enqueueOperation{
[[self operationQueue] addOperation:self];
}
#pragma mark - NSOperation
- (void)start {
if (!self.isCancelled){
NSURL *downloadURL = [NSURL URLWithString:self.downloadUrl];
NSURLRequest *request = [NSURLRequest requestWithURL:downloadURL];
self.downloadTask = [self.session downloadTaskWithRequest:request];
[self.downloadTask resume];
}
}
#pragma mark - NSURLSessionDownloadDelegate
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location {
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *urls = [fileManager URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask];
NSURL *documentsDirectory = [urls objectAtIndex:0];
NSURL *originalUrl = [[downloadTask originalRequest] URL];
NSURL *destinationUrl = [documentsDirectory URLByAppendingPathComponent:[originalUrl lastPathComponent]];
NSError *error;
[fileManager removeItemAtURL:destinationUrl error:NULL];
BOOL success = [fileManager copyItemAtURL:location toURL:destinationUrl error:&error];
if (self.completionAction){
self.completionAction(destinationUrl, success);
}
}
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite {
if (downloadTask == self.downloadTask && self.progressAction){
self.progressAction((double)totalBytesWritten, (double)totalBytesExpectedToWrite);
}
}
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didResumeAtOffset:(int64_t)fileOffset expectedTotalBytes:(int64_t)expectedTotalBytes {
}
#pragma mark - NSURLSessionTaskDelegate
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error {
if (self.progressAction){
self.progressAction((double)task.countOfBytesReceived, (double)task.countOfBytesExpectedToReceive);
}
self.downloadTask = nil;
}
#pragma mark - NSURLSessionDelegate
- (void)URLSessionDidFinishEventsForBackgroundURLSession:(NSURLSession *)session {
CTAppDelegate *appDelegate = (CTAppDelegate *)[[UIApplication sharedApplication] delegate];
if (appDelegate.backgroundSessionCompletionHandler) {
void (^completionHandler)() = appDelegate.backgroundSessionCompletionHandler;
appDelegate.backgroundSessionCompletionHandler = nil;
completionHandler();
}
}
@end
This is my full code. Please check with this ... :)
I would have dealt with download a little different from how you are doing it :)
Here is how I would handle it :)
1.Declare a NSOperation class and move the code to download the file to this class
2.Declare a protocol in your NSOpertaion class which will inform the current download status to whoever confirms to this protocol
3.In your second VC start the NSOpertaion and opt for the delegate :) and update your progress accordingly
Here is a little bit of code for the same :)
downloadAssets.h
downloadAssets.m
AppDelegate.h
SecondVC.h
SecondVC.m
Thats it :)
start the download operation whenever you want :)
You wanna stop it say
Have morethan one download operation ?? Consider using NSOperationQueue or Array to hold NSOperations in appdelegate :)
EDIT
As per your request in comment, you can hold the reference to CTOperation in your appDelegate.
You dont need to declare a protocol as you have already declared completion block and progressActionBlock :)
All you have to do is to, check in your SecondVC viewWillAppear.
and in your downloadImageInBackground method rather than
self.downloadOperation = operation;
say `appdelegate.downloadingOperation = operation;Disclaimer
Please note code provided in this answer is only for the purpose of explaining concept. May I have syntactic or symmentic errors. Please refrain from blindly copy pasting.
Simple right ??? Have doubts, ping me :) Happy coding :)