Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 4 years ago.
Improve this question
HAs anyone implemented MVVM pattern in your iOS app without using ReactiveCocoa?
Found lots of examples here, but all of them use Reactive Cocoa. I wanted a simple example of MVVM implementation.
Reactive Cocoa is definitely not required for MVVM. I have built a very successful MVVM framework w/o any bindings at all. Bindings are not a requirement for MVVM.
Specifically, the linkage between the View Model and the View does require the View Model to signal to the View that it needs to update its data. This can be achieved using Reactive Cocoa, KVO (I really like Facebook's KVOController), or even using a simple delegate pattern.
The View Model knows when the View needs to update - either data has changed, or you're making an async data request via the Model and the Model has been loaded into the View Model.
When you set up your View, you could bind each control to the corresponding value on the View Model. I have found that when I need to churn out screens, this can get very tedious. Instead, I'd rather have a single method that is called when the View Model signals that the View should update itself. Within that method, I'm simply going to set all of my control properties.
Now, you only need to concern yourself with how that method is triggered. In my personal framework, I leverage KVO and my ViewControllers monitor a timestamp property on my ViewModel baseclass. Any time my view models update their underlying data, its timestamp is updated which triggers the update. You could just as easily have the View Controller register itself as the ViewModel's update delegate and use a standard delegate pattern.
Again, MVVM is not about specific implementation requirements, and more about a higher level concept of separation of concerns, dependency decoupling, and encapsulation.
Related
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
which is a correct place to call API in MVVM architecture pattern ?
i have come across two kind of implementations for above scenario.
1) calling api from viewcontroller.
2) calling api from viewmodel.
which one is better suitable as per MVVM architecture ?
or any other better way from where to call api in MVVM ?
ofcourse API calls are implemented in separate web-services related classes.
but from somewhere i have to call that method (either view controller or view model)
View: Only set view logic here such as text, color, constraints, layer etc. Super dumb.
Model: Is a Person, Book, Dog, Todo. It should only represent data in a database. Normally implements Codable etc.
View Model: The view should ask the view model for all data. So, this is where the api call is. The view should not know about the api at all.
A UIViewController, is a view and should have a viewModel. A UIViewController must still be dumb and have not idea from the API.
A view controller acts as an intermediary between the views it manages and the data of your app
so it make more sense to call your apiCall function on the ViewController
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I'm trying to create an app with ability for user to log in via Facebook, Twitter, Google, and LinkedIn. I'm using a single UIViewController and adding more and more code to viewDidLoad method. At this point my controller looks awful and I would like to split the code between associated classes (one for Facebook, and so on). I even thought about using custom views for every social media login button...
What would be the right thing to do?
There are at least two things to split in your case.
Login with different networks. It purely depends on your UI. You might have a separate view (and corresponding class) for each network or a popup.
Split code by applying an architectural pattern (MVC, MVVM, VIPER, etc.)
Here is a nice overview of them:
https://techblog.badoo.com/blog/2016/03/21/ios-architecture-patterns/
Additional change you should do is to introduce a separate bunch of classes to handle log in via social networks. Then in your view controller (or better in a View Model or somewhere else, depending on the design pattern you choose) you call SocialNetworkService.authenticateWithTwitter(email:password)
But remember, these classes have to know nothing about view controller.
I hope my answer gives you a hint in which direction to go.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 7 years ago.
Improve this question
In iOS generally we follow MVC project structure but it makes our view controller very massive.
I seen many Github projects but there is no such example which makes my view controller small, modular code, reusable code and many more things.
Generally what i am doing is something like this... Image of architecture
Service layer handles the logic whether they want to save the data in CoreData or not and also provides proper parameters for network layer to make api call..
Network Layer calls the api with or without header or token according to requirement and then get the response and then parse the data and provide the temp model to service layer.
But still i am unable to find the best way to create architecture for iOS project. It would be really helpful if someone will provide me any link or provide us any github link of their code.
You can look to MVVM pattern. Some example is here
I've written a quick introduction to MVVM. It explains briefly what MVVM is and why you should use it over MVC. Here's a more detailed look at MVVM.
If you need more granulation, you can use VIPER.
In my opinion MVVM is great for small/medium projects and VIPER should be considered in huge projects with a lot of contributors.
You need some language mechanic for binding in MVVM. KVO does the job, but it's API isn't very good. You could try ReactiveCocoa for data binding in MVVM/VIPER.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
What is exactly inheritance, polymorphism and encapsulation in iOS with example? Do iOS uses all this features? And how?
The concept of inheritance brings something of a real-world view to programming. It allows a class to be defined that has a certain set of characteristics (such as methods and instance variables) and then other classes to be created which are derived from that class. The derived class inherits all of the features of the parent class and typically then adds some features of its own.
Please check This link: An Objective-C Inheritance Example
The word polymorphism means having many forms. Typically, polymorphism occurs when there is a hierarchy of classes and they are related by inheritance.
Objective-C polymorphism means that a call to a member function will cause a different function to be executed depending on the type of object that invokes the function.
Consider the example, we have a class Shape that provides the basic interface for all the shapes. Square and Rectangle are derived from the base class Shape.
Please check This link: An Objective-C Polymorphism Example
Encapsulation is an Object-Oriented Programming concept that binds together the data and functions that manipulate the data and that keeps both safe from outside interference and misuse. Data encapsulation led to the important OOP concept of data hiding.
Data encapsulation is a mechanism of bundling the data and the functions that use them, and data abstraction is a mechanism of exposing only the interfaces and hiding the implementation details from the user.
Please check This link: An Objective-C Data Encapsulation
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I am an ASP.NET MVC developer and I am confused. What's the difference between Model, ViewModel and DTO (Data Transfer Object)? Is it ok for model to have methods that will save itself to database?
DTO is an object for passing data in case of communication between layers. It's a general pattern that is not tied to ASP.NET MVC.
ViewModel contains a data specific to particular view, is passed to that view in controller and is used in the view for rendering. It's a pattern specific to ASP.NET MVC (don't mix up with ViewModel from MVVM - they are different)
Model is a set of objects that represent your business domain. It can contain methods that will save it to DB depending of what pattern you will choose to build it (something like Active Record in your case).