I’m an asp.net developer and starting to learn objective c. Been struggling for days to find a fix for this:
I’m completely new to ios development so forgive me if my question sound stupid.
I have 2 table view controllers:
AnnouncementsTableViewController and NewsTableViewController.
For each of these controllers Im calling a web service using rest kit:
Here’s my code in 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];
}
And I’m doing the exact same code as above on AnnouncementsViewController (just changing the base URL and mappings).
Problem is it only loads for the newstableviewcontroller and not on announcements.
I have read this https://github.com/RestKit/RestKit/wiki/Using-Multiple-Base-URLs-%28and-Multiple-Object-Managers%29
but cant seem to understand and apply on my code.
I hope someone can shed some light!
Best regards,
Rikuna
@PauldeLange thanks for taking time to respond. I’ve finally fixed it. This line is just what did the magic: