我最近建立和应用程序加载一组图像,必须像火种呢,并且用户可以通过图像翻转,但我们获取图像的列表之前,和图像本身,我们需要:如何建模应用程序体系结构以观察值?
- 确保网络已打开并连接。我为此使用了可达性框架。
- 我们还需要确保我们有一个位置集。以下突出显示。
这是我在如何处理图像抓取过程中遇到的问题。目前,我正在触发位置服务管理器中的方法调用,并且仅使用if操作来包装调用,以检查_result数组中是否已经有结果,或者如果lock
var被设置为true,正在处理。我觉得这是写得不好的过程,但我是ObjectiveC新手,但我希望你们都能告诉我应该如何编码它。
目前,可达性和位置管理器都在后台进行自我委派,并发生各自的事件,但我的api进程没有。那么,我该如何构建一个观察位置服务值的方法,如lat和lon,再加上另一个值,让它称为Internet,设置并在它们做什么时触发。
- (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;
.....
这可能会被关闭,因为这是一个相当开放的问题,但我建议查看ReactiveCocoa。这需要花一点时间来围绕它,但它是用于这种用例。 –