Service Manager not populating ViewModel - ios

hello developer right now I learn mvvm. I want to populate the data in my viewModel and I create my service as a singleton. but somehow it didn't work, in my uilabel. instead my uilabel text is disappear, I didn't know if this my setup viewModel is wrong or not. should I separate the model, since I have two model object inside my view model. here I show you my code.
class ProfileViewModel {
private var infos: InfoResult?
private var cities = [City]()
private var religions = [Religion]()
private let services: BasicInfoServices
var profileID: String {
return infos?.id ?? ""
}
var imageURL: String {
let imageUrl = infos?.docAwsUrl ?? ""
return imageUrl
}
var fullName: String {
return infos?.fullName ?? ""
}
var phoneNumber: String {
return infos?.phoneNumber ?? ""
}
var email: String {
return infos?.email ?? ""
}
var cityOfBirth: String {
var cityName = ""
cities.forEach { city in
if infos?.pobId == city.id {
cityName = city.name ?? ""
}
}
return cityName
}
var dateOfBirth: String {
return infos?.dob ?? ""
}
var religion: String {
var religionName = ""
religions.forEach { religion in
if infos?.religionId == religion.id {
religionName = religion.name
}
}
return religionName
}
init(services: BasicInfoServices) {
self.services = services
populateProfile()
}
}
extension ProfileViewModel {
func populateProfile() {
// Basic Info
self.services.getBasicInfo { [weak self] result in
switch result {
case .success(let profile):
self?.infos = profile
case .failure(let error):
print(error)
}
}
// City
self.services.getCity { [weak self] result in
switch result {
case .success(let cities):
self?.cities = cities
case .failure(let error):
print(error)
}
}
// Religion
self.services.getReligion { [weak self] result in
switch result {
case .success(let religions):
self?.religions = religions
case .failure(let error):
print(error)
}
}
}
}
// I initialise it in viewController
var viewModel: ProfileViewModel!
var services = BasicInfoServices()
// than I test it in viewDidLoad
viewModel = ProfileViewModel(services: services)
profileLbl.text = viewModel.fullName // when set this my profileLbl place holder disappear.
profileImage.getUserImage(urlString: viewModel.imageURL)

1) At this line, you make API requests so this is an async process
viewModel = ProfileViewModel(services: services)
2) Without waiting for the success response, you try to use response data in next line
profileLbl.text = viewModel.fullName
Tips for you
1) You should use closures to detect API responses.
class ProfileViewModel {
var info: InfoResult?
private let services: BasicInfoServices
init(services: BasicInfoServices) {
self.services = services
}
func loadData(success: (()->()), failure: ((String)->())){
self.services.getBasicInfo { [weak self] result in
switch result {
case .success(let infoResult):
self?.info = infoResult
success()
case .failure(let error):
print(error)
failure(error.localizedDescription)
}
}
}
}
2) After receiving data, you can show this on the view.
class ViewController: UIViewController {
let viewModel = ProfileViewModel(services: BasicInfoServices())
#IBOutlet weak var lblProfileID:UILabel!
#IBOutlet weak var lblFullName:UILabel!
#IBOutlet weak var lblPhoneNumber:UILabel!
#IBOutlet weak var lblEmail:UILabel!
#IBOutlet weak var lblDataOfBirth:UILabel!
override func viewDidLoad() {
super.viewDidLoad()
self.viewModel.loadData(success: {
self.populateUserData()
}, failure: { errorString in
print(errorString)
})
}
func populateUserData(){
self.lblProfileID.text = self.viewModel.info?.id
self.lblFullName.text = self.viewModel.info?.fullName
self.lblPhoneNumber.text = self.viewModel.info?.phoneNumber
self.lblEmail.text = self.viewModel.info?.email
self.lblDataOfBirth.text = self.viewModel.info?.dob
}
}

Related

Swift - How do I decode json from a REST API

I am trying to make a GET from a REST API in swift. When I use the print statement (print(clubs)) I see the expected response in the proper format. But in the VC is gives me an empty array.
Here is the code to talk to the API
extension ClubAPI {
public enum ClubError: Error {
case unknown(message: String)
}
func getClubs(completion: #escaping ((Result<[Club], ClubError>) -> Void)) {
let baseURL = self.configuration.baseURL
let endPoint = baseURL.appendingPathComponent("/club")
print(endPoint)
API.shared.httpClient.get(endPoint) { (result) in
switch result {
case .success(let response):
let clubs = (try? JSONDecoder().decode([Club].self, from: response.data)) ?? []
print(clubs)
completion(.success(clubs))
case .failure(let error):
completion(.failure(.unknown(message: error.localizedDescription)))
}
}
}
}
and here is the code in the VC
private class ClubViewModel {
#Published private(set) var clubs = [Club]()
#Published private(set) var error: String?
func refresh() {
ClubAPI.shared.getClubs { (result) in
switch result {
case .success(let club):
print("We have \(club.count)")
self.clubs = club
print("we have \(club.count)")
case .failure(let error):
self.error = error.localizedDescription
}
}
}
}
and here is the view controller code (Before the extension)
class ClubViewController: UIViewController {
private var clubs = [Club]()
private var subscriptions = Set<AnyCancellable>()
private lazy var dataSource = makeDataSource()
enum Section {
case main
}
private var errorMessage: String? {
didSet {
}
}
private let viewModel = ClubViewModel()
#IBOutlet private weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
self.subscriptions = [
self.viewModel.$clubs.assign(to: \.clubs, on: self),
self.viewModel.$error.assign(to: \.errorMessage, on: self)
]
applySnapshot(animatingDifferences: false)
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.viewModel.refresh()
}
}
extension ClubViewController {
typealias DataSource = UITableViewDiffableDataSource<Section, Club>
typealias Snapshot = NSDiffableDataSourceSnapshot<Section, Club>
func applySnapshot(animatingDifferences: Bool = true) {
// Create a snapshot object.
var snapshot = Snapshot()
// Add the section
snapshot.appendSections([.main])
// Add the player array
snapshot.appendItems(clubs)
print(clubs.count)
// Tell the dataSource about the latest snapshot so it can update and animate.
dataSource.apply(snapshot, animatingDifferences: animatingDifferences)
}
func makeDataSource() -> DataSource {
let dataSource = DataSource(tableView: tableView) { (tableView, indexPath, club) -> UITableViewCell? in
let cell = tableView.dequeueReusableCell(withIdentifier: "ClubCell", for: indexPath)
let club = self.clubs[indexPath.row]
print("The name is \(club.name)")
cell.textLabel?.text = club.name
return cell
}
return dataSource
}
}
You need to apply a new snapshot to your table view once you have fetched the clubs. Your current subscriber simply assigns a value to clubs and nothing more.
You can use a sink subscriber to assign the new clubs value and then call applySnapshot. You need to ensure that this happens on the main queue, so you can use receive(on:).
self.subscriptions = [
self.viewModel.$clubs.receive(on: RunLoop.main).sink { clubs in
self.clubs = clubs
self.applySnapshot()
},
self.viewModel.$error.assign(to: \.errorMessage, on: self)
]

Escaping closure captures mutating 'self' parameter, Firebase

I have the following code, How can i accomplish this without changing struct into class. Escaping closure captures mutating 'self' parameter,
struct RegisterView:View {
var names = [String]()
private func LoadPerson(){
FirebaseManager.fetchNames(success:{(person) in
guard let name = person.name else {return}
self.names = name //here is the error
}){(error) in
print("Error: \(error)")
}
init(){
LoadPerson()
}a
var body:some View{
//ui code
}
}
Firebasemanager.swift
struct FirebaseManager {
func fetchPerson(
success: #escaping (Person) -> (),
failure: #escaping (String) -> ()
) {
Database.database().reference().child("Person")
.observe(.value, with: { (snapshot) in
if let dictionary = snapshot.value as? [String: Any] {
success(Person(dictionary: dictionary))
}
}) { (error) in
failure(error.localizedDescription)
}
}
}
SwiftUI view can be created (recreated) / copied many times during rendering cycle, so View.init is not appropriate place to load some external data. Use instead dedicated view model class and load explicitly only when needed.
Like
class RegisterViewModel: ObservableObject {
#Published var names = [String]()
func loadPerson() {
// probably it also worth checking if person has already loaded
// guard names.isEmpty else { return }
FirebaseManager.fetchNames(success:{(person) in
guard let name = person.name else {return}
DispatchQueue.main.async {
self.names = [name]
}
}){(error) in
print("Error: \(error)")
}
}
struct RegisterView: View {
// in SwiftUI 1.0 it is better to inject view model from outside
// to avoid possible recreation of vm just on parent view refresh
#ObservedObject var vm: RegisterViewModel
// #StateObject var vm = RegisterViewModel() // << only SwiftUI 2.0
var body:some View{
Some_Sub_View()
.onAppear {
self.vm.loadPerson()
}
}
}
Make the names property #State variable.
struct RegisterView: View {
#State var names = [String]()
private func LoadPerson(){
FirebaseManager.fetchNames(success: { person in
guard let name = person.name else { return }
DispatchQueue.main.async {
self.names = [name]
}
}){(error) in
print("Error: \(error)")
}
}
//...
}

UI not updating in viewController MVVM

I'm learning MVVM in swift. I setup the viewModel right but the ui is not updating, what went wrong actually I made the services as a singleton service parameter? this my code setup
class UserViewModel {
private var user: GTUser?
let service: UserService
var id: String {
return user?.userId ?? ""
}
var userName: String {
return user?.fullName ?? ""
}
var imageUrl: String {
return user?.docAwsUrl ?? ""
}
init(service: UserService) {
self.service = service
populateUser()
}
private func populateUser() {
service.getUserData { result in
switch result {
case .success(let user):
self.user = user
print(self.user)
case .failure(let error):
print(error)
}
}
}
}
let services = UserService()
var viewModel: UserViewModel!
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
setupNavigation()
configure()
viewModel = UserViewModel(service: services)
DispatchQueue.main.async {
self.profileImage.getUserImage(urlString: self.viewModel.imageUrl)
self.profileLbl.text = self.viewModel.userName
}
}
I already try using dispatchQueue but still not working
Your private func populateUser() function is Async function so when you try to access self.profileImage.getUserImage(urlString: self.viewModel.imageUrl) from viewDidLoad it might not be available,
So to fix this you can have a completion handler closure into function and on success and failure call the completion handler so after you can perform the operation
like
your init func will take a closure
init(service: UserService,completion:(Bool) -> ()) {
self.service = service
populateUser(completion)
}
and
private func populateUser(_ completion:(Bool) -> ()) {
service.getUserData { result in
switch result {
case .success(let user):
self.user = user
print(self.user)
completion(true)
case .failure(let error):
print(error)
completion(false)
}
}
}
and Now
viewModel = UserViewModel(service: services){[unowned self] (success) in
//Check success if you want !!
self.profileImage.getUserImage(urlString: self.viewModel.imageUrl)
self.profileLbl.text = self.viewModel.userName
}
Hope it is helpful

Making Api call on View DidLoad with MVVM swift

I have an application where I want to make an API call once the screen is awaken in the ViewController. Basically, I am using Universal Link to activate the ViewCOntroller and when it displays the UIViewController, I want to make an API call based on the Data got. I am currently using the MVVM Architecture and I have added my code below
My ViewModel
class EmailVerificationViewModel: ViewModel, ViewModelType {
struct Input {
let editEmailTrigger: Driver<Void>
}
struct Output {
}
let routeManager: BehaviorRelay<RouteMatchResult?>
let currentEmail: BehaviorRelay<String?>
init(routeManager: RouteMatchResult?, provider: Api, currentEmail: String?) {
self.routeManager = BehaviorRelay(value: routeManager)
self.currentEmail = BehaviorRelay(value: currentEmail)
super.init(provider: provider)
}
func transform(input: Input) -> Output {
// THE CALL I WANT TO MAKE
routeManager.errorOnNil().asObservable()
.flatMapLatest { (code) -> Observable<RxSwift.Event<User>> in
log("=========++++++++++++==========")
// guard let code = code else {return}
let params = code.values
let challengeId = Int(params["xxx"] as? String ?? "0")
let login = LoginResponseModel(identifier: params["xxxx"] as? String, key: params["xxxxxx"] as? String, oth: params["xxxxx"] as? String, id: 0, challengeId: challengeId)
return self.provider.postVerifyApp(challengeId: login.challengeId!, oth: login.oth!, identifier: login.identifier!)
.trackActivity(self.loading)
.trackError(self.error)
.materialize()
}.subscribe(onNext: { [weak self] (event) in
switch event {
case .next(let token):
log(token)
AuthManager.setToken(token: token)
// self?.tokenSaved.onNext(())
case .error(let error):
log(error.localizedDescription)
default: break
}
}).disposed(by: rx.disposeBag)
return Output()
}
}
My Viewcontroller
override func bindViewModel() {
super.bindViewModel()
guard let viewModel = viewModel as? EmailVerificationViewModel else { return }
let input = EmailVerificationViewModel.Input(editEmailTrigger: editEmailBtn.rx.tap.asDriver())
let output = viewModel.transform(input: input)
viewModel.loading.asObservable().bind(to: isLoading).disposed(by: rx.disposeBag)
viewModel.parsedError.asObservable().bind(to: error).disposed(by: rx.disposeBag)
isLoading.asDriver().drive(onNext: { [weak self] (isLoading) in
isLoading ? self?.startAnimating() : self?.stopAnimating()
}).disposed(by: rx.disposeBag)
error.subscribe(onNext: { [weak self] (error) in
var title = ""
var description = ""
let image = R.image.icon_toast_warning()
switch error {
case .serverError(let response):
title = response.message ?? ""
}
self?.view.makeToast(description, title: title, image: image)
}).disposed(by: rx.disposeBag)
}
so how can I make the call on the commented like THE CALL I WANT TO MAKE once the application catches the universal link and loads up. Basically making an API call on viewDidLoad
The code in your sample was way more than is needed to answer the question. Here is how you make a network call on viewDidLoad:
class ViewController: UIViewController {
var viewModel: ViewModel!
private let disposeBag = DisposeBag()
override func viewDidLoad() {
super.viewDidLoad()
let input = ViewModel.Input()
let output = viewModel.transform(input: input)
output.viewData
.bind(onNext: { viewData in
// setup the view with viewData
})
.disposed(by: disposeBag)
}
}
class ViewModel {
struct Input { }
struct Output {
let viewData: Observable<ViewData>
}
init(api: API) {
self.api = api
}
func transform(input: Input) -> Output {
let viewData = api.networkCall()
.map { ViewData(from: $0) }
return Output(viewData: viewData)
}
let api: API
}

Model for MVVM in iOS

Here's a User model class. This model will be container for data while registering new user, logging an already registered user and displaying profile.
struct User {
typealias message = (Bool,String)
var name: String?
var username: String
var password: String
var image: String?
func isValidForLogin() -> message {
let emailMessage = isValidEmail(testStr: username)
let passwordMessage = isValidPassowrd(testStr: password)
if emailMessage.0 && passwordMessage.0 {
return (true,"Valid")
}
if !emailMessage.0{
return (emailMessage.0, emailMessage.1)
}else{
return (passwordMessage.0, passwordMessage.1)
}
}
func isValidForRegister() -> message {
if let name = self.name{
let nameMessage = isValidName(testStr: name)
let emailMessage = isValidEmail(testStr: username)
let passwordMessage = isValidPassowrd(testStr: password)
if emailMessage.0 && passwordMessage.0 && nameMessage.0{
return (true,"Valid")
}
if !emailMessage.0{
return (emailMessage.0, emailMessage.1)
}else if !passwordMessage.0{
return (passwordMessage.0, passwordMessage.1)
}else{
return (nameMessage.0, nameMessage.1)
}
}
return (false, "Name " + Constants.emptyField)
}
private func isValidName(testStr: String) -> message{
if testStr.isEmpty{
return (false, "Name " + Constants.emptyField )
}
return (true, "Valid")
}
private func isValidPassowrd(testStr: String) -> (Bool, String) {
if testStr.isEmpty{
return (false, "Password " + Constants.emptyField )
}
if testStr.count > 6{
return (true, "Valid")
}
return (false, Constants.invalidPassword)
}
private func isValidEmail(testStr: String) -> message {
if testStr.isEmpty{
return (false, "Email " + Constants.emptyField)
}
let emailRegEx = "^(?:(?:(?:(?: )*(?:(?:(?:\\t| )*\\r\\n)?(?:\\t| )+))+(?: )*)|(?: )+)?(?:(?:(?:[-A-Za-z0-9!#$%&’*+/=?^_'{|}~]+(?:\\.[-A-Za-z0-9!#$%&’*+/=?^_'{|}~]+)*)|(?:\"(?:(?:(?:(?: )*(?:(?:[!#-Z^-~]|\\[|\\])|(?:\\\\(?:\\t|[ -~]))))+(?: )*)|(?: )+)\"))(?:#)(?:(?:(?:[A-Za-z0-9](?:[-A-Za-z0-9]{0,61}[A-Za-z0-9])?)(?:\\.[A-Za-z0-9](?:[-A-Za-z0-9]{0,61}[A-Za-z0-9])?)*)|(?:\\[(?:(?:(?:(?:(?:[0-9]|(?:[1-9][0-9])|(?:1[0-9][0-9])|(?:2[0-4][0-9])|(?:25[0-5]))\\.){3}(?:[0-9]|(?:[1-9][0-9])|(?:1[0-9][0-9])|(?:2[0-4][0-9])|(?:25[0-5]))))|(?:(?:(?: )*[!-Z^-~])*(?: )*)|(?:[Vv][0-9A-Fa-f]+\\.[-A-Za-z0-9._~!$&'()*+,;=:]+))\\])))(?:(?:(?:(?: )*(?:(?:(?:\\t| )*\\r\\n)?(?:\\t| )+))+(?: )*)|(?: )+)?$"
let emailTest = NSPredicate(format:"SELF MATCHES %#", emailRegEx)
let result = emailTest.evaluate(with: testStr)
if result{
return (result, "Valid")
}else{
return (result, Constants.invalidEmail)
}
}
}
I am trying to follow MVVM pattern. So, my ViewModel class for RegisterViewViewModel:
struct RegisterViewModel {
private let minUserNameLength = 4
private let minPasswordLength = 6
var name: String
var email: String
var password: String
private var userModel: User{
return User(name: name, username: email, password: password, image: "")
}
func isValid() -> (Bool, String) {
return userModel.isValidForRegister()
}
func register(){
....
}
}
And in my RegisterViewController :
class RegisterViewController: UIViewController{
#IBOutlet weak var txtName: UITextField!
#IBOutlet weak var txtUsername: UITextField!
#IBOutlet weak var txtPassword: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func btnSignUpPressed(_ sender: UIButton) {
if let name = txtName.text, let email = txtUsername.text, let password = txtPassword.text{
let userModel = RegisterViewModel(name: name, email: email, password: password)
let validate = userModel.isValid()
if validate.0{
userModel.register()
}else{
//do error handling here
print(validate.1)
}
}
}
}
Am I going in right direction? Any suggestion will be appreciated.
I would recommend you to use RxSwift with MVVM. Also you could export validation to a separate ValidationService class. Otherwise you will probably have to copy same validation methods between different models.
enum ValidationResult {
case ok
case empty
case validating
case failed(message: String)
}
extension ValidationResult {
var isValid: Bool {
switch self {
case .ok:
return true
default:
return false
}
}
var isEmpty: Bool {
switch self {
case .empty:
return true
default:
return false
}
}
}
class ValidationService {
let minPasswordCount = 4
static let shared = ValidationService()
func validateName(_ name: String) -> Observable<ValidationResult> {
if name.isEmpty {
return .just(.empty)
}
if name.rangeOfCharacter(from: CharacterSet.decimalDigits) != nil {
return .just(.failed(message: "Invalid name"))
}
return .just(.ok)
}
}
What you are trying to do is not MVVM pattern.
You are creating a new ViewModel when button is clicked. It is the same as you are creating a business class to handle some business logics.
ViewModel and View are communicating through data binding. If you are familiar with RxSwift, the I suggest to use this library: https://github.com/duyduong/DDMvvm
I wrote this library after using it a lot on private projects. There are examples for you to start and understand how MVVM works. Give it a try!
To implement MVVM in iOS we can use a simple combination of Closure and didSet to avoid third-party dependencies.
public final class Observable<Value> {
private var closure: ((Value) -> ())?
public var value: Value {
didSet { closure?(value) }
}
public init(_ value: Value) {
self.value = value
}
public func observe(_ closure: #escaping (Value) -> Void) {
self.closure = closure
closure(value)
}
}
An example of data binding from ViewController:
final class ExampleViewController: UIViewController {
private func bind(to viewModel: ViewModel) {
viewModel.items.observe(on: self) { [weak self] items in
self?.tableViewController?.items = items
// self?.tableViewController?.items = viewModel.items.value // This would be Momory leak. You can access viewModel only with self?.viewModel
}
// Or in one line:
viewModel.items.observe(on: self) { [weak self] in self?.tableViewController?.items = $0 }
}
override func viewDidLoad() {
super.viewDidLoad()
bind(to: viewModel)
viewModel.viewDidLoad()
}
}
protocol ViewModelInput {
func viewDidLoad()
}
protocol ViewModelOutput {
var items: Observable<[ItemViewModel]> { get }
}
protocol ViewModel: ViewModelInput, ViewModelOutput {}
final class DefaultViewModel: ViewModel {
let items: Observable<[ItemViewModel]> = Observable([])
// Implmentation details...
}
Later it can be replaced with SwiftUI and Combine (when a minimum iOS version in of your app is 13)
In this article, there is a more detailed description of MVVM
https://tech.olx.com/clean-architecture-and-mvvm-on-ios-c9d167d9f5b3
class RegisterViewController: UIViewController {
var user = User() {
didSet {
// update UI
}
}
}
Most MVVM/RxSwift developers don't understand the notion of "over-engineering", as can be seen from all previous answers. Two of them refer you to a even more complicated design pattern, and one of them built the said pattern from scratch.
You don't need any of the RxSwift nonsense. MVVM isn't about having an object called view model and shoving everything to it.
Build a model so that when it changes, it updates associated view.
Simple, as all things should be.
Below is the pinnacle of over-engineering
protocol ViewModel: ViewModelInput, ViewModelOutput {}
After you define all these, write them down, train colleagues, draw diagrams, and implement them, you would've realized that it's all boilerplate and you should just drop them.

Resources