【问题标题】:RestKit - how to queue resource loading?RestKit - 如何排队资源加载?
【发布时间】:2012-10-27 17:49:54
【问题描述】:

所以,这是我的问题。

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

完成这种行为的正确、更简洁的方法是什么?

【问题讨论】:

    标签: iphone ios ipad restkit


    【解决方案1】:

    好的,我想我明白了。

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

    我有伪代码:

    1. 创建一个RKRequestQueue 并将其并发定义为1。
    2. 将其标记为已暂停,以便它等待加载,直到我完成对资源加载请求的排队。
    3. 循环我的资源加载请求并按照我希望它们执行的顺序添加它们。

    我们从请求队列的延迟加载开始

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-08-13
      • 1970-01-01
      • 1970-01-01
      • 2021-07-25
      • 1970-01-01
      • 1970-01-01
      • 2010-10-19
      • 2017-07-04
      相关资源
      最近更新 更多