0

在 iOS 中,我需要调用相同的方法次数,但应用程序必须等到方法完成它的第一个任务。(我不能使用 bool 值来检查函数是否正在运行)。

如何将请求排队或等到上一个任务完成?有没有办法可以使用 NSOperation 或 NSThread?

谢谢

这是我需要多次调用的方法

- (void)fetchJobs
{

    dispatch_queue_t queueLocal = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_group_t groupLocal = dispatch_group_create();

    UIApplication *app = [UIApplication sharedApplication];

    NSTimeInterval backgroundTime = app.backgroundTimeRemaining;
    NSLog(@"Background task remain time: %g seconds", (double)backgroundTime);


    [[NSNotificationCenter defaultCenter] postNotificationName:@"UPLOAD_PROCESS_START" object:nil];

    self.assetUpdateTaskID = [app beginBackgroundTaskWithExpirationHandler:^{
        dispatch_async(dispatch_get_main_queue(), ^{

            [self endTask];
        }); 
    }];

    dispatch_group_async(groupLocal, queueLocal, ^{

        NSEntityDescription *entity = [NSEntityDescription entityForName:@"Files" inManagedObjectContext:self.managedObjectContext];

        //Setup the fetch request
        NSFetchRequest *request = [[NSFetchRequest alloc] init];
        [request setEntity:entity];

        NSPredicate *predicate = [NSPredicate predicateWithFormat: @"uploaded like %@", [NSString stringWithFormat:@"0"]];
        [request setPredicate:predicate];

        NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"timeStamp" ascending:YES];
        [request setSortDescriptors:[NSArray arrayWithObject:sortDescriptor]];

        NSError *error = nil;

        //fetch the records and handle an error
        NSArray *fetchResults = [self.managedObjectContext executeFetchRequest:request error:&error];


        if (fetchResults == nil) {
            NSLog(@"File Data fetching error : %@", [error localizedDescription]);
        }
        else{
            if ([fetchResults count] > 0) {            

                dispatch_apply([fetchResults count], queue, ^(size_t i){
                    NSLog(@"Loop count %zu", i);

                    Files *file = [fetchResults objectAtIndex:i]; 
                    self.manageObjectForFiles = file;
                    NSLog(@"Fetched File details -> Name: %@ & status: %@" , file.fileName, file.uploaded);                    

                    BOOL uploaded = [self filePosting:file.fileName];


                    if (uploaded) {

                        [self.managedObjectContext deleteObject:file];
                        NSError *error;

                        if (![self.managedObjectContext save:&error]) {
                            NSLog(@"File table update error : %@", [error description]);
                        }
                    }                     

                });
                /*
                for (int i = 0; i < [fetchResults count]; i++) {                    

                }*/
            }            
        }

        dispatch_async(dispatch_get_main_queue(), ^{

            [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

            int remainJobs = [self fetchFileTableCount];
            if (remainJobs > 0) {

                [UIApplication sharedApplication].applicationIconBadgeNumber = remainJobs;
                [[NSNotificationCenter defaultCenter] postNotificationName:@"UPLOAD_REMAIN" object:nil];
            }
            else{
                [UIApplication sharedApplication].applicationIconBadgeNumber = 0;
                [[NSNotificationCenter defaultCenter] postNotificationName:@"UPLOAD_PROCESS_COMPLETED" object:nil];
                NSLog(@"-----------------------------------------------");
                NSLog(@"There are no more uploads");
                NSLog(@"-----------------------------------------------");
            }
            [self endTask];                                    

        });

    });

    dispatch_release(groupLocal);

}
4

1 回答 1

1

您没有说调用是否必须是串行的,或者它们是否可以并发。如果并发,您可以使用 NSOperation 的 mainQueue,挂起它,添加除第一个调用之外的所有内容,然后在您的方法中,最后查看队列是否挂起,如果是,则恢复它。

使用 Grand Central Dispatch (CGD),您可以更轻松地创建串行调度队列,并做同样的事情 - 挂起它,添加除第一个调用之外的所有调用,然后在第一个调用结束时取消挂起队列。如果您需要它在主线程上运行,您可以将串行队列绑定到主 GCD 队列。

于 2012-07-17T17:47:04.640 回答