Coder Social home page Coder Social logo

Comments (5)

zdnk avatar zdnk commented on May 22, 2024 1

Ok.

What I feel when looking at ReactorKit is that I am somehow missing the ViewModel. Reactor as by name already reminds of something that is yet behind the view model and controls all the tasks like networks requests/services etc.

from reactorkit.

devxoul avatar devxoul commented on May 22, 2024

Global state management is not a ReactorKit's responsibility. So you can use anything to manage the synchronisation. For example, you can use a global Subject to store synchronisation process or you can even use Reactor as a global synchronisation state manager. The important thing is that every Reactor can observe the global state and update its state.

Let's assume that we have a sync state as a Variable.

SyncState.swift

struct SynchronisationState {
  var usernames: [String] // example properties that will be binded to table view
}
let syncState = Variable<SynchronisationState>(SynchronisationState(usernames: []))

AppDelegate.swift

func applicationDidBecomeActive(_ application: UIApplication) {
  Synchroniser.sync() { newState in
    syncState.value = newState // emit a synchronised state
  }
}

MyViewReactor.swift

class MyViewReactor: Reactor {
  enum Action { ... }
  enum Mutation {
    case setUsernames(String)
  }
  struct State {
    var usernames: [String]
  }

  // Merges two observables into a single observabe:
  //   1. Mutation observable from Reactor
  //   2. Mutation observable from global `syncState`
  func transform(mutation: Observable<Mutation>) -> Observable<Mutation> {
    let fromSyncState = syncState.asObservable()
      .map { $0.usernames }
      .map { Mutation.setUsernames($0) }
    return Observable.merge(mutation, fromSyncState)
  }

  func reduce(state: State, mutation: Mutation) -> State {
    switch mutation {
    case let .setUsernames(usernames):
      var newState = state
      newState.usernames = usernames
      return newState
    }
  }
}

from reactorkit.

zdnk avatar zdnk commented on May 22, 2024

I see. So I should also transform and map the data as they are supposed to go to view? Does Reactor also poses as ViewModel then?

What about transitioning between data? Like UITableViews beginUpdate, insertRows etc?

from reactorkit.

devxoul avatar devxoul commented on May 22, 2024

@zdenektopic, You can directly map the global state to the View but it's recommended to use Reactor for testability. It's true that a Reactor it similar to a ViewModel because ReactorKit was originally started from MVVM.

If you're using RxDataSources you just can use RxTableViewSectionedAnimatedDataSource or RxCollectionViewSectionedAnimatedDataSource for animated updates. Otherwise you can use beginUpdate() and endUpdate().

from reactorkit.

devxoul avatar devxoul commented on May 22, 2024

Please reopen it if you have further questions :)

from reactorkit.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.