Question

I'm creating my own protocols and delegate using AFNetworking 2.0.

I have some methods to show the download progress (works great), when the download is finished and when the download will begin.

The problem is: I don't know how to know in another class when my download is finished.

Someone has an idea ? Suggestions ?

Thank you so much!

Here my .h file:

#import <Foundation/Foundation.h>

@class MapDownloader;

@protocol MapDownloaderDelegate <NSObject>

@optional

- (BOOL)mapDownloaderWillMapDownload:(MapDownloader *)downloader;
- (BOOL)mapDownloaderDidMapDownload:(MapDownloader *)downloader;

- (void)mapDownloader:(MapDownloader *)downloader progressDownloading:(float)progress;

@end

@interface MapDownloader : NSObject

@property (nonatomic, weak) id<MapDownloaderDelegate> delegate;
@property (nonatomic, strong) NSString *mapName;

- (void)downloadAsync:(NSString*)mapName;

@end

And here is my .m file:

@implementation MapDownloader

- (void)downloadAsync:(NSString *)mapName
{
    if (![self willDownloadMap])
        return;

    self.mapName = mapName;

    [self startDownload];
}

#pragma mark -
#pragma mark Private Methods

- (void)startDownload
{

    NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
    AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];

    NSURL * URL = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@", kUrlWalk, self.mapName]];
    NSURLRequest *request = [NSURLRequest requestWithURL:URL];
    NSProgress *progress;

    NSURLSessionDownloadTask *downloadTask =
    [manager downloadTaskWithRequest:request
                            progress:&progress
                         destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {
                             return [self filePath];
                         }
                   completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
                       [self downloadComplete];
                   }];

    [downloadTask resume];

    [progress addObserver:self
               forKeyPath:@"fractionCompleted"
                  options:NSKeyValueObservingOptionNew
                  context:nil];

}

- (void)downloadProgress:(double)progress
{
    [self progressDownloading:progress];
}

- (void)downloadComplete
{
    [self didDownloadMap];
}

#pragma mark Helpers

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    NSProgress *progress = (NSProgress *)object;
    [self downloadProgress:progress.fractionCompleted];
}

- (NSURL*)documentPath
{
    return [NSURL fileURLWithPath:[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]];
}

- (NSURL*)mapsPath
{
    NSURL *documentsDirectoryPath = [self documentPath];

    return [documentsDirectoryPath URLByAppendingPathComponent:kDirMap];
}

- (NSURL*)filePath
{
    NSURL *mapsPath = [self mapsPath];

    return [mapsPath URLByAppendingPathComponent:[NSString stringWithFormat:@"%@", self.mapName]];
}

#pragma mark - Events

- (BOOL)willDownloadMap
{
    if ([self.delegate respondsToSelector:@selector(mapDownloaderWillMapDownload:)])
        return [self.delegate mapDownloaderWillMapDownload:self];

    return YES;
}

- (void)didDownloadMap {

    if ([self.delegate respondsToSelector:@selector(mapDownloaderDidMapDownload:)])
        [self.delegate mapDownloaderDidMapDownload:self];
}

- (void)progressDownloading:(float)progress {

    if ([self.delegate respondsToSelector:@selector(mapDownloader:progressDownloading:)])
        [self.delegate mapDownloader:self progressDownloading:progress];
}

@end
Was it helpful?

Solution

Please refer to NSNOtification Class . Please call this in the ViewDidLoad method of the class where you want to know whether download is finished

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(finished:) name:@"requestfinishes" object:nil];

and call this is the (void)didDownloadMap method

[[NSNotificationCenter defaultCenter] postNotificationName:@"requestfinishes" object:nil userInfo:nil];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top