core data in rubymotion #inspect

Post on 12-May-2015

375 Views

Category:

Software

0 Downloads

Preview:

Click to see full reader

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.

TRANSCRIPT

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

Lori Olson

• lori@wndx.com

• www.wndx.com

• @wndxlori

• Freelance developer, mentor, trainer

• CANADA!

KUDOSChanging the world, one Thank You at a time!

kudosnow.com

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

i Develop• Ruby (mostly)

• Rails

• iOS (RubyMotion)

• JRuby

• Javascript (some)

• jQuery

• Maps/Charts

• Javascript Testing

i Teach

• Ruby on Rails for Real Developers (RoR4Real)

• Rails for iOS Developers (Rails4iOS)

• Ladies Learning Code - Ruby Introduction

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

The Task

Why?Learning RubyMotion

How?To rewrite a (mobile) web application

as an iOS application

What?Wells In My Back Yard

The Challenges

LOTS of data

On a Map

In a List (table)

Filtering by Location

How hard could it be?

Oh, not that much data

244,292

RubyMotion gems/DSL’s

• Nitron

• MotionDataWrapper

• MotionModel

• MotionMigrate

• many, many others

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?

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

iOS Basics

• Ray Wenderlich!

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

It’s not as scary as it looks

What?

• Models (entities) in code

• Relationships in code

• Loading data

• Optimization

Models in code

Models in Code

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

• Locations

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

• MVCS pattern

location.rb (model)

location.rb (entity)

location.rb (properties)

location_store.rb (store)

location_store.rb (MoM)

location_store.rb (psc)

Persistent Store Coordinator

location_store.rb (MoC)

Managed Object Context

That was easy

Or was it?

Overly simplistic

• Doesn’t work with multiple models with relationships

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

Chicken & Egg problem

Relationships in code

Solution

• Define your entities. First.

• Lazily! define your entities’ properties

• properties = (attributes and relationships)

Just the entity...

Wait a minute...

MoC, lazily defined properties

attributes

attributes

relationships

relationships

properties

The “other” model

More of the same

Relationships

• Done

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

Data Loading

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.

The RubyMotion way

Inside our store, create a load method

Read the data file (JSON)

Add data to store

Wait, what does add_bank do again?

add_bank

That was easy

Or was it?

How many wells do I have to load again?

244,292 Oh.

Houston, we have a problem

Read in the WHOLE file?

save for every add?

add, save, add, save

That won’t work either

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

Ok, no, that won’t work either

Batching… ?

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

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

Wait, what?

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)

New, improved load

streaming (CSV) load

create_bank (no save)

saves, every 100 progress every 100/1000

Catch the odd ones

Ta da! 244,292 wells loaded

And then your table view chokes on 244k

items

Optimization

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

Why?

• NSFetchedResultsController gives us huge performance benefits

• Avoids loading all the data in memory at once

bank_store.rb

failed_bank_table_view_controller.rb

failed_bank_table_view_controller.rb

wall of text. let’s break that down

number of rows

cell for row

configure cell

the delegate

In Summary

• Create models in code

• Create relationships between models in code

• Pre-loading data

• LOTS of data

• Efficiently displaying data

Please use Core Data!

Finnegan!

Core Data in Motion

• It’s an ebook!

• Almost finished!

• http://coredatainmotion.com

That’s All, Folks!• Code:

https://github.com/wndxlori/WNDXRubyMotion/tree/master/FailedBankCD

• Questions? Comments?

• @wndxlori

• lori@wndx.com

• http://www.linkedin.com/in/loriolson

• http://www.wndx.com

top related