我需要做的是:
从以下地址获取数据:
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接受的答案中描述的调度组.
总结以上是内存溢出为你收集整理的ios – RestKit:如何批量处理多个请求并在完成后获得响应?全部内容,希望文章能够帮你解决ios – RestKit:如何批量处理多个请求并在完成后获得响应?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)