我最近构建了一个加载一组图像的应用程序,必须像Tinder一样,用户可以浏览图像,但在我们获取图像列表和图像本身之前,我们需要:
- 确保网络已打开并连接。我使用的是可达性框架
- 我们还需要确保我们有一个位置设置。下面突出显示
这就是我在如何处理图像获取过程方面遇到的问题。目前,我正在从位置服务管理器中触发方法调用,并使用if操作包装调用,该操作检查_result数组中是否已经有结果,或者在我们正在处理的事件中lock
var是否设置为true。我觉得这是一个写得不好的过程,但我是ObjectiveC的新手,但我希望你们都能向我展示我应该如何编码它
目前,Reachability和Location Manager都在后台自我委派,并通过各自的事件启动,但我的api进程没有。那么,我该如何构建一个方法来观察定位服务值,比如lat和lon,再加上另一个值,让我们称之为hasInternet,设置好了,然后在它们设置好时启动?
- (void)viewDidLoad
{
[super viewDidLoad];
_lock = false;
self.results = [[NSMutableArray alloc] init];
// setup activity indicator
_activityIndicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
_activityIndicator.frame = CGRectMake(round((self.view.frame.size.width-25) / 2), round((self.view.frame.size.height-25) / 2), 25, 25);
[self.view addSubview:_activityIndicator];
[_activityIndicator startAnimating];
// setup navigation bar
UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,40,40)];
image.contentMode = UIViewContentModeScaleAspectFit;
[image setImage: [UIImage imageNamed:@"loading-logo1"]];
self.navigationItem.titleView = image;
self.navigationController.navigationBar.alpha = .01;
// setup location services
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
// Check for iOS 8. Without this guard the code will crash with "unknown selector" on iOS 7.
if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
[self.locationManager requestWhenInUseAuthorization];
}
}
/**
Location Manager Delegate Methods
**/
- (void)locationManager:(CLLocationManager*)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
switch (status) {
case kCLAuthorizationStatusNotDetermined: {
NSLog(@"User still thinking..");
} break;
case kCLAuthorizationStatusDenied: {
NSLog(@"User hates you");
// [self userNeedToApproveLocationServicesAlert];
} break;
case kCLAuthorizationStatusAuthorizedWhenInUse:
case kCLAuthorizationStatusAuthorizedAlways: {
[self.locationManager startUpdatingLocation]; //Will update location immediately
NSLog(@"User approved");
} break;
default:
break;
}
}
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
CLLocation *currentLocation = [locations lastObject];
self.lat = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude];
self.lon = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude];
if([_results count] == 0 && _lock == false)
{
[self fetchDataImages];
}
// [self.locationManager stopUpdatingLocation];
NSLog(@"%@%@",self.lat,self.lon);
}
/**
This just gets the json data and processes it into an object
**/
-(void)fetchDataImages {
//This should be fairly quick lol
_lock = true;
.....
好的,您有两个异步处理程序,可以随时调用。
只有当你们都有网络并且有一个有效的位置时,你们才想触发图像下载。
因此,添加实例变量haveNetwork
和haveValidLocation
。
设置两个处理程序方法以设置/清除相应的方法,然后编写一个方法downloadIfPossible
并从两个处理过程中调用它。只有当两个标志都为TRUE(并且下载尚未进行)时,才让downloadIfPossible
启动下载过程