我尝试在iOS 10中实现后台提取和刷新。 我使用XML解析来解析数据,然后将其存储在文档目录中的文件中。为了解析XML,我使用自定义类(XMLParser)来确认 NSXMLParserDelegate 协议。
后台提取工作正常。但是,当我点击刷新按钮以及viewDidLoad时,我在显示刷新数据方面遇到了问题。
我在viewDidLoad中调用 refreshData 方法。 我已经走了多远。
AppDelegate.m
onDestroy
ArtsViewController.h
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
//--Set background fetch--//
[application setMinimumBackgroundFetchInterval:UIApplicationBackgroundFetchIntervalMinimum];
}
...
#pragma mark Background data fetch methods
-(void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{
NSDate *fetchStart = [NSDate date];
ArtsViewController *artsViewController = (ArtsViewController *)self.window.rootViewController;
[artsViewController fetchNewDataWithCompletionHandler:^(UIBackgroundFetchResult result) {
completionHandler(result);
NSDate *fetchEnd = [NSDate date];
NSTimeInterval timeElapsed = [fetchEnd timeIntervalSinceDate:fetchStart];
NSLog(@"Background Fetch Duration: %f seconds", timeElapsed);
}];
}
ArtsViewcontroller.m
@interface ArtsViewController : UIViewController <UIPageViewControllerDataSource>
@property BOOL newsAvailable;
-(void)fetchNewDataWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler; // No problems here
@end
我调试了应用程序,发现在viewDidLoad之后 完成执行,写入数据文件但视图不是 更新。我还尝试在main中调用 refreshData 方法 线程,但没有变化。 在viewDidLoad完成后,将调用 showNoNewNews 方法。
我怀疑我的逻辑没有错,但实施却是错误的。线程在这里发挥.. 任何帮助将不胜感激。
更新 希望这有助于那些有类似问题的人...... 我将
@interface ArtsViewController () @property (nonatomic, strong) NSArray *arrNewsData; -(void)refreshData; -(void)performNewFetchedDataActionsWithDataArray:(NSArray *)dataArray; @end ... @implementation ArtsViewController - (void)viewDidLoad { [super viewDidLoad]; [self refreshData]; //--Load the file that saves news--// [self loadNews]; if (_newsAvailable == YES) { [self setupPageViewController]; } else { [self showNoNewsMessage]; } } ... #pragma mark Data Fetch methods -(void)refreshData{ XMLParser *xmlParser = [[XMLParser alloc] initWithXMLURLString:ArtsNewsFeed]; [xmlParser startParsingWithCompletionHandler:^(BOOL success, NSArray *dataArray, NSError *error) { if (success) { [self performNewFetchedDataActionsWithDataArray:dataArray]; } else{ NSLog(@"%@", [error localizedDescription]); } }]; } -(void)performNewFetchedDataActionsWithDataArray:(NSArray *)dataArray{ // 1. Initialize the arrNewsData array with the parsed data array. if (self.arrNewsData != nil) { self.arrNewsData = nil; } self.arrNewsData = [[NSArray alloc] initWithArray:dataArray]; // 2. Write the file and reload the view. NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString * docDirectory = [paths objectAtIndex:0]; NSString * newsFilePath = [NSString stringWithFormat:@"%@",[docDirectory stringByAppendingPathComponent:@"arts2"]]; // NewsFile if (![self.arrNewsData writeToFile:newsFilePath atomically:YES]) { _newsAvailable = NO; NSLog(@"Couldn't save data."); } else { _newsAvailable = YES; NSLog(@"Saved data."); [self viewWillAppear:YES]; } } -(void)fetchNewDataWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{ XMLParser *xmlParser = [[XMLParser alloc] initWithXMLURLString:ArtsNewsFeed]; [xmlParser startParsingWithCompletionHandler:^(BOOL success, NSArray *dataArray, NSError *error) { if (success) { NSDictionary *latestDataDict = [dataArray objectAtIndex:0]; NSString *latestTitle = [latestDataDict objectForKey:@"title"]; NSDictionary *existingDataDict = [self.arrNewsData objectAtIndex:0]; NSString *existingTitle = [existingDataDict objectForKey:@"title"]; if ([latestTitle isEqualToString:existingTitle]) { completionHandler(UIBackgroundFetchResultNoData); NSLog(@"No new data found."); } else{ [self performNewFetchedDataActionsWithDataArray:dataArray]; completionHandler(UIBackgroundFetchResultNewData); NSLog(@"New data was fetched."); } } else{ completionHandler(UIBackgroundFetchResultFailed); NSLog(@"Failed to fetch new data."); } }]; } ... #pragma mark IBActions - (IBAction)reloadNews:(UIBarButtonItem *)sender { [self viewDidLoad]; }
的逻辑移到了另一种方法,这种方法在viewDidLoad
中首次称为方法,在viewDidLoad
中再次称为方法refreshData