gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 03:07:50 24 4
gpt4 key购买 nike

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

我需要做的是:

从以下地址获取数据:

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

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

一旦所有请求 完成,我想运行一段代码(例如调用一个新的 View Controller )。执行此操作的最佳方法是什么?

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

- (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",
@"created_at": @"created_at",
@"resource_uri": @"resource_uri",
@"updated_at": @"updated_at",
@"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, RKMappingResult *mappingResult) {
NSLog(@"SUCCESS: %@", mappingResult.array);
} failure: ^(RKObjectRequestOperation * operation, NSError * error) {
NSLog(@"FAILURE %@", error);
}];
}

有什么建议吗?

最佳答案

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

或者,我认为这实际上是更简单、更清洁的解决方案,如对 this question 的已接受答案中所述使用调度组。 .

关于ios - RestKit:如何批处理多个请求并在完成后获得响应?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14956485/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com