Search code examples
iosiphoneinheritancedelegatesdelegation

Delegate not being set correctly


Im working on a project and my UIWebview class needs to execute a method from my DownloadView class

I am using the open source project https://github.com/robertmryan/download-manager

When this code executes the method:

 DownloadTableView *download = [[DownloadTableView alloc] init];
 [download queueAndStartDownloads:_downloadURL];

This line doesnt set the delegate right

  self.downloadManager = [[DownloadManager alloc] initWithDelegate:self];

The whole start download method

- (void)queueAndStartDownloads:(NSURL *)url
{


NSString *documentsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
NSString *downloadFolder = [documentsPath stringByAppendingPathComponent:@"downloads"];

if ([[NSFileManager defaultManager] fileExistsAtPath:downloadFolder])       //Does file exist?
{
    if (![[NSFileManager defaultManager] createDirectoryAtPath:downloadFolder
                                   withIntermediateDirectories:NO
                                                    attributes:nil
                                                         error:nil]) {

    }
}

self.downloadManager = [[DownloadManager alloc] initWithDelegate:self];
self.downloadManager.maxConcurrentDownloads = 4;


    NSString *downloadFilename = [downloadFolder stringByAppendingPathComponent:[url lastPathComponent]];
    [self.downloadManager addDownloadWithFilename:downloadFilename URL:url];


self.cancelButton.enabled = YES;
self.startDate = [NSDate date];
NSLog(@"DOwnling");
[self.downloadManager start];

}

The methods in my DownloadView class wont execute

 - (void)didFinishLoadingAllForManager:(DownloadManager *)downloadManager

{


Solution

  • Assuming that your code is under ARC, from the code I understand thatDownloadTableView *download is a local variable. Hence the DownloadTableView object gets released after the scope of the method where it is declared ends. Hence the delegate method doesn't get called, because the delegate is released. To avoid this, you can create DownloadTableView object as an instance variable.