2

我希望我的应用程序下载一个 zip 文件和几部电影。在继续我的应用程序之前,我想等待所有这些都准备好。这是到目前为止的代码。问题是,一旦下载并解压缩 zip 文件,应用程序将始终继续运行,它不会等待电影完成。我已将所有电影操作设置为“主要”操作中的依赖项,但这并没有帮助。我错过了什么?

// create our network engine
MKNetworkEngine* engine = [[MKNetworkEngine alloc]
                           initWithHostName:hostName customHeaderFields:nil];

// Main operation for our app download.
MKNetworkOperation *op = [engine operationWithPath:appUpdateFile];

// If we have videos, create operations for them
// We add them to an array, so we can start them later

NSMutableArray *videoOperations = [[NSMutableArray alloc] init];
if ([appVideos isEqual:@"no_videos"]) {
    NSLog(@"We have no videos");
} else {
    for(id anObject in appVideos) {
        MKNetworkOperation *opVideo = [engine operationWithPath:[anObject valueForKey:@"downloadUrl"]];
        // add this operation as a dependency to our main operation
        [op addDependency:opVideo];
        NSString *videoFilePath = [videoFolder stringByAppendingPathComponent:[anObject valueForKey:@"name"]];
        [opVideo addDownloadStream:[NSOutputStream outputStreamToFileAtPath:videoFilePath append:YES]];
        [opVideo addCompletionHandler:^(MKNetworkOperation *completedOperation) {
            NSLog(@"We downloaded video %@", [anObject valueForKey:@"name"]);
        } errorHandler:^(MKNetworkOperation *completedOperation, NSError *error) {
            NSLog(@"Error downloading video %@: %@", [anObject valueForKey:@"name"], error);
        }];
        [videoOperations addObject:opVideo];
    }
}

[op addDownloadStream:[NSOutputStream outputStreamToFileAtPath:filePath append:YES]];

[op addCompletionHandler:^(MKNetworkOperation *completedOperation) {
    ZipArchive *zipArchive = [[ZipArchive alloc] init];

    if([zipArchive UnzipOpenFile:filePath]) {
        if ([zipArchive UnzipFileTo:htmlContent overWrite:YES]) {
            //unzipped successfully
            [[NSFileManager defaultManager] removeItemAtPath:filePath error:NULL];
            NSString *htmlContentPath = [htmlContent stringByAppendingPathComponent:@"/htmlContent/html/modules/index/index.html"];
            NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
            [defaults setObject:newAppVersion forKey:@"appVersion"];
            [defaults setObject:htmlContentPath forKey:@"htmlContent"];
            [defaults synchronize];
        } else {
            NSLog(@"Failure To Unzip Archive %@", filePath);
        }
    } else  {
        NSLog(@"Failure To Open Archive %@", filePath);
    }
} errorHandler:^(MKNetworkOperation *completedOperation, NSError *error) {
    NSLog(@"%@", error);
}];

for(id opVideo in videoOperations) {
    [engine enqueueOperation: opVideo];
}
[engine enqueueOperation: op];

return op;

在我想使用它的视图中,我这样做:

    self.updateOperation = [ApplicationDelegate.internetUpdater checkAndUpdate];

    [self.updateOperation onDownloadProgressChanged:^(double progress) {
        pb.progress = progress;
    }];

    [self.updateOperation addCompletionHandler:^(MKNetworkOperation *completedOperation) {
        bottomLabel.text = @"Done";
        [UIView animateWithDuration:0.6
                              delay: 1.0
                            options: UIViewAnimationOptionCurveEaseIn
                         animations:^{
                             v.alpha = 0;
                         }
                         completion:^(BOOL finished){
                             [parentV removeFromSuperview];
                         }
         ];

        [self.delegate loginViewControllerDidFinish:self];
        [self dismissModalViewControllerAnimated:YES];
    } errorHandler:^(MKNetworkOperation *completedOperation, NSError *error) {
        NSLog(@"%@", error);
    }];

即使电影仍在下载,它将始终运行此完成块。关于为什么这不能像我预期的那样工作的任何提示?还是我错误地解释了依赖关系?

4

0 回答 0