使用OperationQueue以同步方式执行操作

时间:2017-07-31 11:40:10

标签: ios objective-c nsoperationqueue

我在循环中使用不同的参数执行相同的get调用。这是我的代码:

for (Printer* printer in responsePrinterList){

    NSURLComponents *components = [NSURLComponents componentsWithString:notification_count];
    NSURLQueryItem *assetId = [NSURLQueryItem queryItemWithName:@"assetId" value:[printer printerHash]];
    NSURLQueryItem *count = [NSURLQueryItem queryItemWithName:@"state" value:@"active"];
    components.queryItems = @[ assetId, count];
    NSURL *url = components.URL;
    NSString* urlString = url.absoluteString;
    UrlRequest *urlRequest = [[UrlRequest alloc] initUrlRequestWithURL:urlString setHTTPBody:nil shouldHandleCookies:NO isHTTPMethodPost:NO withTimeoutInterval:30];

    NSString *authorizationKey = access_token;
    NSString *authorizationValue = [NSString stringWithFormat:@"Bearer %@",authorizationKey];
    [urlRequest addValue:@"application/json;charset=UTF-8" forHTTPHeaderField:@"Content-Type"];
    [urlRequest addValue:authorizationValue forHTTPHeaderField:@"Authorization"];

    UrlSession *urlSession = [[UrlSession alloc] initUrlSession];
    self.eccUserInfoOp = [[InvocationOperation alloc] initInvocationOpearationWithTarget:self selector:@selector(SuccessWithData:) object:nil urlSession:urlSession urlRequest:urlRequest];
    self.eccUserInfoOp.failedInvocation = [self.eccUserInfoOp createInvocationWithTarget:self selector:@selector(FailedWithData:)];
    //[self.eccOperationQueue setMaxConcurrentOperationCount:1];
    [self.operationQueue addOperation:self.eccUserInfoOp];
 }

以下是我的OperationQueue类的代码

OperationQueue.h

@interface OperationQueue : NSOperationQueue

+ (instancetype)sharedInstanceOfOperationQueue;

@end

OperationQueue.m

@implementation OperationQueue
+ (instancetype)sharedInstanceOfOperationQueue {
    static dispatch_once_t onceOfOperationQueue;
    static id sharedInstanceOfOperationQueue;
    dispatch_once(&onceOfOperationQueue, ^{
        sharedInstanceOfOperationQueue = [[self alloc] init];
    });
    return sharedInstanceOfOperationQueue;
}


@end

我的要求是这些单独的来电按照我在循环中调用它们的顺序执行,即列表中第一台打印机的响应应先返回,然后再返回第二台打印机,依此类推。

我不确定如何使我的Operation Queue连续并实现此目的。

0 个答案:

没有答案