Skip to content
This repository has been archived by the owner on Oct 1, 2018. It is now read-only.

Commit

Permalink
Fix for #53 . Better download process.
Browse files Browse the repository at this point in the history
  • Loading branch information
nikDemyankov committed Dec 2, 2015
1 parent 4ebc952 commit d994233
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 19 deletions.
7 changes: 3 additions & 4 deletions src/ios/Network/HCPFileDownloader.m
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ - (void) downloadDataFromUrl:(NSURL*) url completionBlock:(HCPDataDownloadComple
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration];

NSURLSessionDataTask* dowloadTask = [session dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
block(data, error);
block(data, error);
}];

[dowloadTask resume];
Expand Down Expand Up @@ -53,9 +53,8 @@ - (void) downloadFiles:(NSArray *)filesList fromURL:(NSURL *)contentURL toFolder
if (error) {
canceled = YES; // do not dispatch any other error
}
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
block(error);
});
// we should already be in the background thread
block(error);
}
}];

Expand Down
12 changes: 7 additions & 5 deletions src/ios/Updater/HCPUpdateLoader.m
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ - (void)setup:(id<HCPFilesStructure>)filesStructure {

- (NSString *)addUpdateTaskToQueueWithConfigUrl:(NSURL *)configUrl {
// TODO: add better communication between installer and loader.
// For now - skip update load request if installation or download is in progress.
if ([HCPUpdateInstaller sharedInstance].isInstallationInProgress || _isExecuting) {
if (_isExecuting) {
return nil;
}

Expand All @@ -52,9 +51,12 @@ - (NSString *)addUpdateTaskToQueueWithConfigUrl:(NSURL *)configUrl {

- (void)executeTask:(id<HCPWorker>)task {
_isExecuting = YES;
[task runWithComplitionBlock:^{
_isExecuting = NO;
}];
// execute in background, so the callbacks don't block main thread
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[task runWithComplitionBlock:^{
_isExecuting = NO;
}];
});
}

@end
39 changes: 29 additions & 10 deletions src/ios/Updater/HCPUpdateLoaderWorker.m
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ - (void)run {
- (void)runWithComplitionBlock:(void (^)(void))updateLoaderComplitionBlock {
NSError *error = nil;

// wait before installation is finished
[self waitForInstallationToComplete];

// initialize before the run
if (![self loadLocalConfigs:&error]) {
updateLoaderComplitionBlock();
Expand Down Expand Up @@ -101,17 +104,37 @@ - (void)runWithComplitionBlock:(void (^)(void))updateLoaderComplitionBlock {
return;
}

// find files that were updated
NSArray *updatedFiles = [_oldManifest calculateDifference:newManifest].updateFileList;
if (updatedFiles.count == 0) {
// compare manifests to find out if anything has changed since the last update
HCPManifestDiff *manifestDiff = [_oldManifest calculateDifference:newManifest];
if (manifestDiff.isEmpty) {
[_manifestStorage store:newManifest inFolder:_pluginFiles.wwwFolder];
[_appConfigStorage store:newAppConfig inFolder:_pluginFiles.wwwFolder];
updateLoaderComplitionBlock();
[self notifyNothingToUpdate:newAppConfig];
return;
}

[self downloadUpdatedFiles:updatedFiles appConfig:newAppConfig manifest:newManifest complitionBlock:updateLoaderComplitionBlock];
// create new download folder
[self recreateDownloadFolder:_pluginFiles.downloadFolder];

// if there is anything to load - do that
NSArray *updatedFiles = manifestDiff.updateFileList;
if (updatedFiles.count > 0) {
[self downloadUpdatedFiles:updatedFiles appConfig:newAppConfig manifest:newManifest complitionBlock:updateLoaderComplitionBlock];
return;
}

// otherwise - update holds only files for deletion;
// just save new configs and notify subscribers about success
[_manifestStorage store:newManifest inFolder:_pluginFiles.downloadFolder];
[_appConfigStorage store:newAppConfig inFolder:_pluginFiles.downloadFolder];

// move download folder to installation folder
[self moveDownloadedContentToInstallationFolder];

updateLoaderComplitionBlock();

[self notifyUpdateDownloadSuccess:newAppConfig];
}];
}];
}
Expand All @@ -120,9 +143,6 @@ - (void)runWithComplitionBlock:(void (^)(void))updateLoaderComplitionBlock {

- (void)downloadUpdatedFiles:(NSArray *)updatedFiles appConfig:(HCPApplicationConfig *)newAppConfig manifest:(HCPContentManifest *)newManifest complitionBlock:(void (^)(void))updateLoaderComplitionBlock{

// create new download folder
[self recreateDownloadFolder:_pluginFiles.downloadFolder];

// download files
HCPFileDownloader *downloader = [[HCPFileDownloader alloc] init];
// TODO: set credentials on downloader
Expand Down Expand Up @@ -210,8 +230,7 @@ - (BOOL)loadLocalConfigs:(NSError **)error {
* Copy all loaded files from download folder to installation folder from which we will install the update.
*/
- (void)moveDownloadedContentToInstallationFolder {
// ignore for now, since we are not launching installation and download tasks at the same time
//[self waitForInstallationToComplete];
[self waitForInstallationToComplete];

NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error = nil;
Expand All @@ -223,7 +242,7 @@ - (void)moveDownloadedContentToInstallationFolder {
*/
- (void)waitForInstallationToComplete {
while ([HCPUpdateInstaller sharedInstance].isInstallationInProgress) {
[NSThread sleepForTimeInterval:0.1]; // avoid busy loop
[NSThread sleepForTimeInterval:1]; // avoid busy loop
}
}

Expand Down

0 comments on commit d994233

Please sign in to comment.