The best way to communicate between the ViewModel and the controller

I am new to development and have recently been involved in MVVM design. For communication between ViewModel and Controller I use Closure. I know I can use a delegate. But is there any agreement or reason why I should follow the message. I am a bit confused. Any help would be appreciated.

+6
source share
2 answers

I also searched for this answer, and I found this,

Passing a closure from the user interface layer (UIL) to the business logic layer (BLL) will result in a “Separate Problem” (SOC) break. The data you are preparing is in the BLL, so essentially you would say, "Hey, BLL, follow this UIL logic for me." This is SOC. (More info here https://en.wikipedia.org/wiki/Separation_of_concerns .)

BLL should only communicate with UIL through delegate notifications. So the BLL essentially says: "Hi UIL, I have finished executing my logic and here are some data arguments that you can use to control the user interface controls as you need."

Therefore, the UIL should never ask the BLL to execute the user interface control logic for it. Should only ask the BLL to notify him.

, .

MVVM iOS

+7

, . . . (, ), . , . , singleton DataManager - . .

, :

class DataManager
{
    static let sharedInstance = DataManager()

    var _value: Int = 0
    var value: Int
    {
        get
        {
            return _value
        }
        set
        {
            _value = newValue
        }
    }
}

class DemoController1
{
    let dm = DataManager.sharedInstance

    func incValue()
    {
        dm.value += 1
    }
}

class DemoController2
{
    let dm = DataManager.sharedInstance

    func mulValue()
    {
        dm.value *= 2
    }
}

let dm = DataManager.sharedInstance
let dc1 = DemoController1()
let dc2 = DemoController2()

print ("value: \(dm.value)")
dc1.incValue()
print ("value: \(dm.value)")
dc1.incValue()
print ("value: \(dm.value)")
dc2.mulValue()
print ("value: \(dm.value)")
0

All Articles