我正在做一个项目,我必须从两个单独的URL中获取数据。这些URL包含两个不同地方的天气json。我正在使用UISegmentedControl在两个位置之间切换并在UITableView中显示数据。我正在使用Core Data,因此获取了json并将数据存储在coredata中,然后我使用fetchedResultsController。
我的问题是,当我从显示第一个城市的天气数据的segment1更改为应显示第二个城市的数据的segment2时。它什么都不做,只显示从第一个URL获取的数据。
以下是代码的片段 -
这是我的分段控制IBAction
- (IBAction)segmentedControlAction:(UISegmentedControl *)sender {
//for Sydney Weather
if (sender.selectedSegmentIndex == 0) {
[self refreshDataWithURL:sydneyWeatherURL atLocation:@"Sydney"];
NSLog(@"SYDNEY");
}
//for Mosman weather
else if (sender.selectedSegmentIndex == 1) {
[self refreshDataWithURL:mosmanWeatherURL atLocation:@"Mosman"];
[self.tableView reloadData];
NSLog(@"MOSMAN");
}
}
这是上面代码中使用的refreshDataWithURL方法 -
- (void)refreshDataWithURL:(NSString *)stringURL atLocation:(NSString *)location {
self.managedObjectContext = [CoreDataManager sharedInstance].managedObjectContext;
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
NSURL *weatherURL = [NSURL URLWithString:stringURL];
GetWeatherData *getWeatherData = [[GetWeatherData alloc] initWithURL:weatherURL andContext:self.managedObjectContext];
NSLog(@"Fetching Data for %@", location);
[getWeatherData getWeather];
[self.tableView reloadData];
}
这是我的fetchedResultsController -
- (NSFetchedResultsController *) fetchedResultsController {
if (_fetchedResultsController != nil) {
return _fetchedResultsController;
}
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"DailyForecast" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entityDescription];
[fetchRequest setShouldRefreshRefetchedObjects:YES];
//Get the latest weather data
NSDate *currentdate = [NSDate date];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(date >= %@)", currentdate];
[fetchRequest setPredicate:predicate];
//order the result from newest to oldest
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"date" ascending:YES];
[fetchRequest setSortDescriptors:[NSArray arrayWithObject:sortDescriptor]];
[fetchRequest setFetchBatchSize:30];
NSLog(@"FETCH REQUEST --- %@", fetchRequest);
NSFetchedResultsController *theFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:@"date" cacheName:nil];
_fetchedResultsController.delegate = self;
self.fetchedResultsController = theFetchedResultsController;
[[[self fetchedResultsController] managedObjectContext] performBlock:^{
NSError *error = nil;
BOOL success = [theFetchedResultsController performFetch:&error];
if (!success) {
NSLog(@"Error: %@, Error Description: %@", error, [error userInfo]);
abort(); //Fail
} else {
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
[self.tableView reloadData];
}];
}
}];
return _fetchedResultsController;
}
还有其他frc委托方法。我在其他主题中读到应该有两个应该处理这两个URL的managedObjectContexts,但我不知道如何做到这一点。
这是GetWeatherData .m文件 -
- (void)getWeather {
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:_yahooApiURL cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:30.0];
NSURLResponse *urlResponse;
NSError *error;
//Fetch data from the URL
NSData *urlData = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&urlResponse error:&error];
UIAlertView *alertView;
if (urlData != nil) {
NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:urlData options:NSJSONReadingAllowFragments error:&error];
NSArray *jsonArray = [[[[[jsonDict objectForKey:@"query"] objectForKey:@"results"] objectForKey:@"channel"] objectForKey:@"item"] objectForKey:@"forecast"];
NSString *location = [[[[[jsonDict objectForKey:@"query"] objectForKey:@"results"] objectForKey:@"channel"] objectForKey:@"location"] objectForKey:@"city"];
for (NSDictionary *dictionary in jsonArray) {
[DailyForecast getForecastWithWeatherAPI:dictionary forLocation:location inManagedObjectContext:_managedObjectContext];
}
} else {
alertView = [[UIAlertView alloc] initWithTitle:@"Error" message:@"There is an error in connecting to the internet" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alertView show];
}
}
任何人都可以提供帮助,非常感谢您的帮助。
提前致谢。