所以,这是我的问题。
我想要做的是排队加载资源;这样每个资源请求加载一次完成一个,一个接一个(控制我接收这些资源的顺序)。
完成这种行为的正确和更清洁的方法是什么?
好的,我想我明白了。
RKObjectLoader一个都是对象的子类RKRequest。所以它可能与定制的RKRequestQueue.RKRequestQueue其配置为一次只能处理 1 个元素,我们可以实现排序。通过将其并发设置为 1。我有伪代码:
RKRequestQueue并将其并发定义为 1。我们从请求队列的延迟加载开始
- (RKRequestQueue *)mainDownloadRequestQueue {
    if (!_mainDownloadRequestQueue) {
        // Creating the request queue
        RKRequestQueue * queue = [RKRequestQueue requestQueue];
        //queue.delegate = self;
        queue.concurrentRequestsLimit = 1;
        queue.showsNetworkActivityIndicatorWhenBusy = YES;
        // Start processing!
        [queue start];
        _mainDownloadRequestQueue = [queue retain];
    }
    return _mainDownloadRequestQueue;
}
主要方法可能/应该看起来像这样,我们在块中设置队列,就在 RestKit 检查队列是否可用于处理下载之前。
    - (void)setUpMainQueue {
        // We lock the download queue so that, no download will start until, we want it
        [[self mainDownloadRequestQueue] setSuspended:YES];
        // Fill up the queue
        [self fillQueueWithMandatoryDownloads];
        // No, let's start and wait for data to be there
        [[self mainDownloadRequestQueue] setSuspended:NO];
    } 
    - (void)fillQueueWithMandatoryDownloads {
        // Add the first request
        [self addLanguagesRequest];
        // Add another request
        [self addLanguagesRequest];
        // … Add any other request
    }
    - (void)addLanguagesRequest {
        // Load the object model via RestKit
        RKObjectManager *objectManager = [RKObjectManager sharedManager];
        objectManager.client.baseURL = [RKURL URLWithString:kFoundationHost];
        __unsafe_unretained OMResourceLoader * wSelf = self;
        [objectManager loadObjectsAtResourcePath:@"/sources/api/languages" usingBlock:^(RKObjectLoader * loader) {
            // Set the queue there, this is the one defined
            loader.queue = [wSelf mainDownloadRequestQueue];
            // Do other configuration or behaviour for that
            loader.onDidLoadObjects = ^(NSArray *objects) {
                [[OMLogManager sharedLogManager] log:[objects description] logLevelParam:OM_LOG_LEVEL_INFO exceptionParam:nil errorParam:nil];
            };
        }];
    }
    - (void)addCategoriesRequest {
        // Load the object model via RestKit
        RKObjectManager *objectManager = [RKObjectManager sharedManager];
        objectManager.client.baseURL = [RKURL URLWithString:kFoundationHost];
        __unsafe_unretained OMResourceLoader * wSelf = self;
        [objectManager loadObjectsAtResourcePath:@"/sources/api/categories" usingBlock:^(RKObjectLoader * loader) {
            // Set the queue
            loader.queue = [wSelf mainDownloadRequestQueue];
            loader.onDidFailLoadWithError = ^(NSError * error) {
                [[OMLogManager sharedLogManager] log:@"Categories loading error" logLevelParam:OM_LOG_LEVEL_ERROR exceptionParam:nil errorParam:error];
            };
            loader.onDidFailWithError = ^(NSError * error) {
                [[OMLogManager sharedLogManager] log:@"Categories loading error" logLevelParam:OM_LOG_LEVEL_ERROR exceptionParam:nil errorParam:error];
            };
            loader.onDidLoadResponse = ^(RKResponse *response) {
                [[OMLogManager sharedLogManager] log:[[response bodyAsString] description] logLevelParam:OM_LOG_LEVEL_INFO exceptionParam:nil errorParam:nil];
            };
            loader.onDidLoadObjects = ^(NSArray *objects) {
                [[OMLogManager sharedLogManager] log:[objects description] logLevelParam:OM_LOG_LEVEL_INFO exceptionParam:nil errorParam:nil];
            };
        }];
    }