Slide 1

Slide 1 text

New Library for CoreData Hiroshi Kimura potatotips #19

Slide 2

Slide 2 text

໦ଜ ׮ Hiroshi Kimura iOS Developer eureka, Inc. @muukii0803

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

CoreStore https://github.com/JohnEstropia/CoreStore

Slide 7

Slide 7 text

Developer Hiroshi Kimura eureka, Inc. @muukii0803 John Estropia eureka, Inc. @JohnEstropia

Slide 8

Slide 8 text

Good things :)

Slide 9

Slide 9 text

Good things :) • SwiftͰॻ͔Ε͍ͯΔ

Slide 10

Slide 10 text

Good things :) • SwiftͰॻ͔Ε͍ͯΔ • ϝΠϯεϨουͷContext͸Read-Only
 ॻ͖ࠐΈ͸͢΂ͯSerialʹBackgroundContextͰߦΘΕΔͨΊɺ
 ҆શʹಈ࡞͢Δઃܭ

Slide 11

Slide 11 text

Good things :) • SwiftͰॻ͔Ε͍ͯΔ • ϝΠϯεϨουͷContext͸Read-Only
 ॻ͖ࠐΈ͸͢΂ͯSerialʹBackgroundContextͰߦΘΕΔͨΊɺ
 ҆શʹಈ࡞͢Δઃܭ • ෳ਺ͷSQLiteϑΝΠϧ͕࢖༻Մೳ (ConfigurationͰ෼͚Δ)

Slide 12

Slide 12 text

Good things :) • SwiftͰॻ͔Ε͍ͯΔ • ϝΠϯεϨουͷContext͸Read-Only
 ॻ͖ࠐΈ͸͢΂ͯSerialʹBackgroundContextͰߦΘΕΔͨΊɺ
 ҆શʹಈ࡞͢Δઃܭ • ෳ਺ͷSQLiteϑΝΠϧ͕࢖༻Մೳ (ConfigurationͰ෼͚Δ) • Observer͕༻ҙ͞Ε͍ͯΔ (FRCΛॻ͔ͳͯ͘ྑ͘ͳΔ)

Slide 13

Slide 13 text

Good things :) • SwiftͰॻ͔Ε͍ͯΔ • ϝΠϯεϨουͷContext͸Read-Only
 ॻ͖ࠐΈ͸͢΂ͯSerialʹBackgroundContextͰߦΘΕΔͨΊɺ
 ҆શʹಈ࡞͢Δઃܭ • ෳ਺ͷSQLiteϑΝΠϧ͕࢖༻Մೳ (ConfigurationͰ෼͚Δ) • Observer͕༻ҙ͞Ε͍ͯΔ (FRCΛॻ͔ͳͯ͘ྑ͘ͳΔ) • ϚΠάϨʔγϣϯΛαϙʔτ

Slide 14

Slide 14 text

Usage

Slide 15

Slide 15 text

DataStack struct Stack { static let data: DataStack = { let dataStack = DataStack(modelName: "CoreData") dataStack.addSQLiteStoreAndWait( "CoreData.sqlite", configuration: nil, resetStoreOnMigrationFailure: true ) return dataStack }() } SQLiteʹඥͮ͘DataStackΛੜ੒͠·͢

Slide 16

Slide 16 text

struct Stack { static let dataA: DataStack = { let dataStack = DataStack(modelName: "StackSetupDemo") dataStack.addSQLiteStoreAndWait( "CoreData.sqlite", configuration: "Configuration_A", resetStoreOnMigrationFailure: true ) return dataStack }() static let dataB: DataStack = {...} static let dataC: DataStack = {...} } Configuration͝ͱʹDataStackͷੜ੒͕Մೳ ࠓճ͸ConfigurationΛ࢖Θͳ͍ํ๏ͰਐΊ͍͖ͯ·͢ɻ

Slide 17

Slide 17 text

let people = Stack.data.fetchAll( From(MyPersonEntity), Where("age > 30"), OrderBy(.Ascending("name"), .Descending("age")), Tweak { (fetchRequest) -> Void in fetchRequest.includesPendingChanges = false } ) Fetch NSSortDescriptor, NSPredicate͸࢖Θͣʹࢦఆ
 ࡉ͔͍ઃఆ͸TweakͰࢦఆՄೳ

Slide 18

Slide 18 text

Stack.data.beginAsynchronous { (transaction) -> Void in let qrcode = transaction.create(Into(QRCode)) let codes = transaction.fetch( . . . ) transaction.commit() { result in switch result { case .Success(let hasChanges): success?() case .Failure(let error): failure?(error: error) } } } Create - Update

Slide 19

Slide 19 text

ViewController Fetch - Observe

Slide 20

Slide 20 text

Fetch

Slide 21

Slide 21 text

Controller͔ΒCoreDataΛ࢖͏࣌ͬͯ

Slide 22

Slide 22 text

Controller͔ΒCoreDataΛ࢖͏࣌ͬͯ NSFetchedResultsController ?

Slide 23

Slide 23 text

Controller͔ΒCoreDataΛ࢖͏࣌ͬͯ

Slide 24

Slide 24 text

Controller͔ΒCoreDataΛ࢖͏࣌ͬͯ ManagedObjectListController

Slide 25

Slide 25 text

var codes: ManagedObjectListController = { return Stack.data.observeObjectList( From(QRCode), Where("removed == false”), OrderBy(.Descending("readAt")) ) }() Controllerʹදࣔ͢ΔΦϒδΣΫτͷ৚݅Λࢦఆ

Slide 26

Slide 26 text

codes.numberOfSections() Sectionͷ਺Λऔಘ

Slide 27

Slide 27 text

codes.numberOfObjectsInSection(section) Section͔ΒObjectͷ਺Λऔಘ

Slide 28

Slide 28 text

let code = codes[indexPath] NSIndexPath͔ΒObjectΛऔಘ

Slide 29

Slide 29 text

Observe

Slide 30

Slide 30 text

codes.removeObserver(self) codes.addObserver(self) Observe Observeͷ։࢝ Observeͷऴྃ

Slide 31

Slide 31 text

protocol ManagedObjectListChangeObserver • WillChange • Insert • Delete • Update • Move • InsertSection • deleteSection • DidChange GenericsͰ࣮૷͞Ε͍ͯΔͨΊɺas ͕ෆཁ

Slide 32

Slide 32 text

Conclusion • DataStackΛϕʔεʹಈ࡞͢Δ • Fetchͷهड़͕γϯϓϧ • DataStack͝ͱʹγϦΞϧΩϡʔͰಈ࡞͢ΔͨΊ҆શ • ViewControllerͱͷ࿈ܞ͕؆୯ • ϚΠάϨʔγϣϯ͕ॆ࣮ 
 ΠϯΫϦϝϯλϧʹϚΠάϨʔγϣϯ͢Δ͜ͱ΋Մೳ • GenericsͰ࣮૷͞Ε͍ͯΔͨΊμΠφϛοΫΩϟετ͕ݮΒͤΔ

Slide 33

Slide 33 text

Swift2.0

Slide 34

Slide 34 text

Thank you :)

Slide 35

Slide 35 text

Thank you :)