Slide 1

Slide 1 text

Core Data in Motion RubyMotion #inspect May 28-29, 2014

Slide 2

Slide 2 text

Lori Olson • [email protected] • www.wndx.com • @wndxlori • Freelance developer, mentor, trainer • CANADA!

Slide 3

Slide 3 text

KUDOS Changing the world, one Thank You at a time! kudosnow.com

Slide 4

Slide 4 text

i Develop • Ruby (mostly) • Rails • iOS (RubyMotion) • JRuby • Javascript (some) • jQuery • Maps/Charts • Javascript Testing

Slide 5

Slide 5 text

i Teach • Ruby on Rails for Real Developers (RoR4Real) • Rails for iOS Developers (Rails4iOS) • Ladies Learning Code - Ruby Introduction

Slide 6

Slide 6 text

i Speak • 2014 • Scottish Ruby Conference, RubyMotion #inspect • 2013 • RubyConf AU, NSScotland • 2012 • Confoo, RailsConf, Aloha RubyConf • 2011 • jQueryConf, Madison Ruby • 2010 • Conferencia Rails • 2009 • RailsConf

Slide 7

Slide 7 text

The Task

Slide 8

Slide 8 text

Why? Learning RubyMotion

Slide 9

Slide 9 text

How? To rewrite a (mobile) web application as an iOS application

Slide 10

Slide 10 text

What? Wells In My Back Yard

Slide 11

Slide 11 text

The Challenges

Slide 12

Slide 12 text

LOTS of data

Slide 13

Slide 13 text

On a Map

Slide 14

Slide 14 text

In a List (table)

Slide 15

Slide 15 text

Filtering by Location

Slide 16

Slide 16 text

How hard could it be?

Slide 17

Slide 17 text

Oh, not that much data

Slide 18

Slide 18 text

RubyMotion gems/DSL’s • Nitron • MotionDataWrapper • MotionModel • MotionMigrate • many, many others

Slide 19

Slide 19 text

Problems • RubyMotion gems/DSL’s hide a lot • Straight iOS Objective-C development relies on Xcode magic (hides a lot) • Complex data is complex • Large data is large • What do I do when I reach the limitations of these solutions?

Slide 20

Slide 20 text

The Real Problem • Sometimes you just need to understand how to solve problems at the most basic API code level, and the abstractions (and magic) just get in the way

Slide 21

Slide 21 text

iOS Basics • Ray Wenderlich! • http://www.raywenderlich.com/934/core- data-tutorial-for-ios-getting-started

Slide 22

Slide 22 text

It’s not as scary as it looks

Slide 23

Slide 23 text

What? • Models (entities) in code • Relationships in code • Loading data • Optimization

Slide 24

Slide 24 text

Models in code

Slide 25

Slide 25 text

Models in Code • Hey, isn’t there a sample app for that? • Locations • https://github.com/HipByte/ RubyMotionSamples/tree/master/ios/ Locations • MVCS pattern

Slide 26

Slide 26 text

location.rb (model)

Slide 27

Slide 27 text

location.rb (entity)

Slide 28

Slide 28 text

location.rb (properties)

Slide 29

Slide 29 text

location_store.rb (store)

Slide 30

Slide 30 text

location_store.rb (MoM)

Slide 31

Slide 31 text

location_store.rb (psc)

Slide 32

Slide 32 text

location_store.rb (MoC)

Slide 33

Slide 33 text

That was easy

Slide 34

Slide 34 text

Or was it?

Slide 35

Slide 35 text

No content

Slide 36

Slide 36 text

Overly simplistic • Doesn’t work with multiple models with relationships • You need a reference to an entity, to define a relationship fully

Slide 37

Slide 37 text

Chicken & Egg problem

Slide 38

Slide 38 text

Relationships in code

Slide 39

Slide 39 text

Solution • Define your entities. First. • Lazily! define your entities’ properties • properties = (attributes and relationships)

Slide 40

Slide 40 text

Just the entity...

Slide 41

Slide 41 text

MoC, lazily defined properties

Slide 42

Slide 42 text

attributes

Slide 43

Slide 43 text

attributes

Slide 44

Slide 44 text

relationships

Slide 45

Slide 45 text

relationships

Slide 46

Slide 46 text

properties

Slide 47

Slide 47 text

The “other” model

Slide 48

Slide 48 text

Relationships • Done • With that, you can pretty much define any kind of model and relationship you want, in code, no magic required.

Slide 49

Slide 49 text

Data Loading

Slide 50

Slide 50 text

iOS Core Data Basics • Back to Ray • http://www.raywenderlich.com/12170/core- data-tutorial-how-to-preloadimport- existing-data-updated

Slide 51

Slide 51 text

The RubyMotion way

Slide 52

Slide 52 text

Read the data file (JSON)

Slide 53

Slide 53 text

Add data to store

Slide 54

Slide 54 text

add_bank

Slide 55

Slide 55 text

That was easy

Slide 56

Slide 56 text

Or was it?

Slide 57

Slide 57 text

How many wells do I have to load again?

Slide 58

Slide 58 text

244,292 Oh.

Slide 59

Slide 59 text

Houston, we have a problem

Slide 60

Slide 60 text

Read in the WHOLE file?

Slide 61

Slide 61 text

save for every add?

Slide 62

Slide 62 text

That won’t work either • add, add, add ...., save once

Slide 63

Slide 63 text

Batching… ? • add, add, add, save, add, add, add, save

Slide 64

Slide 64 text

Wait, what?

Slide 65

Slide 65 text

Back to Core Data Basics • Thankfully, Ray figured that out. • Updated the tutorial to operate as an OS X (console) app. • RubyMotion can do that, too.

Slide 66

Slide 66 text

New, improved load

Slide 67

Slide 67 text

streaming (CSV) load

Slide 68

Slide 68 text

create_bank (no save)

Slide 69

Slide 69 text

saves, every 100 progress every 100/1000

Slide 70

Slide 70 text

Catch the odd ones

Slide 71

Slide 71 text

Ta da! 244,292 wells loaded

Slide 72

Slide 72 text

And then your table view chokes on 244k items

Slide 73

Slide 73 text

Optimization

Slide 74

Slide 74 text

Back to Basics (again) • Ray sure has a lot of good material, doesn’t he? • http://www.raywenderlich.com/999/core- data-tutorial-for-ios-how-to-use- nsfetchedresultscontroller

Slide 75

Slide 75 text

Why? • NSFetchedResultsController gives us huge performance benefits • Avoids loading all the data in memory at once

Slide 76

Slide 76 text

bank_store.rb

Slide 77

Slide 77 text

failed_bank_table_view_controller.rb

Slide 78

Slide 78 text

failed_bank_table_view_controller.rb

Slide 79

Slide 79 text

number of rows

Slide 80

Slide 80 text

cell for row

Slide 81

Slide 81 text

configure cell

Slide 82

Slide 82 text

the delegate

Slide 83

Slide 83 text

No content

Slide 84

Slide 84 text

In Summary • Create models in code • Create relationships between models in code • Pre-loading data • LOTS of data • Efficiently displaying data

Slide 85

Slide 85 text

Please use Core Data!

Slide 86

Slide 86 text

Core Data in Motion • It’s an ebook! • Almost finished! • http://coredatainmotion.com

Slide 87

Slide 87 text

That’s All, Folks! • Code: 
 https://github.com/wndxlori/WNDXRubyMotion/ tree/master/FailedBankCD • Questions? Comments? • @wndxlori • [email protected] • http://www.linkedin.com/in/loriolson • http://www.wndx.com