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

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

    ෳ਺ͷSQLiteϑΝΠϧ͕࢖༻Մೳ (ConfigurationͰ෼͚Δ) • Observer͕༻ҙ͞Ε͍ͯΔ (FRCΛॻ͔ͳͯ͘ྑ͘ͳΔ) • ϚΠάϨʔγϣϯΛαϙʔτ
  3. DataStack struct Stack { static let data: DataStack = {

    let dataStack = DataStack(modelName: "CoreData") dataStack.addSQLiteStoreAndWait( "CoreData.sqlite", configuration: nil, resetStoreOnMigrationFailure: true ) return dataStack }() } SQLiteʹඥͮ͘DataStackΛੜ੒͠·͢
  4. 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Λ࢖Θͳ͍ํ๏ͰਐΊ͍͖ͯ·͢ɻ
  5. 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ͰࢦఆՄೳ
  6. 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
  7. var codes: ManagedObjectListController<QRCode> = { return Stack.data.observeObjectList( From(QRCode), Where("removed ==

    false”), OrderBy(.Descending("readAt")) ) }() Controllerʹදࣔ͢ΔΦϒδΣΫτͷ৚݅Λࢦఆ
  8. protocol ManagedObjectListChangeObserver • WillChange • Insert • Delete • Update

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

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