mobile architecture problems and solutions
TRANSCRIPT
Cons
◦ Massive View Controller!◦ Sometimes hard to test◦ More “layers” are needed as app grows
◦ Its an “Apple thing”◦ Decrease code clarity◦ There are better design patterns
MVVM
◦ View controllers are technically distinct components, they almost always go hand-in-hand together, paired
◦ When is the last time that a view could be paired with different view controllers??
MVVM
◦ Most of the stuff in ViewController is presentation logic
◦ Things like transforming values from the model into something the view can present
MVVM
MVVM is an augmented version of MVC where we formally connect the view and controller, and move presentation logic out of the controller and into a new object.
MVVM
Why?◦ Reduces the complexity of one’s view controllers and makes one’s presentation logic easier to test
Some Code
@interface Person : NSObject
- (instancetype)initwithSalutation:(NSString *)salutation firstName:(NSString
*)firstName lastName:(NSString *)lastName birthdate:(NSDate *)birthdate;
@property (nonatomic, readonly) NSString *salutation;
@property (nonatomic, readonly) NSString *firstName;
@property (nonatomic, readonly) NSString *lastName;
@property (nonatomic, readonly) NSDate *birthdate;
@end
PersonViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
if (self.model.salutation.length > 0) {
self.nameLabel.text = [NSString stringWithFormat:@"%@ %@ %@", self.model.salutation,
self.model.firstName, self.model.lastName];
} else {
self.nameLabel.text = [NSString stringWithFormat:@"%@ %@", self.model.firstName,
self.model.lastName];
}
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"EEEE MMMM d, yyyy"];
self.birthdateLabel.text = [dateFormatter stringFromDate:model.birthdate];
}
Now with MVVM, PersonViewModel.h
@interface PersonViewModel : NSObject
- (instancetype)initWithPerson:(Person *)person;
@property (nonatomic, readonly) Person *person;
@property (nonatomic, readonly) NSString *nameText;
@property (nonatomic, readonly) NSString *birthdateText;
@end
PersonViewModel.m
@implementation PersonViewModel
- (instancetype)initWithPerson:(Person *)person {
self = [super init];
if (!self) return nil;
_person = person;
if (person.salutation.length > 0) {
_nameText = [NSString stringWithFormat:@"%@ %@ %@", self.person.salutation,
self.person.firstName, self.person.lastName];
} else {
_nameText = [NSString stringWithFormat:@"%@ %@", self.person.firstName,
self.person.lastName];
}
An our view controller
- (void)viewDidLoad {
[super viewDidLoad];
self.nameLabel.text = self.viewModel.nameText;
self.birthdateLabel.text = self.viewModel.birthdateText;
}
MVP Model View Presenter
Presenter is a layer that provides View with data from Model. Presenter also handles background tasks.
On top of that
◦ View can disappear and appear at random time
◦ Do not forget about saving/restoring of Views
◦ Attach a couple of background tasks to that temporary Views
MVP Pros
◦ Complex tasks are split into simpler tasks and are easier to solve.
◦ Smaller objects, less bugs, easier to debug.
◦ Testable.