2015-12-29 28 views
0

我最近建立和应用程序加载一组图像,必须像火种呢,并且用户可以通过图像翻转,但我们获取图像的列表之前,和图像本身,我们需要:如何建模应用程序体系结构以观察值?

  1. 确保网络已打开并连接。我为此使用了可达性框架。
  2. 我们还需要确保我们有一个位置集。以下突出显示。

这是我在如何处理图像抓取过程中遇到的问题。目前,我正在触发位置服务管理器中的方法调用,并且仅使用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; 
..... 
+1

这可能会被关闭,因为这是一个相当开放的问题,但我建议查看ReactiveCocoa。这需要花一点时间来围绕它,但它是用于这种用例。 –

回答

3

好吧,你有2个可以随时调用的异步处理程序。

你只想触发图像下载,如果你们都有网络并且有一个有效的位置。

因此添加实例变量haveNetworkhaveValidLocation

设置您的两个处理程序方法来设置/清除适当的方法,然后编写一个方法downloadIfPossible并从两个处理程序中调用它。 downloadIfPossible只有在两个标志都为TRUE时才开始下载过程(并且下载还没有进行中)

+0

所以,如果我正确理解你的答案,我不能创建一个方法或类,它只是监视一个实例变量,并在它变为true时作用于它。我需要从任何处理程序和所有处理程序中调用该方法,并决定是否满足所有要求? – LeviXC

+0

是的,你可以这样做。你可以使用KVO。在Xcode文档中搜索“KVC”以了解更多信息。 –