ios – RestKit:如何批量处理多个请求并在完成后获得响应?

我刚刚发现了RestKit,它将成为我正在做的应用程序的重要部分.当时,我能够将它与核心数据集成,但还没有找到发送多个GET请求的最佳方式.

我需要做的是:

从以下地址获取数据:

http://url.com/api/banner/
http://url.com/api/category/
http://url.com/api/link/

URL将始终采用以下格式:http://url.com/api/SOMETHING/

完成所有请求后,我想运行一个代码(例如调用一个新的视图控制器).最好的方法是什么?

目前,这是我正在使用的代码

- (id)init
{
    self = [super init];
    if (self) {
        [self setupConnector];
        [self setupDatabase];
        [self setupMappings];
        [self sendRequests];
    }

    return self;
}

- (void)setupConnector
{
    // Initialize RestKIT
    RKObjectManager *objectManager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:@"http://baseURL"]];
    self.managedobjectStore = [[RKManagedobjectStore alloc] initWithManagedobjectModel:[[NLCoreData shared] managedobjectModel]];
    objectManager.managedobjectStore = self.managedobjectStore;
}

- (void)setupDatabase
{
    Nsstring *storePath = [[NLCoreData shared] storePath];
    NSError *error = nil;
    NSPersistentStore *persistentStore = [self.managedobjectStore addsqlitePersistentStoreAtPath:storePath fromSeedDatabaseAtPath:nil withConfiguration:nil options:nil error:&error];
    NSAssert(persistentStore,@"Failed to add persistent store with error: %@",error);

    [self.managedobjectStore createManagedobjectContexts];

    self.managedobjectStore.managedobjectCache = [[RKInMemoryManagedobjectCache alloc] initWithManagedobjectContext:self.managedobjectStore.persistentStoreManagedobjectContext];
}

- (void)setupMappings
{
    RKObjectManager *objectManager = [RKObjectManager sharedManager];

    // Mappings

    // banner
    RKEntityMapping *bannerMapping = [RKEntityMapping mappingForEntityForName:@"Banner" inManagedobjectStore:self.managedobjectStore];
    [bannerMapping addAttributeMappingsFromDictionary:@{
     @"title": @"title",@"id": @"bannerID",@"created_at": @"created_at",@"image": @"image",@"resource_uri": @"resource_uri",@"updated_at": @"updated_at",@"url": @"url"
     }];
    bannerMapping.identificationAttributes = @[ @"bannerID" ];

    RKResponseDescriptor *bannerDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:bannerMapping
                                                                                        pathPattern:@"/api/v1/banner/"
                                                                                            keyPath:@"objects"
                                                                                        statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
    [objectManager addResponseDescriptor:bannerDescriptor];

    // category
    RKEntityMapping *categoryMapping = [RKEntityMapping mappingForEntityForName:@"Category" inManagedobjectStore:self.managedobjectStore];
    [categoryMapping addAttributeMappingsFromDictionary:@{
     @"name": @"name",@"id": @"categoryID",@"active": @"active"
     }];
    categoryMapping.identificationAttributes = @[ @"categoryID" ];

    RKResponseDescriptor *categoryDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:categoryMapping
                                                                                     pathPattern:@"/api/v1/category/"
                                                                                         keyPath:@"objects"
                                                                                     statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
    [objectManager addResponseDescriptor:categoryDescriptor];


}

- (void)sendRequests
{
    RKObjectManager *objectManager = [RKObjectManager sharedManager];

    // Send Request
    [objectManager getobjectsAtPath:@"/api/v1/banner/" parameters:nil success:^(RKObjectRequestOperation * operation,RKMappingResult *mappingResult) {
        NSLog(@"SUCCESS: %@",mappingResult.array);
    } failure: ^(RKObjectRequestOperation * operation,NSError * error) {
        NSLog(@"FAILURE %@",error);
    }];

    // category
    [objectManager getobjectsAtPath:@"/api/v1/category/" parameters:nil success:^(RKObjectRequestOperation * operation,error);
    }];
}

有小费吗?

解决方法

RestKit解决方案是这样的:不是使用方便方法ObjectManager :: getobjectsAtPath,而是必须手动初始化所​​有RKObjectRequestOperations,然后使用 ObjectManager::enqueueBatchOfObjectRequestOperations:progress:completion:方法将它们排入队列.

或者,我认为这实际上更容易和更清洁的解决方案,使用this question接受的答案中描述的调度组.

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...