Skip to content

Commit 454c368

Browse files
committed
Prevent UI thread from blocking during batch upload
The `batchUploadFiles` call does not report back a response until all uploads are completed. To do this, it calls sleep(1) while it waits for (1) everything to finish, (2) a cancel request, or (3) all uploads to finish. The calls to sleep(1) are made inside a `setResponseBlock` handler that is not passed a `queue` parameter. Because the queue isn't specified, it runs on the main thread. This causes apps that call this function to freeze on their main thread for a second. This patch makes a dedicated NSOperationQueue for monitoring the progress.
1 parent f47e528 commit 454c368

File tree

3 files changed

+9
-2
lines changed

3 files changed

+9
-2
lines changed

Source/ObjectiveDropboxOfficial/Shared/Handwritten/Resources/DBCustomDatatypes.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ NS_ASSUME_NONNULL_BEGIN
2525
/// The queue on which most response handling is performed.
2626
@property (nonatomic, readonly) NSOperationQueue *queue;
2727

28+
/// The queue on which we sleep until all processing is completed or the timeout is hit.
29+
@property (nonatomic, readonly) NSOperationQueue *pollingQueue;
30+
2831
/// The dispatch group that pairs upload requests with upload responses so that we can wait for all request/response
2932
/// pairs to complete before batch committing. In this way, we can start many upload requests (for files under the chunk
3033
/// limit), without waiting for the corresponding response.

Source/ObjectiveDropboxOfficial/Shared/Handwritten/Resources/DBCustomDatatypes.m

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ - (instancetype)initWithFileCommitInfo:(NSDictionary<NSURL *, DBFILESCommitInfo
1616
// we specifiy a custom queue so that the main thread is not blocked
1717
_queue = queue;
1818
[_queue setMaxConcurrentOperationCount:1];
19+
20+
// create a special background queue to monitor progress and sleep until the processing is complete
21+
_pollingQueue = [NSOperationQueue new];
22+
[_pollingQueue setMaxConcurrentOperationCount:1];
1923

2024
// we want to make sure all of our file data has been uploaded
2125
// before we make our final batch commit call to `/upload_session/finish_batch`,

Source/ObjectiveDropboxOfficial/Shared/Handwritten/Resources/DBCustomRoutes.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ - (void)queryJobStatus:(DBBatchUploadData *)uploadData asyncJobId:(NSString *)as
381381
uploadData.responseBlock(nil, routeError, error, uploadData.fileUrlsToRequestErrors);
382382
}];
383383
}
384-
}];
384+
} queue:uploadData.pollingQueue];
385385
}
386386

387387
- (NSUInteger)endBytesWithFileSize:(NSUInteger)fileSize startBytes:(NSUInteger)startBytes {
@@ -423,7 +423,7 @@ - (void)batchFinishUponCompletion:(DBBatchUploadData *)uploadData {
423423
uploadData.responseBlock(nil, nil, error, uploadData.fileUrlsToRequestErrors);
424424
}];
425425
}
426-
}];
426+
} queue:uploadData.pollingQueue];
427427
});
428428
}
429429

0 commit comments

Comments
 (0)