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

Add fetch type observation #237

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion Framework/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CFBundleShortVersionString</key>
<string>2.2.0</string>
<string>2.2.1</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ for (int i = 0; i < 1000; ++i) {
This will result in a backoffTime that has drifted far away from its vanilla exponential calculation. Why? Because we add a random jitter to the calculations in order to prevent clients from connecting at the same time, in order to spread the load out evenly when experiencing a reconnect storm. The jitter gets greater along with the exponent.

### Consumption observation
SPTDataLoaderService allows you to add a consumption observer whose purpose is to monitor the data consumption of the service for both uploads and downloads. This object must conform to the SPTDataLoaderConsumptionObserver protocol. This is quite easy considering it is a single method like so:
SPTDataLoaderService allows you to add a consumption observer whose purpose is to monitor the data consumption of the service for both uploads and downloads and whether the resource was loaded from network or cache. This object must conform to the SPTDataLoaderConsumptionObserver protocol. This is quite easy considering it is a single method like so:
```objc
- (void)load
{
Expand Down
51 changes: 51 additions & 0 deletions Sources/SPTDataLoader/SPTDataLoaderService.m
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,57 @@ - (void)URLSession:(NSURLSession *)session
completionHandler(disposition, credential);
}

- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didFinishCollectingMetrics:(NSURLSessionTaskMetrics *)metrics {

if (self.consumptionObservers.count == 0) {
return;
}

SPTDataLoaderRequestTaskHandler *handler = [self handlerForTask:task];
SPTDataLoaderResponse *response = [handler completeWithError:nil];

if (response == nil && !handler.cancelled) {
return;
}

NSURLSessionTaskTransactionMetrics *lastTransactionMetric = metrics.transactionMetrics.lastObject; // Last transaction after any redirects.

if (lastTransactionMetric == nil) {
return;
}

SPTDataLoaderFetchType fetchType = SPTDataLoaderFetchTypeUnknown;

switch (lastTransactionMetric.resourceFetchType) {
case NSURLSessionTaskMetricsResourceFetchTypeUnknown:
fetchType = SPTDataLoaderFetchTypeUnknown;
break;
case NSURLSessionTaskMetricsResourceFetchTypeLocalCache:
fetchType = SPTDataLoaderFetchTypeLocalCache;
break;
case NSURLSessionTaskMetricsResourceFetchTypeNetworkLoad:
fetchType = SPTDataLoaderFetchTypeNetwork;
break;
case NSURLSessionTaskMetricsResourceFetchTypeServerPush:
fetchType = SPTDataLoaderFetchTypeNetwork;
break;
}

@synchronized(self.consumptionObservers) {
for (id<SPTDataLoaderConsumptionObserver> consumptionObserver in self.consumptionObservers) {
dispatch_block_t observerBlock = ^ {
[consumptionObserver endedRequestWithResponse:response fetchType:fetchType];
};
dispatch_queue_t queue = [self.consumptionObservers objectForKey:consumptionObserver];
if ([NSThread isMainThread] && queue == dispatch_get_main_queue()) {
observerBlock();
} else {
dispatch_async(queue, observerBlock);
}
}
}
}

#pragma mark NSURLSessionTaskDelegate

- (void)URLSession:(NSURLSession *)session
Expand Down
13 changes: 13 additions & 0 deletions include/SPTDataLoader/SPTDataLoaderConsumptionObserver.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@

NS_ASSUME_NONNULL_BEGIN

typedef NS_ENUM(NSInteger, SPTDataLoaderFetchType) {
SPTDataLoaderFetchTypeUnknown,
SPTDataLoaderFetchTypeNetwork,
SPTDataLoaderFetchTypeLocalCache
};

/**
The protocol an observer of the data loaders consumption must conform to
*/
Expand All @@ -35,6 +41,13 @@ NS_ASSUME_NONNULL_BEGIN
bytesDownloaded:(int)bytesDownloaded
bytesUploaded:(int)bytesUploaded;

/**
Called when a request ends (either via cancel or receiving a server response
@param response The response the request was ended with
@param fetchType Whether the resource was loaded from network or cache (if known).
*/
- (void)endedRequestWithResponse:(SPTDataLoaderResponse *)response fetchType:(SPTDataLoaderFetchType)fetchType;

@end

NS_ASSUME_NONNULL_END