Tony Lin
Tony Lin

Reputation: 942

how does MVVM with a child MVVM work in iOS?

Suppose I have ViewController A, its viewModel AVM. A has a containerView, which is ViewController B and its viewModel BVM. A has reference of B and AVM has a reference of BVM as its child View Model.

And I have a C, which is a manager class controls the behaviour of B, calling functions in B such as setSDImage(url) from B.

B has a UIImageView and has setSDImage(url) function with a callback. A has a button, when button pressed, it should trigger the setSDImage(url) for B.

So the problems is: Press button from A, goes to AVM, then AVM contacts C telling it to do some work to B, now, C has to get a reference of B since the function call is setSDImage(url) which is UI layer. Also, the callback needs to be implemented in B as well, that's why I don't know how to get this work in BVM rather than B.

So, questions in this case:

1, manager class C, controls behaviour of B. How does AVM contact C without passing reference of B? (since VM don't want to know about ViewControllers/views)

2, should manager class C controls B or BVM? I make it controls B instead of BVM because a) the set image function has to be done in UILayer which is B. b) the callback needs to be implemented in UILayer?

Upvotes: 1

Views: 519

Answers (1)

Dave Weston
Dave Weston

Reputation: 6635

It's a bit hard to understand what's going on just from your description, but I would say that a method called setSDImage which takes a URL doesn't belong in the view controller, since then the view controller needs to do the work of downloading the image data, converting it to a UIImage and then setting that on a UIImagView.

Downloading the image and converting it is work that should be initiated by BVM which then notifies B when the UIImage is ready.

So, to answer your second question, I think C should have a reference to BVM, which should be easy for AVM.

Does that make sense?

Upvotes: 1

Related Questions