Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Rocket Data SLUG Talk

Rocket Data SLUG Talk

Slides for the presentation given at SLUG on July 21st, 2016. http://www.meetup.com/swift-language/events/232135313/

Avatar for Peter Livesey

Peter Livesey

July 21, 2016

Other Decks in Programming

Transcript

  1. Immutable Models • Thread-safe • Performant • Easier to debug

    • Promote functional code https://realm.io/news/slug-keith-smiley-embrace-immutability/
  2. How do I keep things consistent? class MyViewController: UIViewController {

    let personModel: PersonModel func comeOnline() { personModel.isOnline = true refreshView() } }
  3. Our Requirements • Immutable, thread-safe models • Consistency • Non-blocking

    access on all reads and writes • Simple eviction strategy • Scales well • Easy migrations
  4. How does this look in a view controller? class MyViewController:

    UIViewController { let dataProvider = DataProvider<PersonModel>() func viewDidLoad() { super.viewDidLoad() dataProvider.fetchDataFromCache(cacheKey: self.id) { (_, _) in self.refreshView() } MyNetworkManager.fetchPerson(id: self.id) { (model, error) in if let model = model { self.dataProvider.setData(model) self.refreshView() } } } }
  5. How to Respond to Changes class MyViewController: UIViewController { func

    dataProviderHasUpdatedData<T>(dataProvider: DataProvider<T>, context: Any?) { refreshView() } }
  6. What’s Required for Models protocol Model { var modelIdentifier: String?

    { get } func isEqualToModel(model: Model) -> Bool func map(transform: Model -> Model?) -> Self? func forEach(visit: Model -> Void) }
  7. The Cache public protocol CacheDelegate { func modelForKey<T: Model>(cacheKey, context,

    completion) func setModel<T: Model>(model, forKey cacheKey, context) func collectionForKey<T: Model>(cacheKey, context, completion) func setCollection<T: Model>(collection, forKey cacheKey, context) func deleteModel(model, forKey cacheKey, context) }
  8. Thank you Thanks to Nick Snyder, Nitesh Donti, and the

    Voyager, Groups and Lynda teams for making Data happen