Skip to content

Commit e00d35a

Browse files
authored
Punt pause task to the end of the queue (#8296)
* Punt pause task to the end of the queue * allow more generous execution on the work queue before pause * Use one frame delay? * Fix timing issue * style
1 parent 4ef6248 commit e00d35a

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

FirebaseStorage/Sources/FIRStorage.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ - (instancetype)initWithApp:(FIRApp *)app
166166
_scheme = kFIRStorageScheme;
167167
_port = @(kFIRStoragePort);
168168
_fetcherServiceForApp = nil; // Configured in `ensureConfigured()`
169+
// Must be a serial queue.
169170
_dispatchQueue = dispatch_queue_create("com.google.firebase.storage", DISPATCH_QUEUE_SERIAL);
170171
_maxDownloadRetryTime = 600.0;
171172
_maxDownloadRetryInterval =

FirebaseStorage/Sources/FIRStorageDownloadTask.m

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -165,13 +165,23 @@ - (void)cancelWithError:(NSError *)error {
165165
- (void)pause {
166166
__weak FIRStorageDownloadTask *weakSelf = self;
167167
[self dispatchAsync:^() {
168-
weakSelf.state = FIRStorageTaskStatePausing;
169-
[weakSelf.fetcher stopFetching];
170-
// Give the resume callback a chance to run (if scheduled)
171-
[weakSelf.fetcher waitForCompletionWithTimeout:0.001];
172-
weakSelf.state = FIRStorageTaskStatePaused;
173-
FIRStorageTaskSnapshot *snapshot = weakSelf.snapshot;
174-
[weakSelf fireHandlersForStatus:FIRStorageTaskStatusPause snapshot:snapshot];
168+
__strong FIRStorageDownloadTask *strongSelf = weakSelf;
169+
if (!strongSelf || strongSelf.state == FIRStorageTaskStatePaused ||
170+
strongSelf.state == FIRStorageTaskStatePausing) {
171+
return;
172+
}
173+
strongSelf.state = FIRStorageTaskStatePausing;
174+
// Use the resume callback to confirm pause status since it always runs after the last
175+
// NSURLSession update.
176+
[strongSelf.fetcher setResumeDataBlock:^(NSData *data) {
177+
// Silence compiler warning about retain cycles
178+
__strong __typeof(self) strong = weakSelf;
179+
strong->_downloadData = data;
180+
strong.state = FIRStorageTaskStatePaused;
181+
FIRStorageTaskSnapshot *snapshot = strong.snapshot;
182+
[strong fireHandlersForStatus:FIRStorageTaskStatusPause snapshot:snapshot];
183+
}];
184+
[strongSelf.fetcher stopFetching];
175185
}];
176186
}
177187

0 commit comments

Comments
 (0)