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

New Library for CoreData

Muukii
July 13, 2015

New Library for CoreData

Muukii

July 13, 2015
Tweet

More Decks by Muukii

Other Decks in Programming

Transcript

  1. New Library for CoreData Hiroshi Kimura potatotips #19

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

  3. None
  4. None
  5. None
  6. CoreStore https://github.com/JohnEstropia/CoreStore

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

    @JohnEstropia
  8. Good things :)

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

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

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

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

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

    ෳ਺ͷSQLiteϑΝΠϧ͕࢖༻Մೳ (ConfigurationͰ෼͚Δ) • Observer͕༻ҙ͞Ε͍ͯΔ (FRCΛॻ͔ͳͯ͘ྑ͘ͳΔ) • ϚΠάϨʔγϣϯΛαϙʔτ
  14. Usage

  15. DataStack struct Stack { static let data: DataStack = {

    let dataStack = DataStack(modelName: "CoreData") dataStack.addSQLiteStoreAndWait( "CoreData.sqlite", configuration: nil, resetStoreOnMigrationFailure: true ) return dataStack }() } SQLiteʹඥͮ͘DataStackΛੜ੒͠·͢
  16. 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Λ࢖Θͳ͍ํ๏ͰਐΊ͍͖ͯ·͢ɻ
  17. 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ͰࢦఆՄೳ
  18. 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
  19. ViewController Fetch - Observe

  20. Fetch

  21. Controller͔ΒCoreDataΛ࢖͏࣌ͬͯ

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

  23. Controller͔ΒCoreDataΛ࢖͏࣌ͬͯ

  24. Controller͔ΒCoreDataΛ࢖͏࣌ͬͯ ManagedObjectListController

  25. var codes: ManagedObjectListController<QRCode> = { return Stack.data.observeObjectList( From(QRCode), Where("removed ==

    false”), OrderBy(.Descending("readAt")) ) }() Controllerʹදࣔ͢ΔΦϒδΣΫτͷ৚݅Λࢦఆ
  26. codes.numberOfSections() Sectionͷ਺Λऔಘ

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

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

  29. Observe

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

  31. protocol ManagedObjectListChangeObserver • WillChange • Insert • Delete • Update

    • Move • InsertSection • deleteSection • DidChange GenericsͰ࣮૷͞Ε͍ͯΔͨΊɺas ͕ෆཁ
  32. Conclusion • DataStackΛϕʔεʹಈ࡞͢Δ • Fetchͷهड़͕γϯϓϧ • DataStack͝ͱʹγϦΞϧΩϡʔͰಈ࡞͢ΔͨΊ҆શ • ViewControllerͱͷ࿈ܞ͕؆୯ •

    ϚΠάϨʔγϣϯ͕ॆ࣮ 
 ΠϯΫϦϝϯλϧʹϚΠάϨʔγϣϯ͢Δ͜ͱ΋Մೳ • GenericsͰ࣮૷͞Ε͍ͯΔͨΊμΠφϛοΫΩϟετ͕ݮΒͤΔ
  33. Swift2.0

  34. Thank you :)

  35. Thank you :)