1

I am loading an image through a NSURLConnection. Everything works fine, expect in EDGE mode. When I NSLog(); my connection (received data), the old image is still loading (step by step until connectionDidFinishLoading), even after [connection cancel];.

What can I do? In that case the old image still shows up before the new one comes in.

EDIT

The connection is called from class A:

ProductImageLoader *imageLoader = [[ProductImageLoader alloc] initWithString:productID delegate:self];

[self.queue_3LF addOperation:imageLoader];

And here it's going to be initialized in class B:

- (void)start_XL_ImageDownload
{
    [self.authConnection_N3I cancel];
    self.authConnection_N3I = nil;

    if (self.authConnection_N3I == nil)
    {
        NSMutableURLRequest *request_N3I = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://www.softridge.ch/images/articles/%@_1_xl.jpg", self.product_id]] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:18.0];

        [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];

        self.authConnection_N3I = [[NSURLConnection alloc] initWithRequest:request_N3I delegate:self];
    }
}

This is the abortion in class B:

- (void)abortRunningProcess
{
    [self.authConnection_N3I cancel];
    self.authConnection_N3I = nil;
}

Called from within class A:

ProductDataParser *PDP = [[ProductDataParser alloc] init];
[PDP abortRunningProcess];
Arun
  • 3,406
  • 4
  • 30
  • 55
filou
  • 1,609
  • 5
  • 26
  • 53
  • Your question is not very clear. First, are you saying you have an old image already on the device, and you are downloading a new image to take its place, but you do not want to display any image until the new one has finished downloading? Second, are you sure you are calling cancel on the correct connection? After it is called, your delegate should not receive any more notifications, including the `connectionDidFinishLoading` callback. – Mathew Apr 04 '13 at 21:02
  • Excuse me for any misunderstandings. Yes, exactly. The old one should stop loading, when I cancel the conntection (and yes, it is the right one). It seems to start loading again as soon as it is cancelled. The problem is, when connectionDidFinishLoading gets called, the "old" image will show up in the UIImageView. – filou Apr 04 '13 at 21:06
  • Put a break point in your code at every point where a new "image loading" connection may get created. Take a look at the stack each time the break point is reached to make sure you are not recreating a connection to load the old image at some point after the cancel call is made. I cannot think of a way in which the connection would be restarting itself (the documentation says after cancel is called, the connection will never be usable again), so it is probably a matter of figuring out why your code leads to a new connection being created. – Mathew Apr 04 '13 at 21:19
  • The other way you could confirm or deny my above suspicion is by `NSLogging` the connection variable each time `connectionDidReceiveData` is called and also logging when cancel is called and on which connection. Logging the connection will output its memory location to the console. If you do not see the memory location appear again after cancel is called, then you are indeed creating a new connection, as opposed to the same connection starting to load again. – Mathew Apr 04 '13 at 21:22
  • ok, I just NSLogged the connection. It's the same until connectionDidFinishLoading is called (does not seem to cancel). – filou Apr 04 '13 at 21:30
  • I think you're going to have to show some code — at least `NSURLConnection` creation and cancellation. Are you trying to do anything with threads, operation or dispatch queues? – Tommy Apr 04 '13 at 22:12
  • So I assume a couple of things about your source above, let me know if I am incorrect. First, looks like the `start_XL_ImageDownload` is called from class B's init method. Second, you changed your last snippet (with the `PDP` variable) to abort immediately for the purpose of debugging. I also want to double check that you are using ARC like it looks like you are. – Mathew Apr 04 '13 at 23:28
  • yes, absolutely correct. – filou Apr 04 '13 at 23:46
  • What I think: The abortion code gets executed, but does not work. When I use [self isCancelled]; and NSLog before and after, it's value is still 0. – filou Apr 05 '13 at 09:18

1 Answers1

0

Just imported the connection into the other class. Now it works perfectly. Could have been a delegation error.

filou
  • 1,609
  • 5
  • 26
  • 53