Can we do get and didSet together with lazy keyword. I have an array it fetch data from database (get) and another thing when that array will modify i will call delegate to reload data in didSet.
Its not working and not complain of any error.
class CoreDataManager {
func fetchUsers() -> [UserEntity] {
var users: [UserEntity] = []
do {
users = try! context.fetch(UserEntity.fetchRequest())
}catch {
print("Fetch user Error,", error)
}
return users
}
}
protocol UserControllerToViewModel: AnyObject {
func reloadData()
}
class UserViewModel {
private var manager = CoreDataManager()
weak var delegate: UserControllerToViewModel?
// Here I want to get users and didset together with lazy keyword but its not working
lazy var users: [UserEntity] = manager.fetchUsers() {
didSet {
delegate?.reloadData()
}
}
}
Demo GitHub Project link: https://github.com/YogeshPateliOS/TableView_MVVM.git
As @EmilioPelaez said, the first time you access the lazy property you will be initialising it so the
didSetwon't be called.This can be demonstrated by the following simplification of your example code:
This gives the output:
demonstrating that the
didSetis only called when the property is being modified and not when being initialised.EDIT:
To demonstrate that where the method that initialises the lazy var exists is irrelevant. Replace the above code with the below and the output is exactly the same;
didSetwon't be run on the first initialisation.