Skip to content
This repository has been archived by the owner on Dec 5, 2019. It is now read-only.

Data Context Interaction pattern on top of MVVM. #5

Closed
oleksii-demedetskyi opened this issue Aug 21, 2013 · 9 comments
Closed

Data Context Interaction pattern on top of MVVM. #5

oleksii-demedetskyi opened this issue Aug 21, 2013 · 9 comments
Labels

Comments

@oleksii-demedetskyi
Copy link

Hi, this rather a question or ask for advice, but definitely not an issue.

I spend some time today playing with architecture described in README.md
So at first some of my assumptions.

  1. View layer = View Controller + custom UIView + layouts and other view related stuff
  2. ViewModel = Subclass of some basic ViewModel class, with link to actual model, actual this is an adapter of model for View Controller consumption, values transforming. Other ViewModel responsibilities not clear for me. Who manages UINavigationController stack?
  3. Model = Some DTO class (like Mantle for example)

If all above assumptions are correct, then ViewModel can act as a Role in DCI pattern.
In this pattern Roles collected in Contexts, or in another words - Use cases.
Each Use case can also have some output signals or commands which will trigger start of another use cases.

And finally, Use cases can be connected with each other in some Workflow or Application entity.

This system will allow to test and isolate application not just by parts, but also by task, user stories, and other logical chunks.

Open questions.

  1. Common Shared state between ViewModels (network clients, core data contexts)
  2. Container View Controllers responsibility.
@jspahrsummers
Copy link
Member

Other ViewModel responsibilities not clear for me. Who manages UINavigationController stack?

In response to changes in the view model, the view layer will present or pop view controllers.

As a general rule of thumb, the view model should not use anything from UIKit. This isn't always possible, but it's a good guideline.

This system will allow to test and isolate application not just by parts, but also by task, user stories, and other logical chunks.

I'm not really familiar enough with DCI to comment on this.

  1. Common Shared state between ViewModels (network clients, core data contexts)
  2. Container View Controllers responsibility.

What exactly are your questions here?

@oleksii-demedetskyi
Copy link
Author

In response to changes in the view model, the view layer will present or pop view controllers.

For example. In app we have 2 screens. List of some items, and item details.
User select some row.

  1. Table transfer control to its delegate, (typically controller)
  2. Controller change some property of ViewModel ( selectedItem for example) (is it correct?)
  3. View Model react and .. what? Change another property? Fire some event to the signal?
  4. Some how control flow goes back to controller - now controller need to instantiate next controller, push it to navigation.. But for this action controller need to create another controller's ViewModel, and some how link them with actual model. All this stuff is out of view competence, so i am sure there is error in my thoughts.

1.Common Shared state between ViewModels (network clients, core data contexts)

How common dependency traverse between view models? From one to another? then how transitive dependency solved? I mean:

A -> B -> C, where {A require D1, B require D2, C require D1}

@jspahrsummers
Copy link
Member

  1. Controller change some property of ViewModel ( selectedItem for example) (is it correct?)

Yep.

  1. View Model react and .. what? Change another property? Fire some event to the signal?

Yes, exactly.

For example, the current view model may have a detailViewModel property. Changing that property will notify the VC that a new view controller should be pushed onto the navigation stack.

How common dependency traverse between view models? From one to another? then how transitive dependency solved?

This isn't a problem unique to VMs. Like you would do anywhere else, you can use dependency injection, or just pass to B the data that C will eventually need.

@oleksii-demedetskyi
Copy link
Author

Great Thanks for your time and explanations!
Now it is much more clear to me.

@jspahrsummers
Copy link
Member

No problem!

@fatuhoku
Copy link

How might one use ReactiveCocoa to pass data between ViewControllers?

I know that the typical way people do this is

  • pass data forward by
    • init-injecting data into next VC
    • setter-injecting data into the destinationViewController in prepareForSegue: when using storyboards
  • pass data backward by having the previous view controller implement a delegate

Can ReactiveCocoa be used to streamline the way data is passed to and from view controllers?

@kastiglione
Copy link
Member

Instead of having controllers communicate back via a delegate, declare a signal (or property) on the controller and have the invoking controller subscribe to that signal/property.

@bangerang
Copy link

I'm also interested in passing data between ViewControllers with ReactiveCocoa. Could you share som example code? Let's say we have a UITableViewController and a DetailViewController. If I tap a cell in the UITableViewController how can pass along data based on the tap to the DetailViewController?

@jspahrsummers
Copy link
Member

@bangerang You can create the next view controller by passing a view model to its initializer, or just set a property, like in @ashfurrow's C-41 project.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

5 participants