3

There are plenty of resources on the web regarding the use of Reactive Cocoa for the purpose of "pulling" data from the web via API, etc.

I like the MVVM approach, and am trying to figure out what a good practice would be for sending updates via API when local objects change.

I suspect this is a problem others have solved elegantly. I am thinking that one good approach would be to observe a local array of objects, and make API calls appropriately when data changes. But care would need to be taken to only 'react' when the changes made to the local data objects have been made by the user, and not the server.

Are there existing resources that could point me in the right direction?

Michael Bopp
  • 656
  • 1
  • 4
  • 13

1 Answers1

0

Here's a fantastic book by Andre Staltz that explains a lot of reactive's design patterns, and gives some pretty solid examples: The introduction to Reactive Programming you've been missing.

RxJS and ReactiveCocoa are both based on Microsoft's RxObservable framework, so their functions have practically the same name, most of what you learn from that book is transferrable to any library based on RxObservable.

In regards to your question about sending updates when objects change, one (very simplistic) way to do it in RAC would be like this:

@weakify(self)
[[[[self rac_signalForSelector:@selector(addObjectToArrayBecauseTheUserWantsTo:)]
 mapReplace:self.arrayOfObjects]
flattenMap:^RACStream *(NSArray * objects) {
    @strongify(self)
    return [self.model sendObjectsToServerAndRetrieveResponse:objects];
}] subscribeNext:^(id response) {
    NSLog(@"Response from server: %@", response);
}];
Charles Maria
  • 2,165
  • 15
  • 15