不同的Web服务调用不同的视图控制器Restkit

时间:2012-08-07 07:41:59

标签: ios web-services ipad restkit

我是一名asp.net开发人员并开始学习目标c。几天来一直在努力找到解决方案:

我对ios开发完全陌生,请原谅我,如果我的问题听起来很愚蠢。

我有2个表视图控制器:

AnnouncementsTableViewController和NewsTableViewController。

对于这些控制器中的每一个,我使用rest kit调用Web服务: 这是我在NewsTableViewController

中的代码
- (void)viewDidLoad
{
[super viewDidLoad];
[self sendRequest];

}
- (void)sendRequest
{

//set the base URL
RKURL *baseURL = [RKURL URLWithBaseURLString:@"http://someurl.com/WcfDataService.svc"];
NSString *format=@"json";
NSString *un = @"blah";
NSString *pw = @"blah";

RKObjectManager *objectManager1 = [RKObjectManager objectManagerWithBaseURL:baseURL];
objectManager1.client.baseURL = baseURL;


NSDictionary *queryParams;
queryParams = [NSDictionary dictionaryWithObjectsAndKeys:format,@"format", nil];

objectManager1.client.username=un;
objectManager1.client.password=pw;

//set news mapping

RKObjectMapping *newsMapping = [RKObjectMapping mappingForClass:[News class]];


[newsMapping setSetDefaultValueForMissingAttributes:true]; 
[newsMapping mapKeyPath:@"NEWSTITLE" 
            toAttribute:@"name"]; 

[newsMapping mapKeyPath:@"NEWSBODY" 
            toAttribute:@"newsBody"]; 


 RKURL *newsURL = [RKURL URLWithBaseURL:[objectManager1 baseURL] resourcePath:@"/NEWS"   queryParameters:queryParams];


 [objectManager1 loadObjectsAtResourcePath:[NSString stringWithFormat:@"%@?$%@", [newsURL resourcePath], [newsURL query]] usingBlock:^(RKObjectLoader *loader) 

 {

     [loader.mappingProvider setObjectMapping:newsMapping forKeyPath:@"d"];

     loader.method = RKRequestMethodGET;

     loader.delegate = self;

     loader.userData=@"NewsData";
 }];

}

- (void)objectLoader:(RKObjectLoader *)objectLoader didFailWithError:(NSError *)error
{
NSLog(@"Error: %@", [error localizedDescription]);
}

- (void)request:(RKRequest*)request didLoadResponse:(RKResponse*)response {
NSLog(@"response code: %d", [response statusCode]);
}

- (void)objectLoader:(RKObjectLoader *)objectLoader didLoadObjects:(NSArray *)objects
{
NSString* source = (NSString*) objectLoader.userData;

NSLog(@"objects[%d]", [objects count]);
NSLog(@"%@", source);
data2 = objects;


 [self.tableView reloadData];

}

我在AnnouncementsViewController上执行与上面完全相同的代码(只是更改基本URL和映射)。

问题是它只加载newstableviewcontroller而不是公告。

我已阅读此https://github.com/RestKit/RestKit/wiki/Using-Multiple-Base-URLs-%28and-Multiple-Object-Managers%29

但似乎无法理解并适用于我的代码。

我希望有人可以解释一下!

祝你好运, Rikuna

1 个答案:

答案 0 :(得分:0)

@PauldeLange感谢您抽出宝贵时间作出回应。我终于修好了。这条线就是魔术:

 RKObjectManager *objectManager =  [RKObjectManager sharedManager];