我目前正在尝试使用MWFeedParser https://github.com/mwaterfall/MWFeedParser制作类似iPhone应用的RSS来解析我的xml Feed。目前,我正尝试在我的应用中实现后台提取。当有新项目时,用户会收到本地通知。我很难检查我的RSS Feed中是否有新条目我目前正在使用下面的代码。 parsedItems是一个NSArray,在调用[feedparser parse]
时自动填充。现在的问题是,尽管我刷新了feed,但parsedItems.count总是返回零。有什么想法吗?
在我的app委托中,我有以下功能:
-(void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{
NewsTableViewController *viewController = [[NewsTableViewController alloc] init];
[viewController fetchNewDataWithCompletionHandler:^(UIBackgroundFetchResult result) {
completionHandler(result);
}];
}
在我的NewsTableViewController类中,我有以下功能:
-(void)fetchNewDataWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
NSArray *oldItems;
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSData *objectData = [defaults objectForKey:@"parsedItems"];
if(objectData != nil)
{
oldItems = [NSKeyedUnarchiver unarchiveObjectWithData:objectData];
oldItems = [oldItems sortedArrayUsingDescriptors:
[NSArray arrayWithObject:[[NSSortDescriptor alloc] initWithKey:@"date"
ascending:NO]]];
}
// Refresh data
[feedParser stopParsing];
[parsedItems removeAllObjects];
[feedParser parse];
NSLog(@"old:%lu", oldItems.count);
NSLog(@"new:%lu", parsedItems.count);
NSLog(@"ding:%lu", self.itemsToDisplay.count);
if (parsedItems.count == oldItems.count) {
completionHandler(UIBackgroundFetchResultNoData);
NSLog(@"No data.");
}
else {
[UIApplication sharedApplication].applicationIconBadgeNumber++;
[self sendNotification];
completionHandler(UIBackgroundFetchResultNewData);
[defaults setObject:[NSKeyedArchiver archivedDataWithRootObject:parsedItems] forKey:@"parsedItems"];
[defaults synchronize];
NSLog(@"New data was fetched.");
}
}