我希望我的应用程序下载一个zipfile和几部电影。在继续我的应用之前,我想等待所有人准备好。 这是迄今为止的代码。问题是,一旦zipfile被下载并解压缩,应用程序将始终继续,它将不会等待电影完成。我已经将所有电影操作设置为'main'操作中的依赖项,但这没有帮助。 我错过了什么?
// 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);
}];
即使电影仍在下载,它也会始终运行此完成模块。 有关为什么这不符合预期的任何提示?或者我是否错误地解释了依赖关系?