0

I am working on a project in which I perform lazy loading of images. When the imagedownloader downloads the images,it sends the message to its delegate to handle the image. But when its delegate,which is a view controller, gets deallocated from memory,I dont want imagedownloader class to send messages to its delegate as its already dead. I need to know when can i set delegate of imagedownloader to nil?? My target is set to iOS4.0 so i cant use weak references. And i have many instances of imagedownloader stored in a dictionary ready to sent their delegate the completion message . I have to set delegte of all those stored instances to nil.For now i am doing '

-(void)viewWillDisappear:(BOOL)animated
{
    for(imagedownloader *imagedownloaderObj in dict)
    {
        imagedownloaderObj.delegate = nil;
    }

    [super viewWillDisAppear:animated]
}

but it crashes in the loop. Please help anyone...and sorry about my bad english but i hope you got it whats my problem..

Marco Pace
  • 3,820
  • 19
  • 38
Vishal Singh
  • 4,400
  • 4
  • 27
  • 43

1 Answers1

1

You have a problem in your code - you are enumerating a dictionary which enumerates its keys, not its objects. Instead you want to do:

for(ImageDownloader *imageDownloader in [imageDownloaderDictionary allValues])
{
    if (imageDownloader.delegate == self)
        imageDownloader.delegate = nil;
} //note - I've adjusted naming to match Objective-C style conventions. It fits in better with the framework code now.

Also, I'd say to do this in dealloc instead. I don't know that you'll always get a viewWillDisappear: method before deallocating, on earlier version of iOS (including iOS4) you certainly couldn't guarantee that. And furthermore you don't want to waste time downloading the images again if you come back to that view.

Carl Veazey
  • 18,392
  • 8
  • 66
  • 81
  • I can not use dealloc I am using ARC . Is there any other place where I can set the delegate to nil? and thanks for you answer , I will try your code and tell you whether its working. – Vishal Singh Oct 05 '12 at 08:16
  • You can too override dealloc! This is exactly the kind of stuff you want to do in your dealloc method in ARC. Just don't call `super` in there. – Carl Veazey Oct 05 '12 at 08:17
  • Yes thanks alottt Carl you saved my day . Its working perfectly fine now...I was finally started to thinking to make the property of delegate strong but you saved me to go against memory management rules :):)..much thanks – Vishal Singh Oct 05 '12 at 08:23