Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AlamofireImageExtended Types #394

Merged
merged 3 commits into from
Feb 23, 2020
Merged

AlamofireImageExtended Types #394

merged 3 commits into from
Feb 23, 2020

Conversation

cnoon
Copy link
Member

@cnoon cnoon commented Feb 23, 2020

Issue Link 🔗

Issue #197

Goals ⚽

To convert all af_ APIs over to af. syntax using an AlamofireImageExtended type.

Implementation Details 🚧

There are a couple things worth calling out in this PR that are different than what we had to do in Alamofire itself.

  1. We have to deal with class and instance setters which is a bit tricky. You need to use the nonmutating keyword for the instance setter. Then you have to use the class type itself for the class getters/setters for associated properties.
  2. I have chosen to deprecate the af_ APIs rather than remove them. We should probably keep them around and remove them in AFI 5.0. Thoughts?

Testing Details 🔍

Test suite is passing with all except the two blur tests that are still failing.

@cnoon cnoon added this to the 4.0.0 milestone Feb 23, 2020
@cnoon cnoon requested a review from jshier February 23, 2020 04:19
@cnoon cnoon self-assigned this Feb 23, 2020
guard
let strongSelf = self,
strongSelf.isURLRequestURLEqualToActiveRequestURL(response.request) &&
strongSelf.af_activeRequestReceipt?.receiptID == downloadID
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Had to remove the weakify/strongify logic here since the container is a struct. Not entirely sure how that ends up working now since I capture self. Thoughts on this @jshier?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it should be okay, but you should see if it can be tested. Perhaps through a local weak reference, or a subclass?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wrote some tests and verified we are now capturing it. Found a pretty slick solution in 6335e8d by weaking the image view itself, then capturing imageView?.af as strongSelf in the closure.

Copy link
Contributor

@jshier jshier left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 This looks great, and I think it's mergeable as is, but I had a few issues for consideration:

  1. It would be nice to use the Alamofire extension types, especially since we're still using the af namespace here. It would require some properties made public but reduces duplication. If you don't think it's worth that, that's fine though, I don't think we'll collide between the libraries.
  2. Some sort of the test for the previously weak capturing closures might be a good idea to ensure we don't produce any new reference cycles. I don't think it's an issue, but if the tests are easy enough it would be worthwhile.
  3. I'm not 100% sure, but it might be worthwhile to explore @dynamicMemberLookup for the extension type, as I think it would help get rid of some type property usage.

Otherwise, this is great, and the deprecations should be very helpful.

//

/// Type that acts as a generic extension point for all `AlamofireImageExtended` types.
public struct AlamofireImageExtension<ExtendedType> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aside from needing to make type and init public, we could use AlamofireExtended instead here, and I'd hoped to when adding it to Alamofire. Since we're reusing the af extension point anyway, not duplicating the extension methodology seems like a good idea.

guard
let strongSelf = self,
strongSelf.isURLRequestURLEqualToActiveRequestURL(response.request) &&
strongSelf.af_activeRequestReceipt?.receiptID == downloadID
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it should be okay, but you should see if it can be tested. Perhaps through a local weak reference, or a subclass?

@@ -400,12 +394,14 @@ extension UIImageView {
///
/// - parameter imageTransition: The image transition to ran on the image view.
/// - parameter image: The image to use for the image transition.
public func run(_ imageTransition: ImageTransition, with image: Image) {
public func run(_ imageTransition: UIImageView.ImageTransition, with image: Image) {
let imageView = type
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the local reference here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Eliminated the need to capture self in the closure and seemed to read better.


// Generate a unique download id to check whether the active request has changed while downloading
let downloadID = UUID().uuidString

// Weakify the image view to allow it to go out-of-memory while download is running if deallocated
weak var imageView = self.type
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@cnoon cnoon mentioned this pull request Feb 23, 2020
@jshier jshier merged commit 31302d6 into master Feb 23, 2020
@jshier jshier deleted the feature/afi-extended branch February 23, 2020 06:06
ryndinol pushed a commit to ryndinol/AlamofireImage that referenced this pull request Apr 26, 2021
* Updated af_ to use af. syntax with AlamofireImageExtended types

* Updated image view and button extensions to weakify during image download

* Updated Alamofire dependency to 5.0.1 and removed AlamofireImageExtended type
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants