I am trying to get latest data from a RSS URL and show its content in Today Widget extension , with RSSParser
but the problem is it returns null ! here is my codes :
- (void)viewDidLoad {
[super viewDidLoad];
NSURLRequest *req = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://myBlog.net/?feed=rss2"]];
[RSSParser parseRSSFeedForRequest:req success:^(NSArray *feedItems) {
[self setDataSource:feedItems];
} failure:^(NSError *error) {
[self setTitle:@"Error"];
NSLog(@"Error: %@",error);
}];
item = [self.dataSource objectAtIndex:0];
//this returns null:
NSLog(@"Title is %@",[item title]);
NSLog(@"Widget Runs");
}
Not familiar with the library, but the call pattern is familiar enough to assume that the request runs asynchronously. That means that one's normal assumption that code executes top to bottom doesn't hold.
- (void)viewDidLoad {
[super viewDidLoad];
NSURLRequest *req = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://myBlog.net/?feed=rss2"]];
[RSSParser parseRSSFeedForRequest:req success:^(NSArray *feedItems) {
// this runs after the request completes
NSLog(@"Got here #2");
// moving your test code here will allow it to work
item = [self.dataSource objectAtIndex:0];
NSLog(@"Title is %@",[item title]);
} failure:^(NSError *error) {
[self setTitle:@"Error"];
NSLog(@"Error: %@",error);
}];
// this runs before the request begins
NSLog(@"Got here #1");
}