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

Refactors downloading and fixes download start time. #360

Merged
merged 9 commits into from
May 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## master
* Add your own contributions to the next release on the line below this with your name.
- [fixed] Fixed bytes per second on download tasks (which could affect if an image is progressively rendered) [#360](https://github.com/pinterest/PINRemoteImage/pull/360) [garrettmoon](https://github.com/garrettmoon)
- [new] Added request configuration handler to allow customizing HTTP headers per request [#355](https://github.com/pinterest/PINRemoteImage/pull/355) [zachwaugh](https://github.com/zachwaugh)
- [fixed] Moved storage of resume data to disk from memory. [garrettmoon](https://github.com/garrettmoon)
- [fixed] Hopefully fixes crashes occuring in PINURLSessionManager on iOS 9. [garrettmoon](https://github.com/garrettmoon)
Expand Down
14 changes: 14 additions & 0 deletions PINRemoteImage.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
objects = {

/* Begin PBXBuildFile section */
680B83CF1ECFABD400210A55 /* NSURLSessionTask+Timing.h in Headers */ = {isa = PBXBuildFile; fileRef = 680B83CD1ECFABD400210A55 /* NSURLSessionTask+Timing.h */; };
680B83D01ECFABD400210A55 /* NSURLSessionTask+Timing.m in Sources */ = {isa = PBXBuildFile; fileRef = 680B83CE1ECFABD400210A55 /* NSURLSessionTask+Timing.m */; };
6818C1551E551B5A00875DB7 /* PINCache.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 6818C1541E551B5A00875DB7 /* PINCache.framework */; };
6818C1661E551CC600875DB7 /* decode.h in Headers */ = {isa = PBXBuildFile; fileRef = 6818C15E1E551CC600875DB7 /* decode.h */; };
6818C1671E551CC600875DB7 /* demux.h in Headers */ = {isa = PBXBuildFile; fileRef = 6818C15F1E551CC600875DB7 /* demux.h */; };
Expand Down Expand Up @@ -155,6 +157,7 @@
6818C2851E551DA800875DB7 /* utils.h in Headers */ = {isa = PBXBuildFile; fileRef = 6818C26B1E551DA800875DB7 /* utils.h */; };
6858C0751C9CC5BA00E420EB /* PINRemoteLock.h in Headers */ = {isa = PBXBuildFile; fileRef = 6858C0731C9CC5BA00E420EB /* PINRemoteLock.h */; };
6858C0761C9CC5BA00E420EB /* PINRemoteLock.m in Sources */ = {isa = PBXBuildFile; fileRef = 6858C0741C9CC5BA00E420EB /* PINRemoteLock.m */; };
686D48D01ED38FC0003DB4C2 /* PINRemoteImageTask+Subclassing.h in Headers */ = {isa = PBXBuildFile; fileRef = 686D48CE1ED38FC0003DB4C2 /* PINRemoteImageTask+Subclassing.h */; };
687D3FC91E5650790027B131 /* PINOperation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 687D3FC81E5650790027B131 /* PINOperation.framework */; };
68A0FC1C1E523434000B552D /* PINRemoteImageTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 68A0FC1B1E523434000B552D /* PINRemoteImageTests.m */; };
68A0FC1E1E523434000B552D /* PINRemoteImage.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = F1B918D11BCF239200710963 /* PINRemoteImage.framework */; };
Expand Down Expand Up @@ -292,6 +295,9 @@
/* End PBXCopyFilesBuildPhase section */

/* Begin PBXFileReference section */
680B83C71ECE5F9D00210A55 /* PINRemoteImageManager+Private.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = "PINRemoteImageManager+Private.h"; path = "../PINRemoteImageManager+Private.h"; sourceTree = "<group>"; };
680B83CD1ECFABD400210A55 /* NSURLSessionTask+Timing.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSURLSessionTask+Timing.h"; sourceTree = "<group>"; };
680B83CE1ECFABD400210A55 /* NSURLSessionTask+Timing.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSURLSessionTask+Timing.m"; sourceTree = "<group>"; };
6818C1541E551B5A00875DB7 /* PINCache.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = PINCache.framework; path = "Carthage/Checkouts/PINCache/build/Debug-iphoneos/PINCache.framework"; sourceTree = "<group>"; };
6818C1561E551BAB00875DB7 /* PINOperation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = PINOperation.framework; path = "Carthage/Checkouts/PINOperation/build/Debug-iphoneos/PINOperation.framework"; sourceTree = "<group>"; };
6818C15E1E551CC600875DB7 /* decode.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = decode.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -442,6 +448,7 @@
6818C2AE1E564FF900875DB7 /* PINCache.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = PINCache.xcodeproj; path = Carthage/Checkouts/PINCache/PINCache.xcodeproj; sourceTree = "<group>"; };
6858C0731C9CC5BA00E420EB /* PINRemoteLock.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PINRemoteLock.h; sourceTree = "<group>"; };
6858C0741C9CC5BA00E420EB /* PINRemoteLock.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PINRemoteLock.m; sourceTree = "<group>"; };
686D48CE1ED38FC0003DB4C2 /* PINRemoteImageTask+Subclassing.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "PINRemoteImageTask+Subclassing.h"; sourceTree = "<group>"; };
687D3FC81E5650790027B131 /* PINOperation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = PINOperation.framework; path = "Carthage/Checkouts/PINCache/Carthage/Checkouts/PINOperation/build/Debug-iphoneos/PINOperation.framework"; sourceTree = "<group>"; };
68A0FC191E523434000B552D /* PINRemoteImageTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = PINRemoteImageTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
68A0FC1B1E523434000B552D /* PINRemoteImageTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = PINRemoteImageTests.m; sourceTree = "<group>"; };
Expand Down Expand Up @@ -894,12 +901,16 @@
F1B918DD1BCF23C800710963 /* Categories */ = {
isa = PBXGroup;
children = (
680B83C71ECE5F9D00210A55 /* PINRemoteImageManager+Private.h */,
68A6B1D91E5248BF003A92D1 /* PINImage+ScaledImage.m */,
68A6B1DA1E5248BF003A92D1 /* PINImage+ScaledImage.h */,
9DD47FA01C699FDC00F12CA0 /* PINImage+DecodedImage.h */,
9DD47FA11C699FDC00F12CA0 /* PINImage+DecodedImage.m */,
9DD47FA21C699FDC00F12CA0 /* PINImage+WebP.h */,
9DD47FA31C699FDC00F12CA0 /* PINImage+WebP.m */,
680B83CD1ECFABD400210A55 /* NSURLSessionTask+Timing.h */,
680B83CE1ECFABD400210A55 /* NSURLSessionTask+Timing.m */,
686D48CE1ED38FC0003DB4C2 /* PINRemoteImageTask+Subclassing.h */,
);
path = Categories;
sourceTree = "<group>";
Expand Down Expand Up @@ -972,12 +983,14 @@
6818C1661E551CC600875DB7 /* decode.h in Headers */,
6818C1FE1E551D1D00875DB7 /* neon.h in Headers */,
6818C1691E551CC600875DB7 /* extras.h in Headers */,
686D48D01ED38FC0003DB4C2 /* PINRemoteImageTask+Subclassing.h in Headers */,
6818C1671E551CC600875DB7 /* demux.h in Headers */,
F1B919001BCF23C900710963 /* NSData+ImageDetectors.h in Headers */,
F1B9191E1BCF23C900710963 /* PINURLSessionManager.h in Headers */,
6818C2791E551DA800875DB7 /* huffman.h in Headers */,
6818C2771E551DA800875DB7 /* huffman_encode.h in Headers */,
6818C2301E551D7500875DB7 /* cost.h in Headers */,
680B83CF1ECFABD400210A55 /* NSURLSessionTask+Timing.h in Headers */,
F1B919061BCF23C900710963 /* FLAnimatedImageView+PINRemoteImage.h in Headers */,
6818C16D1E551CC600875DB7 /* types.h in Headers */,
F1B919111BCF23C900710963 /* PINRemoteImageCallbacks.h in Headers */,
Expand Down Expand Up @@ -1231,6 +1244,7 @@
6818C22B1E551D7500875DB7 /* analysis.c in Sources */,
6818C1E51E551D1D00875DB7 /* dec.c in Sources */,
6818C1DD1E551D1D00875DB7 /* cpu.c in Sources */,
680B83D01ECFABD400210A55 /* NSURLSessionTask+Timing.m in Sources */,
6818C20A1E551D1E00875DB7 /* yuv_sse2.c in Sources */,
6818C2451E551D7500875DB7 /* webpenc.c in Sources */,
6818C2311E551D7500875DB7 /* delta_palettization.c in Sources */,
Expand Down
17 changes: 17 additions & 0 deletions Source/Classes/Categories/NSURLSessionTask+Timing.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// NSURLSessionTask+Timing.h
// PINRemoteImage
//
// Created by Garrett Moon on 5/19/17.
// Copyright © 2017 Pinterest. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface NSURLSessionTask (Timing)

- (void)PIN_setupSessionTaskObserver;
- (CFTimeInterval)PIN_startTime;
- (CFTimeInterval)PIN_endTime;

@end
111 changes: 111 additions & 0 deletions Source/Classes/Categories/NSURLSessionTask+Timing.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
//
// NSURLSessionTask+Timing.m
// PINRemoteImage
//
// Created by Garrett Moon on 5/19/17.
// Copyright © 2017 Pinterest. All rights reserved.
//

#import "NSURLSessionTask+Timing.h"

#import <objc/runtime.h>
#import <QuartzCore/QuartzCore.h>

@interface PINURLSessionTaskObserver : NSObject

@property (atomic, assign) CFTimeInterval startTime;
@property (atomic, assign) CFTimeInterval endTime;

- (instancetype)init NS_UNAVAILABLE;
- (instancetype)initWithTask:(NSURLSessionTask *)task NS_DESIGNATED_INITIALIZER;

@end

@implementation PINURLSessionTaskObserver

- (instancetype)initWithTask:(NSURLSessionTask *)task
{
if (self = [super init]) {
_startTime = 0;
_endTime = 0;
[task addObserver:self forKeyPath:@"state" options:0 context:nil];
}
return self;
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context
{
NSURLSessionTask *task = (NSURLSessionTask *)object;
switch (task.state) {
case NSURLSessionTaskStateRunning:
if (self.startTime == 0) {
self.startTime = CACurrentMediaTime();
}
break;

case NSURLSessionTaskStateCompleted:
NSAssert(self.startTime != 0, @"Expect that task was started before it's completed.");
if (self.endTime == 0) {
self.endTime = CACurrentMediaTime();
}
break;

default:
break;
}
}

@end

@implementation NSURLSessionTask (Additions)

- (void)PIN_setupSessionTaskObserver
{
// It's necessary to swizzle dealloc here to remove the observer :(
// I wasn't able to figure out another way; kvo assertion about observed objects being observed occurs
// *before* associated objects are dealloc'd
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
SEL deallocSelector = NSSelectorFromString(@"dealloc");
Method deallocMethod = class_getInstanceMethod([NSURLSessionTask class], deallocSelector);
IMP originalImplementation = method_getImplementation(deallocMethod);
IMP newImplementation = imp_implementationWithBlock(^(void *obj){
@autoreleasepool {
//remove state observer
PINURLSessionTaskObserver *observer = objc_getAssociatedObject((__bridge id)obj, @selector(PIN_setupSessionTaskObserver));
if (observer) {
[(__bridge id)obj removeObserver:observer forKeyPath:@"state"];
}
}

//casting original implementation is necessary to ensure ARC doesn't attempt to retain during dealloc
((void (*)(void *, SEL))originalImplementation)(obj, deallocSelector);
});
class_replaceMethod([NSURLSessionTask class], deallocSelector, newImplementation, method_getTypeEncoding(deallocMethod));
});

PINURLSessionTaskObserver *observer = [[PINURLSessionTaskObserver alloc] initWithTask:self];
objc_setAssociatedObject(self, @selector(PIN_setupSessionTaskObserver), observer, OBJC_ASSOCIATION_RETAIN);
}

- (CFTimeInterval)PIN_startTime
{
PINURLSessionTaskObserver *observer = objc_getAssociatedObject(self, @selector(PIN_setupSessionTaskObserver));
NSAssert(observer != nil, @"setupSessionTaskObserver should have been called before.");
if (observer == nil) {
return 0;
}
return observer.startTime;
}

- (CFTimeInterval)PIN_endTime
{
PINURLSessionTaskObserver *observer = objc_getAssociatedObject(self, @selector(PIN_setupSessionTaskObserver));
NSAssert(observer != nil, @"setupSessionTaskObserver should have been called before.");
if (observer == nil) {
return 0;
}
return observer.endTime;
}

@end
20 changes: 20 additions & 0 deletions Source/Classes/Categories/PINRemoteImageTask+Subclassing.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//
// PINRemoteImageTask+Subclassing.h
// PINRemoteImage
//
// Created by Garrett Moon on 5/22/17.
// Copyright © 2017 Pinterest. All rights reserved.
//

#import "PINRemoteImageTask.h"

NS_ASSUME_NONNULL_BEGIN

@interface PINRemoteImageTask (Subclassing)

- (NSMutableDictionary *)l_callbackBlocks;
- (BOOL)l_cancelWithUUID:(NSUUID *)UUID resume:(PINResume * _Nullable * _Nullable)resume;

@end

NS_ASSUME_NONNULL_END
9 changes: 8 additions & 1 deletion Source/Classes/PINProgressiveImage.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,19 @@

#import "PINRemoteImageMacros.h"

@class PINRemoteImageDownloadTask;

/** An object which store the data of a downloading image and vends progressive scans **/
@interface PINProgressiveImage : NSObject

@property (atomic, copy, nonnull) NSArray *progressThresholds;
@property (atomic, assign) CFTimeInterval estimatedRemainingTimeThreshold;
@property (atomic, assign) CFTimeInterval startTime;
@property (nonatomic, strong, readonly, nonnull) NSURLSessionDataTask * dataTask;
@property (nonatomic, readonly) float bytesPerSecond;
@property (nonatomic, readonly) CFTimeInterval estimatedRemainingTime;

- (nonnull instancetype)init NS_UNAVAILABLE;
- (nonnull instancetype)initWithDataTask:(nonnull NSURLSessionDataTask *)dataTask;

- (void)updateProgressiveImageWithData:(nonnull NSData *)data expectedNumberOfBytes:(int64_t)expectedNumberOfBytes isResume:(BOOL)isResume;

Expand Down
Loading