mvvm

27
MVVM Robert Brown @robby_brown @robert_brown @rob-brown

Upload: robert-brown

Post on 12-May-2015

915 views

Category:

Technology


2 download

DESCRIPTION

In this presentation I implement the MVVM pattern by combining Core Data and Reactive Cocoa. The MVVM pattern can use any data storage and binding libraries, but I chose Core Data and Reactive Cocoa since they are common on iOS. Demo at https://github.com/rob-brown/Demos/tree/master/MVVMDemo.

TRANSCRIPT

Page 2: MVVM

Outline

1. Core Data

2. Reactive Cocoa

3. MVVM

Page 3: MVVM

Core Data

Page 4: MVVM

What is Core Data?

Intimidating

An amazing data persistence framework

Not a database

Page 5: MVVM

Core Data Layers

XML SQLite Binary In Memory Custom

NSPersistentStore

NSPersistentStoreCoordinator NSManagedObjectModel

NSManagedObjectContextNSManagedObject

Application

NSManagedObject Subclass

Page 6: MVVM

Core Data Stack

Persistent Store Coordinator

Managed Object Context(s)

Persistent Store(s)

Managed Object Model

Page 7: MVVM

More on Core Data

Intro to Core Data

Core Data: Data Storage Management for iOS, OS X, and iCloud

RBCoreDataStack

Page 8: MVVM

Reactive Cocoa

Page 9: MVVM

What is Reactive Cocoa?

Made by GitHub

A functional reactive programming (FRP) framework

Provides the bindings in MVVM

Page 10: MVVM

What is Reactive Cocoa?

Binds properties together

Eliminates the need for mutable state

UI updates automatically as data changes

Page 11: MVVM

More on Reactive Cocoa

Intro to Reactive Cocoa

Functional Reactive Programming on iOS

ReactiveCocoa

Page 12: MVVM

MVVM

Page 13: MVVM

What is MVVM?

Model-View-ViewModel

A variant of MVC

Uses an adapter between the view and model

Page 14: MVVM

What is MVVM?

View View Model Model

Owns Owns

UpdatesUpdates

Page 15: MVVM

What is MVVM?

Data Model

ViewModel Network

ViewController

View

Data Storage

Page 16: MVVM

Application Stack

ViewModel Network

ViewController

View

SQLite Binary In Memory Custom

NSPersistentStore

NSPersistentStoreCoordinator NSManagedObjectModel

NSManagedObjectContextNSManagedObject

NSManagedObject Subclass

XML

Page 17: MVVM

Model

Represents the data layer

Can be built with any technology

Ex. Core Data, SQLite, JSON, PList, memory-only

Page 18: MVVM

View

Handles layout and presentation of data

In iOS, the “View” refers to both UIViews and UIViewControllers

Page 19: MVVM

ViewModel

An adapter between the View and Model

Contains all the business logic

Uses bindings to pass data around

Page 20: MVVM

ViewModel

Acts like a model

Hides the real model logic from the view

Page 21: MVVM

ViewModel

Some responsibilities:

Kick off network and database requests

Show/hide information

Data/number formatting

Localization

Page 22: MVVM

Benefits of MVVM

View models are testable

View models can be used like models

View models are (mostly) platform-agnostic

View and view controllers are simpler

Page 23: MVVM

Related Patterns

Model Operation View Event (MOVE)

Model View Presenter (MVP)

Presentation Model (PM)

Page 24: MVVM

Questions?

Page 25: MVVM

Demo

Page 26: MVVM

Want to Learn More?

ReactiveViewModel

Functional Reactive Programming on iOS

Wikipedia