将 RSSParser 用于今日扩展小部件

Using RSSParser for Today Extension Widget

我正在尝试从 RSS URL 获取最新数据并在 Today Widget 扩展中显示其内容,RSSParser 但问题是它 returns 无效!这是我的代码:

- (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");

}

不熟悉库,但调用模式足够熟悉,可以假设请求异步运行。这意味着代码从上到下执行的正常假设不成立。

- (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");
}