NSBlockOperation не ожидает зависимости перед выполнением

Я учусь NSOperations & NSOperationQueue,

У меня есть набор NSBlockOperation: "ЗАГРУЗИТЬ" и "УДАЛИТЬ". Удалить ДОЛЖЕН ждать завершения загрузки перед выполнением.

Я хотел бы, чтобы одна операция была завершена до перехода к следующему.

я использовал NSThread sleepForTimeInterval для имитации времени ожидания загрузки и удаления.

Однако операции не ожидают завершения набора.

Я поставил maxConcurrentOperationCount к 1, но это не похоже на работу.

Вы можете видеть из вывода, что Set 1 закончил нормально.

Но в наборе два Upload 3 смотрел до того, как Delete 2 был закончен. Тогда Upload/Delete 4 в порядке. Тогда оттуда это начинает смешиваться намного больше.

Любая помощь?

ВЫХОД:

Start UPLOAD 1
Completed UPLOAD 1
Start DELETE 1
Completed DELETE 1

Start UPLOAD 2
Completed UPLOAD 2
Start DELETE 2
Start UPLOAD 3
Completed DELETE 2
Start DELETE 3
Completed UPLOAD 3
Completed DELETE 3

Start UPLOAD 4
Start DELETE 4
Completed UPLOAD 4
Completed DELETE 4

Start UPLOAD 5
Start DELETE 5
Completed UPLOAD 5
Start UPLOAD 6
Completed DELETE 5
Start DELETE 6
Completed UPLOAD 6
Start UPLOAD 7
Completed DELETE 6

КОД:

- (void)viewDidLoad {
    [super viewDidLoad];


    NSOperationQueue *operationQueue = [NSOperationQueue mainQueue];
    operationQueue.maxConcurrentOperationCount = 1;


//pretend there are 100 images that need to be uploaded and information in a SQLite DB waiting to be deleted upon successful upload of the image.

//Upload 1 image at a time, upon successful upload, delete the respective DB info then move to the next image 
    for (__block int i = 1; i < 100; i++){
        NSOperation * UPLOAD = [self createNewOperationWithInt:i Message:@"UPLOAD"];
        NSOperation * DELETE = [self createNewOperationWithInt:i Message:@"DELETE"];
        [DELETE addDependency:UPLOAD];
        [operationQueue addOperation:UPLOAD];
        [operationQueue addOperation:DELETE];

    }

}

- (NSBlockOperation *) createNewOperationWithInt:(int)i Message:(NSString*)message {
    NSBlockOperation * operation = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"Start %@ %i",message , i);

        if ([message containsString:@"UPLOAD"]) {
             [NSThread sleepForTimeInterval:1]; //Pretend there is Network latency on upload
        }

        if ([message containsString:@"DELETE"]) {
            [NSThread sleepForTimeInterval:0.5]; //Pretend the SQLDB is being accessed
        }

    }];
    operation.queuePriority = NSOperationQueuePriorityNormal;
    operation.qualityOfService  = NSOperationQualityOfServiceUtility;
    operation.completionBlock = ^{
       NSLog(@"Completed %@ %i",message , i);
    };

    return operation;
}

1 ответ

Такое поведение, кажется, хорошо. "Проблема" заключается в том, что и блок завершения, и зависимость запускаются после завершения задачи загрузки. Вот почему вы иногда получаете "Start DELETE N+1" перед "Completed UPLOAD N".

Попробуйте добавить сообщение в конце операции блокировки, чтобы убедиться, что оно заканчивается, прежде чем начинать следующее:

NSBlockOperation * operation = [NSBlockOperation blockOperationWithBlock:^{
    NSLog(@"Start %@ %i",message , i);

    if ([message containsString:@"UPLOAD"]) {
         [NSThread sleepForTimeInterval:1]; //Pretend there is Network latency on upload
    }

    if ([message containsString:@"DELETE"]) {
        [NSThread sleepForTimeInterval:0.5]; //Pretend the SQLDB is being accessed
    }

    NSLog(@"Finished %@ %i",message , i);
}];
Другие вопросы по тегам