title: ReactiveCocoa MVVM with UITableView
body: <p>I'm using ReactiveCocoa and am trying to apply MVVM. I have a fairly typical UITableView scenario with a refresh control for reloading data.</p>
  
  <p>I've omitted the the UITableDataSource/Delegate methods as these are straight forward. The code below illustrates how I've designed the ViewModel and the ViewController to fit together.</p>
  
  <p><strong>ViewModel.h</strong></p>
  
  <pre><code>@property (strong, nonatomic, readonly) RACCommand *getItemsCommand;
  @property (strong, nonatomic, readonly) NSArray *items;
  </code></pre>
  
  <p><strong>ViewModel.m</strong></p>
  
  <pre><code>- (instancetype)init {
      self = [super init];
      if (!self) return nil;
  
      @weakify(self);
      self.getItemsCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
          return [[ItemsDataSource getItems]
                  doNext:^(NSArray *items) {
                      @strongify(self);
                      // I actually do a little extra work here such as sorting
                      // the items appropriately for the view.
                      self.items = items;
                  }];
      }];
  
      return self;
  }
  </code></pre>
  
  <p><strong>ViewController.m</strong></p>
  
  <pre><code>- (void)viewDidLoad {
      [super viewDidLoad];
  
      [self.tableView addSubview:self.refreshControl];
  
      RACSignal *refreshSignals = [RACSignal merge:@[
          [self.refreshControl rac_signalForControlEvents:UIControlEventValueChanged],
          [RACSignal return:nil]
      ]];
  
      [refreshSignals
       subscribeNext:^(id x) {
           [self.viewModel.getItemsCommand execute:nil];
       }];
  
      [RACObserve(self.viewModel, items)
       subscribeNext:^(NSArray *items) {
           [self.tableView reloadData];
       } completed:^{
           [self.refreshControl endRefreshing];
       }];
  }
  </code></pre>
  
  <p><strong>Questions/Problems</strong></p>
  
  <ol>
  <li>The <code>completed</code> block where I call <code>endRefreshing</code> never gets executed and for the life of me I can't figure out why.</li>
  <li>Would it be better to use a public method <code>- (RACSignal *)getItems</code> instead of the <code>getItems</code> RACCommand?</li>
  <li>Is my usage of <code>doNext:</code> in the ViewModel correct in order to apply side effects (i.e. the sorting of the items array) without causing an additional subscription?</li>
  </ol>