RestKit - 如何排队资源加载?
Posted
技术标签:
【中文标题】RestKit - 如何排队资源加载?【英文标题】:RestKit - how to queue resource loading? 【发布时间】:2012-10-27 17:49:54 【问题描述】:所以,这是我的问题。
我想做的是排队加载资源;这样每个资源请求加载一次完成一个,一个接一个(控制我接收这些资源的顺序)。
完成这种行为的正确、更简洁的方法是什么?
【问题讨论】:
【参考方案1】:好的,我想我明白了。
每个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];
;
];
【讨论】:
以上是关于RestKit - 如何排队资源加载?的主要内容,如果未能解决你的问题,请参考以下文章
RestKit/RKObjectMapping:从服务器加载数据后如何添加其他属性?