Skip to content
This repository was archived by the owner on Jan 17, 2023. It is now read-only.

Conversation

@leavesster
Copy link

Goals ⚽

when UIImageView download same URL twice, the second callback never get called. This behavior is different from the normal logic (normally, the first request will not call back, the second will call back when the request is not equal).

  • different URL
 [self.imageView setImageWithURLRequest:urlRequest1 placeholderImage:nil success:^(NSURLRequest * _Nonnull request, NSHTTPURLResponse * _Nullable response, UIImage * _Nonnull image) { NSLog(@"first callback");// never be called } failure:nil]; [self.imageView setImageWithURLRequest:urlRequest2 placeholderImage:nil success:^(NSURLRequest * _Nonnull request, NSHTTPURLResponse * _Nullable response, UIImage * _Nonnull image) { NSLog(@"second callback");// will be called here } failure:nil];
  • same URL
 [self.imageView setImageWithURLRequest:self.jpegURLRequest placeholderImage:nil success:^(NSURLRequest * _Nonnull request, NSHTTPURLResponse * _Nullable response, UIImage * _Nonnull image) { NSLog(@"first callback");// will be called here } failure:nil]; [self.imageView setImageWithURLRequest:self.jpegURLRequest placeholderImage:nil success:^(NSURLRequest * _Nonnull request, NSHTTPURLResponse * _Nullable response, UIImage * _Nonnull image) { NSLog(@"second callback");// never be called } failure:nil];

Implementation Details 🚧

- (void)setImageWithURLRequest:(NSURLRequest *)urlRequest placeholderImage:(UIImage *)placeholderImage success:(void (^)(NSURLRequest *request, NSHTTPURLResponse * _Nullable response, UIImage *image))success failure:(void (^)(NSURLRequest *request, NSHTTPURLResponse * _Nullable response, NSError *error))failure { .... // cancel Image download when activeTaskURL is not equal to previous request // but if activeTaskURL is equal to previous request, not return, just continue. if (![self isActiveTaskURLEqualToURLRequest:urlRequest]) { [self cancelImageDownloadTask]; } .... // cancel last same url request task, because you already add a new AFImageDownloaderResponseHandler to AFImageDownloaderMergedTask. here you cancel the previous task will not cancel the AFImageDownloaderMergedTask if ([self isActiveTaskURLEqualToURLRequest:urlRequest]) { [self cancelImageDownloadTask]; } self.af_activeImageDownloadReceipt = receipt; .... }

Testing Details 🔍

[AFNetworking UIKit Tests] folder

AFUIImageViewTests.m

- (void)testThatDuplicateRequestOnlyCalledLastTime - (void)testThatDuplicateSameRequestOnlyCalledLastTime
@leavesster leavesster changed the title Fix Fix duplicate same request not call last handler Jul 31, 2021
@iLiuChang
Copy link

Your code will cause repeated downloads. If the same request encapsulates a layer of judgment on the basis of AFN.

@leavesster
Copy link
Author

Your code will cause repeated downloads. If the same request encapsulates a layer of judgment on the basis of AFN.

I don't think so. please show me the code if you have this example.

And even though this happened, this bug is also cause another problem. It is not an expected behavior

@iLiuChang
Copy link

No problem, do a return inside downloadImageForURLRequest 👍🏻.

@leavesster
Copy link
Author

No problem, do a return inside downloadImageForURLRequest 👍🏻.

can't understand what you say .

@iLiuChang
Copy link

I didn't look at the whole code before, AFN checked the existing tasks in this place, your code has no problem of repeated download.

 AFImageDownloaderMergedTask *existingMergedTask = self.mergedTasks[URLIdentifier]; if (existingMergedTask != nil) { AFImageDownloaderResponseHandler *handler = [[AFImageDownloaderResponseHandler alloc] initWithUUID:receiptID success:success failure:failure]; [existingMergedTask addResponseHandler:handler]; task = existingMergedTask.task; return; } 
@leavesster
Copy link
Author

I didn't look at the whole code before, AFN checked the existing tasks in this place, your code has no problem of repeated download.

 AFImageDownloaderMergedTask *existingMergedTask = self.mergedTasks[URLIdentifier]; if (existingMergedTask != nil) { AFImageDownloaderResponseHandler *handler = [[AFImageDownloaderResponseHandler alloc] initWithUUID:receiptID success:success failure:failure]; [existingMergedTask addResponseHandler:handler]; task = existingMergedTask.task; return; } 

I highly recommend you read AFN's whole code. I've learned a lot from it.

@iLiuChang
Copy link

👌🏻 thanks @leavesster

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

2 participants