ReactiveCocoa简单实战 (一)

ReactiveCocoa简单实战

我厂广招各路大神加入:job.koudaitong.com
能够发简历到 tianchi@qima-inc.com O(∩_∩)O~git

前戏

今天从杭州回家错过了高铁,又改为了客车。原本很是懊恼的心情,看到文章被SF博客转发了,一会儿就行了起来。segmentfault

最近闲着也是为了下一个与TX的小伙伴合做的项目作准备,作了一个简单的APP。主要的功能就是设定一个目的地,在你快要到达目的地的时候给你提醒。对于我这种坐动车常作过站的人来讲,恩,时候是拯救本身一把了。框架

欲露还羞

项目的结构很简单,以下图所示:异步

Project

项目使用了MVVM的框架结构来替代MVC框架。storyboard中的内容以下:async

storyboard

第一个界面为保存的路线,选择以后直接进入出发页面,新增路线以后点击出发一样进入出发页面。在出发页面中点击changeBtn(就是那个没显示全的按钮,恩,偷懒了)能够修改提示音乐。出发页面中会计算当前位置与目的地的直线距离,以及估算的到达时间(如今APP里面好像算错了,正在改正)ide

正戏开始

首先,将高德地图的SDK配置好以后,来看看第一个页面--ChooseViewController的内容:atom

#import "ChooseViewController.h"
#import "ChooseViewModel.h"
#import <AMapSearchKit/AMapSearchObj.h>
#import "OnRoadViewController.h"
#import <PromiseKit/PromiseKit.h>

@interface ChooseViewController ()
@property (nonatomic,strong) ChooseViewModel *viewModel;
@property (nonatomic,strong) NSDictionary *targetInfoDic;
@end

@implementation ChooseViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    _viewModel = [ChooseViewModel new];
    self.tableView.delegate   = _viewModel;
    self.tableView.dataSource = _viewModel;

    [_viewModel.cellSelectedSignal subscribeNext:^(id x) {
        if (x) {
            //tiaozhuan
            _targetInfoDic = x;
            [self performSegueWithIdentifier:@"SelectToGo" sender:self];
        }
    }];

    [_viewModel.loadRoutesFromCache subscribeNext:^(id x) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
        });
    } error:^(NSError *error) {
        NSLog(@"%@",error.description);
    }];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    if ([segue.identifier isEqualToString:@"SelectToGo"]) {
        CGPoint location = [_targetInfoDic[@"target"] CGPointValue];
        NSDictionary *info = _targetInfoDic[@"target_info"];
        AMapPOI *targetPoi = [[AMapPOI alloc] init];
        AMapGeoPoint *mgp = [AMapGeoPoint locationWithLatitude:location.x longitude:location.y];
        targetPoi.location = mgp;
        targetPoi.name = info[@"name"];
        targetPoi.address = info[@"address"];
        targetPoi.type = info[@"type"];
        OnRoadViewController *onRoadVC = (OnRoadViewController *)[segue destinationViewController];
        onRoadVC.targetPoi = targetPoi;
    }
}
@end

很简单的一段代码。在匿名类别中添加了两个对象,一个就是ViewModel,另外一个用来储存目的地的经纬座标的字典。spa

viewDidLoad中,咱们将tableview的delegate与dataSource都设为了刚刚new出来的viewModel。而且咱们分别对viewModel中的cellSelectedSignal信号与loadRoutesFromCache信号绑定了next的处理事件。线程

那么咱们就来看看这两个事件究竟是怎么起做用的吧。在ChooseViewModel中,这两个信号分别是这样子的:code

-(RACSignal *)loadRoutesFromCache;

@property (nonatomic,strong) RACSubject *cellSelectedSignal;

在对象被new的时候,咱们实例化了cellSelectedSignal信号:

+(id)new{
    ChooseViewModel *model = [super new];
    model.cellSelectedSignal = [RACSubject subject];
    return model;
}

(不知道这样写有没有问题)它一个RACSubject对象,subject的特色就是信号的发送是可控的。下面就来看看这个信号是怎么可控的吧:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [self.cellSelectedSignal sendNext:self.waysDic[self.routesArr[indexPath.row]]];
}

咱们使用了sendNext:方法将waysDic中的路径信息经过信号发送了出去。在ChooseViewController中注册的next处理事件便会获得执行。在其中,咱们储存了目的地信息吼将页面push到下一个页面,在push以前咱们构建了一个poi对象(高德SDK),并传递了过去。

接下来看看那个从文件中读取内容的信号吧(loadRoutesFromCache)。

-(RACSignal *)loadRoutesFromCache{
    RACSubject *subject = [RACSubject subject];
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        NSData *data = [NSData dataWithContentsOfFile:kWaysSavePath];
        NSMutableDictionary *waysDic = [NSKeyedUnarchiver unarchiveObjectWithData:data];
        if(waysDic!=nil && [waysDic allKeys].count>0){
            self.waysDic = waysDic;
            self.routesArr = [waysDic allKeys];
            [subject sendNext:_routesArr];
            [subject sendCompleted];
        }else{
            [subject sendError:[NSError errorWithDomain:@"rb.loadroutes" code:0 userInfo:@{@"error": @"未能加载"}]];
        };
    });
    return subject;
}

这个信号被表示成了一个方法,这个方法返回了一个Subject对象。在异步执行完成后,向这个Subject对象发送next与completed信号,若是内容不存在则发送error信号。VC中(实际上是view)中的next处理事件在收到信号后回到主线程刷新tableview;error则在收到错误信号后……呃……打印了那个错误……

这么快就没了

恩……男人是否是不能这么快……好吧,此次先写到这儿,好困啊,明天项目正式动工,先早点休息了。

下次为你们继续新建路线中的内容。(其实和这个用法大同小异)

代码写的到不到的还请多多包含,最好能帮忙指出错误!小弟谢过了。

相关文章
相关标签/搜索