RestKit - 如何排队资源加载?

时间:2012-10-27 17:49:55

标签: iphone ios ipad restkit

所以,这是我的问题。

我想做的是排队资源加载;这样每个资源请求加载一次完成一个,一个接一个地完成(控制我接收这些资源的顺序)。

完成此类行为的正确,清洁方法是什么?

1 个答案:

答案 0 :(得分:0)

好的,我想我明白了。

  • 每个RKObjectLoader都是RKRequest个对象的子类。因此,它可能与自定义RKRequestQueue
  • 相关联
  • 设置RKRequestQueue以便在我们可以实现排序时只能处理1个元素。通过将其并发性设置为1。

我有伪代码:

  1. 创建RKRequestQueue并将其并发定义为1.
  2. 将其标记为已暂停,以便在我完成排队资源加载请求之前等待加载。
  3. 遍历我的资源加载请求,并按照我希望它们执行的顺序添加它们。
  4. 我们从延迟加载请求队列开始

    - (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];
                };
            }];
        }