New Library for CoreData

Ca8210ff0ece2bb6f9fff5fd0770ea64?s=47 Muukii
July 13, 2015

New Library for CoreData

Ca8210ff0ece2bb6f9fff5fd0770ea64?s=128

Muukii

July 13, 2015
Tweet

Transcript

  1. 3.
  2. 4.
  3. 5.
  4. 12.

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

    ෳ਺ͷSQLiteϑΝΠϧ͕࢖༻Մೳ (ConfigurationͰ෼͚Δ) • Observer͕༻ҙ͞Ε͍ͯΔ (FRCΛॻ͔ͳͯ͘ྑ͘ͳΔ)
  5. 13.

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

    ෳ਺ͷSQLiteϑΝΠϧ͕࢖༻Մೳ (ConfigurationͰ෼͚Δ) • Observer͕༻ҙ͞Ε͍ͯΔ (FRCΛॻ͔ͳͯ͘ྑ͘ͳΔ) • ϚΠάϨʔγϣϯΛαϙʔτ
  6. 14.
  7. 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Λੜ੒͠·͢
  8. 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Λ࢖Θͳ͍ํ๏ͰਐΊ͍͖ͯ·͢ɻ
  9. 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ͰࢦఆՄೳ
  10. 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
  11. 20.
  12. 25.

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

    false”), OrderBy(.Descending("readAt")) ) }() Controllerʹදࣔ͢ΔΦϒδΣΫτͷ৚݅Λࢦఆ
  13. 29.
  14. 31.

    protocol ManagedObjectListChangeObserver • WillChange • Insert • Delete • Update

    • Move • InsertSection • deleteSection • DidChange GenericsͰ࣮૷͞Ε͍ͯΔͨΊɺas ͕ෆཁ
  15. 32.

    Conclusion • DataStackΛϕʔεʹಈ࡞͢Δ • Fetchͷهड़͕γϯϓϧ • DataStack͝ͱʹγϦΞϧΩϡʔͰಈ࡞͢ΔͨΊ҆શ • ViewControllerͱͷ࿈ܞ͕؆୯ •

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