ios - 如何为应用程序架构建模以观察值(value)?

标签 ios objective-c delegates

我最近构建了一个加载一组图像的应用程序,必须像 Tinder 一样,并且用户可以翻阅图像,但在我们获取图像列表和图像本身之前,我们需要:

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

这是我在处理图像获取过程时遇到的问题。目前我正在从位置服务管理器中触发方法调用,并且只是用一个 if 操作包装调用,该操作检查我们是否已经在 _result 数组中有结果或者 lock var 是否是在我们正在处理的事件中设置为 true。我觉得这个过程写得不好,但我是 ObjectiveC 的新手,但我希望你们都能告诉我应该如何编码。

目前,Reachability 和 Location Manager 都在后台自行委托(delegate)并在各自的事件发生时触发,但我的 api 进程没有。那么我如何构造一个方法来观察位置服务值,如纬度和经度,加上另一个值,让我们称之为 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;
.....

最佳答案

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

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

因此添加实例变量haveNetworkhaveValidLocation

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

关于ios - 如何为应用程序架构建模以观察值(value)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34516624/

相关文章:

iphone - 如何更改uitableview删除按钮文字

iphone - 从 MainViewController 传递和保存 Flip ViewController 之间的设置

objective-c - 从 Objective-C++ 调用 swift 函数

ios - 如何创建类似 UITableView 结构的树?

ios - 将 [NSDate date] 转换为本地化的 UTC 日期字符串

c# - 将额外参数传递给委托(delegate)

ios - 在 didSelectrow uitableview 中调用多个 View Controller

ios - 将所选图像放入网格

ios - 为什么我没有收到任何加速度计更新?

ios - 将配置图像解析为 UIImageView