我需要按顺序完成以下任务,并在完成所有任务后更新UI。
由于数据彼此依赖,我创建了NSOperationQueue *myQueue
,但其中一项任务需要很长时间才能完成,而上一项任务不是等待上一个任务完成而只是更新UI
任务顺序应为
1.显示装载中心
2.Parse html get ids
3.用解析的ids下载Json
4.用json填充数据库
4.1这部分还有很多后台工作,填充数据库大约需要5-6秒
5.隐藏装载中心
因此5在4和4.1完成之前执行.... 基本上我想要这个Serial,但这种情况正在发生我想Concurrent 我也试过'dispatch_sync'没有运气
-(void)viewDidAppear:(BOOL)animated
{
//show hud
[self performSelectorOnMainThread:@selector(showLoadingHud) withObject:self waitUntilDone:YES];
[self startProcess];
}
-(void)startProcess
{
NSOperationQueue *myQueue = [NSOperationQueue mainQueue];
[myQueue setMaxConcurrentOperationCount:1];
__block NSMutableArray *imdbIDs=[[NSMutableArray alloc] init];
NSMutableArray *urlList=[[NSMutableArray alloc] initWithArray:[ImdbURLs getImdburlArray]];
NSBlockOperation *getiImdbViaHtmlParser = [[NSBlockOperation alloc] init];
__weak NSBlockOperation *weakHtmlParser = getiImdbViaHtmlParser;
[weakHtmlParser addExecutionBlock:^{
//first
for (NSString *imdbUrl in urlList) {
...
}];
NSBlockOperation *getJsonFromIMDB = [[NSBlockOperation alloc] init];
__weak NSBlockOperation *weakGetJsonFromIMDB = getJsonFromIMDB ;
[weakGetJsonFromIMDB addExecutionBlock:^{
//second
for (NSString *imdbStrings in imdbIDs) {
//this also calls database methods and populates database with safe threads
[self AFReqTest:imdbStrings];//I guess this causing the problem
}
}];
NSBlockOperation *checkResult = [[NSBlockOperation alloc] init];
__weak NSBlockOperation *weakCheckResult = checkResult ;
[weakCheckResult addExecutionBlock:^{
//dismiss hud
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
[self dismissLoadingHud];//this is getting called before getJsonFromIMDB block finishes
}];
}];
[checkResult addDependency:getJsonFromIMDB];
[getJsonFromIMDB addDependency:getiImdbViaHtmlParser];
[myQueue addOperation:getJsonFromIMDB];
[myQueue addOperation:getiImdbViaHtmlParser];
[myQueue addOperation:checkResult];
}
-(void)AFReqTest:(NSString *)idString
{
...//buch of code
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
//here I call another background method with result
[self insertIntoDatabase:responseObject];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
}
-(void)insertIntoDatabase:(NSDictionary *)responseObject
{
ParseImdbResponse *parse=[[ParseImdbResponse alloc] initWithResponseDic:responseObject];
PopulateDatabase *inserInstance=[[PopulateDatabase alloc] initWithResponseDic:parse];
dispatch_queue_t insertqueue= dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_sync(insertqueue, ^{
[inserInstance inserIntoMoviesTable];
});
dispatch_sync(insertqueue, ^{
[inserInstance inserIntoGenresTable];
});
dispatch_sync(insertqueue, ^{
[inserInstance inserIntoActorsTable];
});
dispatch_sync(insertqueue, ^{
[inserInstance inserIntoDirectorsTable];
});
dispatch_sync(insertqueue, ^{
[inserInstance inserIntoMovieGenresTable];
});
dispatch_sync(insertqueue, ^{
[inserInstance inserIntoMovieCastTable];
});
}
很抱歉,长代码我不确定问题是我的代码结构还是我错过了什么?