Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[expo-file-system] Fix lifetime of EXFileSystem
- Loading branch information
Showing
8 changed files
with
288 additions
and
186 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
1 change: 1 addition & 0 deletions
1
ios/Pods/Headers/Private/EXFileSystem/EXSessionTaskDispatcher.h
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
packages/expo-file-system/ios/EXFileSystem/EXSessionTasks/EXSessionTaskDispatcher.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// Copyright 2015-present 650 Industries. All rights reserved. | ||
|
||
#import <Foundation/Foundation.h> | ||
#import <EXFileSystem/EXSessionTaskDelegate.h> | ||
|
||
NS_ASSUME_NONNULL_BEGIN | ||
|
||
@interface EXSessionTaskDispatcher : NSObject <NSURLSessionDelegate> | ||
|
||
- (void)registerTaskDelegate:(EXSessionTaskDelegate *)delegate forTask:(NSURLSessionTask *)task; | ||
|
||
- (void)registerResumableDownloadTask:(NSURLSessionDownloadTask *)task uuid:(NSString *)uuid; | ||
|
||
- (NSURLSessionDownloadTask * _Nullable)resumableDownload:(NSString *)uuid; | ||
|
||
- (void)removeResumableDownload:(NSString *)uuid; | ||
|
||
- (void)deactivate; | ||
|
||
@end | ||
|
||
NS_ASSUME_NONNULL_END |
114 changes: 114 additions & 0 deletions
114
packages/expo-file-system/ios/EXFileSystem/EXSessionTasks/EXSessionTaskDispatcher.m
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
// Copyright 2015-present 650 Industries. All rights reserved. | ||
|
||
#import <EXFileSystem/EXSessionTaskDispatcher.h> | ||
#import <EXFileSystem/EXSessionResumableDownloadTaskDelegate.h> | ||
|
||
@interface EXSessionTaskDispatcher () | ||
|
||
@property (nonatomic, strong) NSMutableDictionary<NSURLSessionTask *, EXSessionTaskDelegate *> *tasks; | ||
@property (nonatomic, strong) NSMutableDictionary<NSString *, NSURLSessionDownloadTask *> *resumableDownloads; | ||
@property (nonatomic) BOOL isActive; | ||
|
||
@end | ||
|
||
@implementation EXSessionTaskDispatcher | ||
|
||
- (instancetype)init | ||
{ | ||
if (self = [super init]) { | ||
_tasks = [NSMutableDictionary dictionary]; | ||
_resumableDownloads = [NSMutableDictionary dictionary]; | ||
_isActive = true; | ||
} | ||
return self; | ||
} | ||
|
||
#pragma mark - public methods | ||
|
||
- (void)registerTaskDelegate:(EXSessionTaskDelegate *)delegate forTask:(NSURLSessionTask *)task | ||
{ | ||
_tasks[task] = delegate; | ||
} | ||
|
||
- (void)registerResumableDownloadTask:(NSURLSessionDownloadTask *)task uuid:(NSString *)uuid | ||
{ | ||
_resumableDownloads[uuid] = task; | ||
} | ||
|
||
- (NSURLSessionDownloadTask * _Nullable)resumableDownload:(NSString *)uuid | ||
{ | ||
return _resumableDownloads[uuid]; | ||
} | ||
|
||
- (void)removeResumableDownload:(NSString *)uuid | ||
{ | ||
NSURLSessionTask *task = _resumableDownloads[uuid]; | ||
if (!task) { | ||
return; | ||
} | ||
|
||
[_resumableDownloads removeObjectForKey:uuid]; | ||
[_tasks removeObjectForKey:task]; | ||
} | ||
|
||
- (void)deactivate | ||
{ | ||
_isActive = false; | ||
} | ||
|
||
#pragma mark - helpers | ||
|
||
- (void)_unregisterIfResumableTaskDelegate:(EXSessionTaskDelegate *)taskDelegate | ||
{ | ||
if ([taskDelegate isKindOfClass:[EXSessionResumableDownloadTaskDelegate class]]) { | ||
EXSessionResumableDownloadTaskDelegate *exResumableTask = (EXSessionResumableDownloadTaskDelegate *)taskDelegate; | ||
[_resumableDownloads removeObjectForKey:exResumableTask.uuid]; | ||
} | ||
} | ||
|
||
#pragma mark - dispatcher | ||
|
||
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location | ||
{ | ||
if (_isActive) { | ||
EXSessionTaskDelegate *exTask = _tasks[downloadTask]; | ||
if (exTask) { | ||
[exTask URLSession:session downloadTask:downloadTask didFinishDownloadingToURL:location]; | ||
[_tasks removeObjectForKey:downloadTask]; | ||
[self _unregisterIfResumableTaskDelegate:exTask]; | ||
} | ||
} | ||
} | ||
|
||
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error | ||
{ | ||
if (_isActive) { | ||
EXSessionTaskDelegate *exTask = _tasks[task]; | ||
if (exTask) { | ||
[exTask URLSession:session task:task didCompleteWithError:error]; | ||
[_tasks removeObjectForKey:task]; | ||
[self _unregisterIfResumableTaskDelegate:exTask]; | ||
} | ||
} | ||
} | ||
|
||
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask | ||
didWriteData:(int64_t)bytesWritten | ||
totalBytesWritten:(int64_t)totalBytesWritten | ||
totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite | ||
{ | ||
if (_isActive) { | ||
EXSessionTaskDelegate *exTask = _tasks[downloadTask]; | ||
[exTask URLSession:session downloadTask:downloadTask didWriteData:bytesWritten totalBytesWritten:totalBytesWritten totalBytesExpectedToWrite:totalBytesExpectedToWrite]; | ||
} | ||
} | ||
|
||
- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data | ||
{ | ||
if (_isActive) { | ||
EXSessionTaskDelegate *exTask = _tasks[dataTask]; | ||
[exTask URLSession:session dataTask:dataTask didReceiveData:data]; | ||
} | ||
} | ||
|
||
@end |