UI View Controller is not updated when using VIPER Architecture - ios

So, I am new to VIPER and have built simple demo app of http request using the architecture. I got an issue where the UI are not updated although the the method within the view is still called because I have checked that the print("test") is executed and shown on the debug console. This is the relevant code:
View :
private func getAllContacts(){
self.contacts = []
self.hud.show(in: self.view)
self.presenter?.fetchListContacts()
print("asd")
}
Presenter :
func fetchListContacts(){
print("sdf")
interactor?.getContacts()
}
Interactor :
func getContacts(){
print("123")
var contacts : [DetailContact] = []
contacts = \\APICALL
self.presenter?.listContactFetchSuccess(contacts)
}
Baxk to PResenter :
func listContactFetchSuccess(_ contacts: [DetailContact]) {
print("gxx")
view?.fetchSucceed(contacts: contacts)
}
back to View :
func fetchSucceed(contacts: [DetailContact]) {
self.contacts = contacts
self.hud.dismiss()
self.contactTableView.reloadData()
print("test")
}
My Router :
static func createListContactModule() -> ListViewController {
let view = mainstoryboard.instantiateViewController(withIdentifier: "ListViewController") as! ListViewController
var presenter: ViewToPresenterListProtocol & InteractorToPresenterListProtocol = ListContactPresenter()
var interactor: PresenterToInteractorListProtocol = ListContactInteractor()
let router:PresenterToRouterListProtocol = ListContactRouter()
view.presenter = presenter
presenter.view = view
presenter.router = router
presenter.interactor = interactor
interactor.presenter = presenter
return view
}

The progress hud at least closes? or does not suffer any change?
I suggest you put the content of your function fetchSucceed(contacts: [DetailContact]) into the main queue like:
func fetchSucceed(contacts: [DetailContact]) {
DispatchQueue.main.async {
self.contacts = contacts
self.hud.dismiss()
self.contactTableView.reloadData()
print("test")
}
}
Maybe the API call that you use, is changing the thread of the excecution therefore your UI calls are not showing, because it always must be excecuted in the main thread.

Related

ObservedObject is still in memory after the view is dismissed, Memory Leak?

I'm making an app with SwiftUI and UIkit, I use UIkit for the main app controller and navigation, and I use SwiftUI for app design.
The app works very well, but I'm worried about the memory leaks. This is because the ViewModels I use to pass data between views don't call desinit whene the view disappears. I know that in SwiftUI views are not disposed immediately, but since I'm using UIKit to navigate I don't know what the problem is.
//The ViewModel for each user fetched
internal class UserViewModel: ObservableObject, Identifiable {
//MARK: - Propeties var currentListener: ListenerRegistration?
#Published var request: Request?
#Published var user: User
init(user: User) {
self.user = user
getRequest()
fetchAdmins()
}
deinit {
//Dosnt get called removeListener()
}
func getRequest() {
guard let uid = Auth.auth().currentUser?.uid else {return}
guard let id = id else {return}
self.currentListener = Collections.requests(id).document(uid).addSnapshotListener { snapshot, error in
if let error = error {
print(error.localizedDescription)
return
}
if ((snapshot?.exists) != nil) {
if let request = try? snapshot!.data(as: Request.self) {
DispatchQueue.main.async {
self.request = request
}
}
}
}
}
func removeListener() {
self.currentListener?.remove()
}
}
}
//The ViewModel to fetch all the users ViewModels
class UsersViewModel: ObservableObject {
#Published var users = [UserViewModel]()
func fetch() {
DispatchQueue.global(qos: .background).async {
Collections.users.getDocuments(completion: { snapshot, err in
guard let documents = snapshot?.documents else { return } let users = documents.compactMap({ try? $0.data(as: User.self) })
users.forEach { user in
let vm = UserViewModel(user: user)
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
self.users.append(vm)
}
}
})
}
} }
//Show the users cells with the ViewModel
struct HomeView: View {
#ObservedObject var usersViewModels: UsersViewModel
//MARK: - Init
init() {
self.usersViewModels = UsersViewModel()
}
var body: some View {
ListView(content: {
ForEach(usersViewModels) { usersViewModel in
UserCell(viewModel: usersViewModel).id(user.id)
}
})
}
}
This is how I navigate between controllers and views of my app. I don't use NavigationLinks:
public static func push<Content: View>(view: Content) {
DispatchQueue.main.async {
guard let tabBarController = UIApplication.rootViewController as? UITabBarController, let navigationController = tabBarController.selectedViewController as? UINavigationController else { return nil }
if let navigationController = UIApplication.getCurrentNavigationController() {
navigationController.pushViewController(HostingController(content: view), animated: true)
}
}
}
Does anyone know if this method that I am using to navigate can cause me memory problems? And you know why my app doesn't reduce its memory every time I close a window, it just increases more and more.
The disappearing does not mean it is no longer in memory.
It looks like you keep pushing them onto the navigation stack which increases their retain count.
You've got a memory leak here:
struct HomeView: View {
#ObservedObject var usersViewModels: UsersViewModel
//MARK: - Init
init() {
self.usersViewModels = UsersViewModel() // here
}
View structs must not init objects because the View struct is recreated every state change thus the object is being constantly init.
SwiftUI is all about taking advantage of value semantics, try to use #State with value types (or group them in a struct) in the View struct for your view data.
Model data structs go in a singleton ObservableObject supplied to the Views using .environmentObject.

Passing data between ViewModels in MVVM-C

I am using MVVM with Coordinator to design an application. One thing that i am having doubts on is on how to pass data between different ViewModels. Normally the previous viewModel would just create the next viewModel and would just do a method dependency injection in prepareforsegue. However now that i am responsible for all the navigation how do i achieve this ?
Class AppCoordinator : NSObject, Coordinator, UINavigationControllerDelegate {
var childCoordinators = [Coordinator]()
var navigationController: UINavigationController
var dependencyContainer : MainDependencyContainer
func start() {
let vc = ViewController.instantiate()
vc.coordinator = self
vc.viewModel = dependencyContainer.makeMainViewModel()
navigationController.delegate = self
navigationController.pushViewController(vc, animated: true)
}
func createAccount() {
let vc = CreateAccountViewController.instantiate()
vc.coordinator = self
navigationController.pushViewController(vc, animated: true)
}
}
I could ofcourse create the ViewModel for CreateAccountViewController in MainViewModel and pass the ViewModel as a paramter in createAccount method but is it the right way to do it here ? What will be the unit testing implications here ?
Ideally, you don't want both ViewModels to interact with each other and keep both elements separated.
One way to deal with it is to pass through the minimum data required for the navigation.
class AppCoordinator : NSObject, Coordinator, UINavigationControllerDelegate {
var childCoordinators = [Coordinator]()
var navigationController: UINavigationController
var dependencyContainer : MainDependencyContainer
func start() {
let vc = ViewController.instantiate()
vc.coordinator = self
let viewModel = dependencyContainer.makeMainViewModel()
// for specific events from viewModel, define next navigation
viewModel.performAction = { [weak self] essentialData in
guard let strongSelf = self else { return }
strongSelf.showAccount(essentialData)
}
vc.viewModel = viewModel
navigationController.delegate = self
navigationController.pushViewController(vc, animated: true)
}
// we can go further in our flow if we need to
func showAccount(_ data: AnyObject) {
let vc = CreateAccountViewController.instantiate()
vc.viewModel = CreateAccountViewController(with: data)
vc.coordinator = self
navigationController.pushViewController(vc, animated: true)
}
}
Going further, you can create a specific Coordinator for CreateAccountViewController that will get initialized with those data. The start() method will do create whatever is needed for its ViewController.
// we can go further in our flow if we need to
func showAccount(_ data: AnyObject) {
let coordinator = CreateAccountCoordinator(data: data, navigationController: navigationController)
coordinator.start()
childCoordinators.append(coordinator)
}
In this last example, the coordinator is only responsible to build its view and pass through essential information to next coordinator whenever needed. The viewModel is only exposed to its view, and eventually the view is unaware of both. It could be a good alternative in your case.
Finally, you can test using a protocol abstraction to make sure performAction triggers showAccount, that showAccount create a child coordinator, and so on.

passing data between view controllers without changing views

I want to pass data between two view controllers, but don't want the view to change when the users presses my save data button.
The users needs to fill in multiple data fields, and when finish can press another button to go to the second view controller.
I found many tutorials how to pass data using segue, but they all change view as soon as the 'save button is pressed'.
Any one can explain to me how to alter the code?
#Phillip Mills: here is how I used your code. (what am I doing wrong?)
code:
//////// declaring classes on FirstViewController (trying it first on only one ViewController)
class FakeVC1 {
func userInput() {
DataModel.shared.username = outbj14u.text
}
class FakeVC2 {
func viewAppears() {
if let name = DataModel.shared.username {
outbj14p.text = name
print("I have nothing to say")
}
}
}
class DataModel {
static let shared = DataModel()
var username: String?
}
////till here
//// here is where i call the functions
override func viewDidAppear(_ animated: Bool) {
FakeVC1().userInput()
FakeVC2().viewAppears()
if let xbj14p = UserDefaults.standard.object(forKey: "outbj14p") as? String
{
outbj14p.text = xbj14p
}
if let xbj14u = UserDefaults.standard.object(forKey: "outbj14u") as? String
{
outbj14u.text = xbj14u
}
////
#Phillip Mills: Below is what I have know. I think I got the code on the FirstViewController right, but the code on the Second View controller must be wrong. I don't get any errors, but the text field on the SecondViewController remains unchanged after putting input on in the FirstViewController
//// Code on the FirstViewController
class DataModel {
static let shared = DataModel()
var username: String?
}
#IBAction func savebj14p(_ sender: Any) {
outbj14p.text = inbj14p.text
DataModel.shared.username = outbj14p.text
UserDefaults.standard.set(inbj14p.text, forKey: "namebj14p")
}
//and on the SecondViewController
#IBOutlet weak var bj14u: UILabel! // connected to a label
//and
class DataModel {
static let shared = DataModel()
var username: String?
}
override func viewDidAppear(_ animated: Bool) {
if let name = DataModel.shared.username {
bj14u.text = name
}
}
In your case, don't pass data.
Create a shared object to act as your data model. When users fill in the fields, update the data model.
When the user moves to the second controller/view, that controller uses the data model object to show what it needs to.
class FakeVC1 {
func userInput() {
DataModel.shared.username = "Me"
}
}
class FakeVC2 {
func viewAppears() {
if let name = DataModel.shared.username {
print(name)
} else {
print("I have nothing to say")
}
}
}
class DataModel {
static let shared = DataModel()
var username: String?
}
FakeVC1().userInput()
FakeVC2().viewAppears()
If you need to pass value to another viewcontroller without changing the view , you can user NSNotificationCenter class
Refer this link for more details
NSNotificationCenter addObserver in Swift
what i will recommend is to use a global variable or array, you will have the info in all view controllers and you will be able to call it in your new view controller.

Swift 3 : Back to last ViewController with sending data [duplicate]

This question already has answers here:
Passing data between view controllers
(45 answers)
Closed 5 years ago.
I'm trying to go back to my las viewController with sending data, but it doesn't work.
When I just use popViewController, I can go back to the page, but I can't move my datas from B to A.
Here is my code :
func goToLastViewController() {
let vc = self.navigationController?.viewControllers[4] as! OnaylarimTableViewController
vc.onayCode.userId = taskInfo.userId
vc.onayCode.systemCode = taskInfo.systemCode
self.navigationController?.popToViewController(vc, animated: true)
}
To pass data from Child to parent Controller, you have to pass data using Delegate pattern.
Steps to implement delegation pattern, Suppose A is Parent viewController and B is Child viewController.
Create protocol, and create delegate variable in B
Extend protocol in A
pass reference to B of A when Push or Present viewcontroller
Define delegate Method in A, receive action.
After that, According to your condition you can call delegate method from B.
You should do it using delegate protocol
class MyClass: NSUserNotificationCenterDelegate
The implementation will be like following:
func userDidSomeAction() {
//implementation
}
And ofcourse you have to implement delegete in your parent class like
childView.delegate = self
Check this for more information
https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Protocols.html
You have to send back to last ViewController with 2 options.
1. Unwind segue. (With use of storyboard)
You can refer this link.
2. Use of delegate/protocol.
You can refer this link.
Also this link will be useful for you.
You can use Coordinator Pattern
For example, I have 2 screens. The first displays information about the user, and from there, he goes to the screen for selecting his city. Information about the changed city should be displayed on the first screen.
final class CitiesViewController: UITableViewController {
// MARK: - Output -
var onCitySelected: ((City) -> Void)?
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
onCitySelected?(cities[indexPath.row])
}
...
}
UserEditViewController:
final class UserEditViewController: UIViewController, UpdateableWithUser {
// MARK: - Input -
var user: User? { didSet { updateView() } }
#IBOutlet private weak var userLabel: UILabel?
private func updateView() {
userLabel?.text = "User: \(user?.name ?? ""), \n"
+ "City: \(user?.city?.name ?? "")"
}
}
And Coordinator:
protocol UpdateableWithUser: class {
var user: User? { get set }
}
final class UserEditCoordinator {
// MARK: - Properties
private var user: User { didSet { updateInterfaces() } }
private weak var navigationController: UINavigationController?
// MARK: - Init
init(user: User, navigationController: UINavigationController) {
self.user = user
self.navigationController = navigationController
}
func start() {
showUserEditScreen()
}
// MARK: - Private implementation
private func showUserEditScreen() {
let controller = UIStoryboard.makeUserEditController()
controller.user = user
controller.onSelectCity = { [weak self] in
self?.showCitiesScreen()
}
navigationController?.pushViewController(controller, animated: false)
}
private func showCitiesScreen() {
let controller = UIStoryboard.makeCitiesController()
controller.onCitySelected = { [weak self] city in
self?.user.city = city
_ = self?.navigationController?.popViewController(animated: true)
}
navigationController?.pushViewController(controller, animated: true)
}
private func updateInterfaces() {
navigationController?.viewControllers.forEach {
($0 as? UpdateableWithUser)?.user = user
}
}
}
Then we just need to start coordinator:
coordinator = UserEditCoordinator(user: user, navigationController: navigationController)
coordinator.start()

How to pass data back to the first view controller in Swift?

I have two view controllers and I have this code to navigate between the two views
ViewController:
func goToSecondViewController() {
let aa = self.storyboard.instantiateViewControllerWithIdentifier("SecondViewController") as SecondViewController
self.navigationController.pushViewController(aa, animated: true)
}
/
SecondViewController:
func goToFirstViewController() {
self.navigationController.popToRootViewControllerAnimated(true)
}
How to send some data to the first view before popping it ?
If A is a view controller with the following declaration:
class A : UIViewController {
var data: String!
}
Then, whenever you have an instance of A, you can just set the data property directly:
let a = A() // assuming you've defined the init method
a.data = "hello"
Edit
If you want to send data before popping, you'd do something like:
func goToFirstViewController() {
let a = self.navigationController.viewControllers.first as! A
a.data = "data"
self.navigationController.popToRootViewControllerAnimated(true)
}
(haven't compiled the above)

Resources