0

I have a view controller with this method:

- (void)getImages
{
   if (self.myEntities != nil) {
      if (self.myEntities.count > 0) {
         if (self.imagesDownloader == nil) {
            self.imagesDownloader = [[ImagesDownloader alloc] initWithListener];
         }
      }

      for (MyEntity *myEntity in self.myEntities) {
         if (![myEntity.imageUrl isEqualToString:@""] && (myEntity.imageUrl != nil)) {
            [self.imagesDownloader getImageFromServiceUrl:myEntity.imageUrl];
         }
      }
   }
}

And ImagesDownloader is an NSObject subclass like this:

@implementation ImagesDownloader

- (id)initWithListener
{
   self = [super init];
   if (self) {
      [self registerNotifications];
   }
   return self;
}

- (void)registerNotifications
{
   [[NSNotificationCenter defaultCenter] removeObserver:self
                                                   name:@"getImageDidFinish"
                                                 object:nil];
   [[NSNotificationCenter defaultCenter] addObserver:self
                                            selector:@selector(getImageDidFinish:)
                                                name:@"getImageDidFinish"
                                              object:nil];
}

- (void)getImageFromServiceUrl:(NSString *)imageUrl
{
   GetImage *getImage = [[GetImage alloc] init];
   [getImage queryServiceWithImageUrl:imageUrl];
}

// More instance methods

@end

In turn, GetImage is another NSObject subclass that calls a RESTful web service by using an NSURLConnection object, and then, in its connectionDidFinishLoading: delegate method, it posts the notification the imagesDownloader object is observing via Notification Center:

[[NSNotificationCenter defaultCenter] postNotificationName:@"getImageDidFinish"
                                                    object:nil
                                                  userInfo:serviceInfoDict];

This is the call where I sometimes get the EXC_BAD_ACCESS error.

The scenario is like this: the view controller is loaded (it is pushed into a navigation stack) and getImages is called. Right now, its self.myEntities has 3 objects, so self.imagesDownloader is initialized and call 3 times to getImageFromServiceUrl. The service is called 3 times as well, and in connectionDidFinishLoading:, the notification is posted the 3 times with no error.

But I then go back and forth the view controller, and it is loaded again and same calls made. But this time, I get the error the 2nd time the notification is going to be posted from connectionDidFinishLoading:. I don't undersatnd why, what could I be missing?

Thanks in advance

AppsDev
  • 12,319
  • 23
  • 93
  • 186
  • What are these `self.myEntities` ? Core Data entities ? – Randy Nov 11 '15 at 13:02
  • 1
    Does `ImagesDownloader` have a `dealloc` that removes it as an observer? If not, that seems a likely source of trouble. – Phillip Mills Nov 11 '15 at 13:06
  • @Randy Yes, they are managed objects – AppsDev Nov 11 '15 at 13:32
  • I've had some issues using `NSNotificationCenter`and CoreData. Can you try that : `for (MyEntity *myEntity in self.myEntities) { if (![myEntity.imageUrl isEqualToString:@""] && (myEntity.imageUrl != nil)) { NSString *imageUrl = myEntity.imageUrl [self.imagesDownloader getImageFromServiceUrl:imageUrl]; } }` – Randy Nov 11 '15 at 13:39
  • @PhillipMills No, it hadn't. I've just added it and now it seems to work... thanks! – AppsDev Nov 11 '15 at 13:55

1 Answers1

0

As @PhillipMills said, adding this to ImagesDownloader seems to solve the problem:

- (void)dealloc
{
   [[NSNotificationCenter defaultCenter] removeObserver:self];
}
AppsDev
  • 12,319
  • 23
  • 93
  • 186