XMLparsing后数据将不会加载
我是新来的iOS,我一直在尝试实现XMLparsing。 我使用由Gabriel Theodoropoulos提供的用于XMLparsing的自定义类(XMLParser)。 我正在尝试加载和保存应用程序启动时的数据。
数据也被parsing和保存,但是我试图加载保存的文件的视图之前加载,所以数据不被显示。 但是当我再次将视图推入视图时,数据是可见的。
从我所观察到的,AppDelegate中XMLParser类方法的完成发生在视图加载之后。 当我单击一个刷新button来执行相同的操作时,parsing和视图加载正常发生。
你能帮我吗?
这是我来了多远
AppDelegate.m
@interface AppDelegate () @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { [self initialNewsFetch]; return YES; } -(void) initialNewsFetch { //1. Main view|Home:- Most Read UIStoryboard * storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil]; RootViewController * mostRead = [storyboard instantiateViewControllerWithIdentifier:@"RootViewController"]; [mostRead refreshData]; [[NSNotificationCenter defaultCenter] postNotificationName:@"ArtsNewsAvailable" object:nil]; } @end
ViewController.m
-(void)refreshData{ XMLParser *xmlParser = [[XMLParser alloc] initWithXMLURLString:MostReadNewsFeed]; [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:@"mostRead"]]; // NewsFile if (![self.arrNewsData writeToFile:newsFilePath atomically:YES]) { _newsAvailable = NO; NSLog(@"Couldn't save data."); } else { _newsAvailable = YES; [self viewDidLoad]; //even tried calling this// } }