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

Core Data in RubyMotion #inspect

Embed Size (px)

DESCRIPTION

Core Data doesn't have to be hard. Step by step, learn how to use Core Data in code, creating models and relationships. Loading data and efficiently displaying that data.

Citation preview

Page 1: Core Data in RubyMotion #inspect

Core Data in MotionRubyMotion #inspect May 28-29, 2014

Page 2: Core Data in RubyMotion #inspect

Lori Olson

[email protected]

• www.wndx.com

• @wndxlori

• Freelance developer, mentor, trainer

• CANADA!

Page 3: Core Data in RubyMotion #inspect

KUDOSChanging the world, one Thank You at a time!

kudosnow.com

Thanks to my client Kudos, for being understanding about my speaking engagements.

Page 4: Core Data in RubyMotion #inspect

i Develop• Ruby (mostly)

• Rails

• iOS (RubyMotion)

• JRuby

• Javascript (some)

• jQuery

• Maps/Charts

• Javascript Testing

Page 5: Core Data in RubyMotion #inspect

i Teach

• Ruby on Rails for Real Developers (RoR4Real)

• Rails for iOS Developers (Rails4iOS)

• Ladies Learning Code - Ruby Introduction

Page 6: Core Data in RubyMotion #inspect

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

Page 7: Core Data in RubyMotion #inspect

The Task

Page 8: Core Data in RubyMotion #inspect

Why?Learning RubyMotion

Page 9: Core Data in RubyMotion #inspect

How?To rewrite a (mobile) web application

as an iOS application

Page 10: Core Data in RubyMotion #inspect

What?Wells In My Back Yard

Page 11: Core Data in RubyMotion #inspect

The Challenges

Page 12: Core Data in RubyMotion #inspect

LOTS of data

Page 13: Core Data in RubyMotion #inspect

On a Map

Page 14: Core Data in RubyMotion #inspect

In a List (table)

Page 15: Core Data in RubyMotion #inspect

Filtering by Location

Page 16: Core Data in RubyMotion #inspect

How hard could it be?

Page 17: Core Data in RubyMotion #inspect

Oh, not that much data

244,292

Page 18: Core Data in RubyMotion #inspect

RubyMotion gems/DSL’s

• Nitron

• MotionDataWrapper

• MotionModel

• MotionMigrate

• many, many others

Page 19: Core Data in RubyMotion #inspect

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?

Page 20: Core Data in RubyMotion #inspect

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

Page 21: Core Data in RubyMotion #inspect

iOS Basics

• Ray Wenderlich!

• http://www.raywenderlich.com/934/core-data-tutorial-for-ios-getting-started

Page 22: Core Data in RubyMotion #inspect

It’s not as scary as it looks

Page 23: Core Data in RubyMotion #inspect

What?

• Models (entities) in code

• Relationships in code

• Loading data

• Optimization

Page 24: Core Data in RubyMotion #inspect

Models in code

Page 25: Core Data in RubyMotion #inspect

Models in Code

• Hey, isn’t there a sample app for that?

• Locations

• https://github.com/HipByte/RubyMotionSamples/tree/master/ios/Locations

• MVCS pattern

Page 26: Core Data in RubyMotion #inspect

location.rb (model)

Page 27: Core Data in RubyMotion #inspect

location.rb (entity)

Page 28: Core Data in RubyMotion #inspect

location.rb (properties)

Page 29: Core Data in RubyMotion #inspect

location_store.rb (store)

Page 30: Core Data in RubyMotion #inspect

location_store.rb (MoM)

Page 31: Core Data in RubyMotion #inspect

location_store.rb (psc)

Persistent Store Coordinator

Page 32: Core Data in RubyMotion #inspect

location_store.rb (MoC)

Managed Object Context

Page 33: Core Data in RubyMotion #inspect

That was easy

Page 34: Core Data in RubyMotion #inspect

Or was it?

Page 35: Core Data in RubyMotion #inspect
Page 36: Core Data in RubyMotion #inspect

Overly simplistic

• Doesn’t work with multiple models with relationships

• You need a reference to an entity, to define a relationship fully

Page 37: Core Data in RubyMotion #inspect

Chicken & Egg problem

Page 38: Core Data in RubyMotion #inspect

Relationships in code

Page 39: Core Data in RubyMotion #inspect

Solution

• Define your entities. First.

• Lazily! define your entities’ properties

• properties = (attributes and relationships)

Page 40: Core Data in RubyMotion #inspect

Just the entity...

Wait a minute...

Page 41: Core Data in RubyMotion #inspect

MoC, lazily defined properties

Page 42: Core Data in RubyMotion #inspect

attributes

Page 43: Core Data in RubyMotion #inspect

attributes

Page 44: Core Data in RubyMotion #inspect

relationships

Page 45: Core Data in RubyMotion #inspect

relationships

Page 46: Core Data in RubyMotion #inspect

properties

Page 47: Core Data in RubyMotion #inspect

The “other” model

More of the same

Page 48: Core Data in RubyMotion #inspect

Relationships

• Done

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

Page 49: Core Data in RubyMotion #inspect

Data Loading

Page 50: Core Data in RubyMotion #inspect

iOS Core Data Basics

• Back to Ray

• http://www.raywenderlich.com/12170/core-data-tutorial-how-to-preloadimport-existing-data-updated

But of course, his way is the Xcode way. We need to translate to RubyMotion.

Page 51: Core Data in RubyMotion #inspect

The RubyMotion way

Inside our store, create a load method

Page 52: Core Data in RubyMotion #inspect

Read the data file (JSON)

Page 53: Core Data in RubyMotion #inspect

Add data to store

Wait, what does add_bank do again?

Page 54: Core Data in RubyMotion #inspect

add_bank

Page 55: Core Data in RubyMotion #inspect

That was easy

Page 56: Core Data in RubyMotion #inspect

Or was it?

Page 57: Core Data in RubyMotion #inspect

How many wells do I have to load again?

Page 58: Core Data in RubyMotion #inspect

244,292 Oh.

Page 59: Core Data in RubyMotion #inspect

Houston, we have a problem

Page 60: Core Data in RubyMotion #inspect

Read in the WHOLE file?

Page 61: Core Data in RubyMotion #inspect

save for every add?

add, save, add, save

Page 62: Core Data in RubyMotion #inspect

That won’t work either

• add, add, add ...., save once

Ok, no, that won’t work either

Page 63: Core Data in RubyMotion #inspect

Batching… ?

• add, add, add, save, add, add, add, save

Looked promising, but still ended up with out of memory errors

Page 64: Core Data in RubyMotion #inspect

Wait, what?

Page 65: Core Data in RubyMotion #inspect

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.

Instead of running INSIDE the simulator (in your device)

Page 66: Core Data in RubyMotion #inspect

New, improved load

Page 67: Core Data in RubyMotion #inspect

streaming (CSV) load

Page 68: Core Data in RubyMotion #inspect

create_bank (no save)

Page 69: Core Data in RubyMotion #inspect

saves, every 100 progress every 100/1000

Page 70: Core Data in RubyMotion #inspect

Catch the odd ones

Page 71: Core Data in RubyMotion #inspect

Ta da! 244,292 wells loaded

Page 72: Core Data in RubyMotion #inspect

And then your table view chokes on 244k

items

Page 73: Core Data in RubyMotion #inspect

Optimization

Page 74: Core Data in RubyMotion #inspect

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

Page 75: Core Data in RubyMotion #inspect

Why?

• NSFetchedResultsController gives us huge performance benefits

• Avoids loading all the data in memory at once

Page 76: Core Data in RubyMotion #inspect

bank_store.rb

Page 77: Core Data in RubyMotion #inspect

failed_bank_table_view_controller.rb

Page 78: Core Data in RubyMotion #inspect

failed_bank_table_view_controller.rb

wall of text. let’s break that down

Page 79: Core Data in RubyMotion #inspect

number of rows

Page 80: Core Data in RubyMotion #inspect

cell for row

Page 81: Core Data in RubyMotion #inspect

configure cell

Page 82: Core Data in RubyMotion #inspect

the delegate

Page 83: Core Data in RubyMotion #inspect
Page 84: Core Data in RubyMotion #inspect

In Summary

• Create models in code

• Create relationships between models in code

• Pre-loading data

• LOTS of data

• Efficiently displaying data

Page 85: Core Data in RubyMotion #inspect

Please use Core Data!

Finnegan!

Page 86: Core Data in RubyMotion #inspect

Core Data in Motion

• It’s an ebook!

• Almost finished!

• http://coredatainmotion.com

Page 87: Core Data in RubyMotion #inspect

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