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

Modern Core Data(iOS 10)

Noritaka Kamiya
September 28, 2016

Modern Core Data(iOS 10)

Noritaka Kamiya

September 28, 2016
Tweet

More Decks by Noritaka Kamiya

Other Decks in Programming

Transcript

  1. import CoreData
    ࠷ۙɺCore DataͲ͏Ͱ͔͢?
    @nolili
    Noritaka Kamiya

    View full-size slide

  2. NSAsynchronousFetchRequest
    NSFetchRequest
    NSPersistentContainer

    View full-size slide

  3. NSFetchRequest

    View full-size slide

  4. let request = NSFetchRequest(entityName: "Message")
    context.fetch(request) as! [Message]
    Force Casting!
    Before

    View full-size slide

  5. let request: NSFetchRequest = Message.fetchRequest()
    // fetchRequest() -> NSFetchRequest ͸ࣗಈੜ੒͞ΕΔ
    let messages = context.fetch(request) // [Message]

    After

    View full-size slide

  6. NSFetchRequest
    NSManagedObjectͷαϒΫϥεΛfetch͢Δ৔߹ʹศར
    SwiftϑϨϯυϦʔʹ
    Swift/Objective-C

    View full-size slide

  7. NSAsynchronousFetchRequest

    View full-size slide

  8. let request: NSFetchRequest = Message.fetchRequest()
    let asyncRequest = NSAsynchronousFetchRequest(fetchRequest: request)
    { result in
    let messages = messages = result.finalResult ?? []
    }
    try! managedObjectContext.execute(asyncRequest)

    View full-size slide

  9. let result: NSAsynchronousFetchResult =
    try! managedObjectContext.execute(asyncRequest)
    result.cancel()

    View full-size slide

  10. NSAsynchronousFetchRequest
    closureΛ࢖ͬͨඇಉظϑΣονָ͕ʹॻ͚Δ
    Ωϟϯηϧ͕Ͱ͖Δ
    ϑΣονͷProgress΋औಘͰ͖Δ
    iOS 8~

    View full-size slide

  11. NSPersistentContainer

    View full-size slide

  12. Α͋͘ΔCore DataͷηοτΞοϓ

    View full-size slide

  13. /41FSTJTUFOU4UPSF

    View full-size slide

  14. /4.BOBHFE0CKFDU.PEFM
    /41FSTJTUFOU4UPSF

    View full-size slide

  15. /41FSTJTUFOU4UPSF$PPSEJOBUPS /4.BOBHFE0CKFDU.PEFM
    /41FSTJTUFOU4UPSF

    View full-size slide

  16. /41FSTJTUFOU4UPSF$PPSEJOBUPS /4.BOBHFE0CKFDU.PEFM
    /4.BOBHFE0CKFDU$POUFYU
    /41FSTJTUFOU4UPSF

    View full-size slide

  17. /41FSTJTUFOU4UPSF$PPSEJOBUPS /4.BOBHFE0CKFDU.PEFM
    /4.BOBHFE0CKFDU$POUFYU
    /4.BOBHFE0CKFDU$POUFYU
    1SJWBUF2VFVF

    /41FSTJTUFOU4UPSF

    View full-size slide

  18. NSPersistentContainer

    View full-size slide

  19. /41FSTJTUFOU$POUBJOFS NSPersistentContainer(name: "iOS10Sampler")

    View full-size slide

  20. /41FSTJTUFOU4UPSF$PPSEJOBUPS
    /4.BOBHFE0CKFDU.PEFM
    /4.BOBHFE0CKFDU$POUFYU
    /4.BOBHFE0CKFDU$POUFYU
    1SJWBUF2VFVF

    /41FSTJTUFOU4UPSF
    NSPersistentContainer(name: "iOS10Sampler")
    /41FSTJTUFOU$POUBJOFS

    View full-size slide

  21. It just works

    View full-size slide

  22. NSPersistentContainer (iOS 10~)
    Α͋͘ΔCoreDataͷηοτΞοϓΛ؆ུԽ
    طʹ࢖͍ͬͯΔਓʹ͸౰ͨΓલ͔΋͠Εͳ͍͚Ͳͱ͖ͬͭʹ͘͞͸؇࿨͞Ε͍ͯΔ
    ۜͷ஄ؙͰ͸ͳ͍ɻCoreDataΛ࢖͍͜ͳͨ͢Ίʹ֮͑ͳ͚Ε͹͍͚ͳ͍͜ͱ͸ଟ͍ɻ
    NSPersistentStoreDescription Λ࢖͑͹ࡉ͔ͳઃఆมߋ΋Մ

    View full-size slide

  23. References
    iOS 10 Sampler
    https://github.com/shu223/iOS-10-Sampler
    What’s new in Core Data
    https://developer.apple.com/videos/play/wwdc2016/242/
    SwiftʴCore DataʹΑΔiOSΞϓϦϓϩάϥϛϯά (੢ํՆࢠɺ ؙࢁ߂ࢻ)
    https://book.mynavi.jp/ec/products/detail/id=50729

    View full-size slide

  24. https://www.wantedly.com/projects/61105

    View full-size slide

  25. import CoreData
    ࠷ۙɺCore DataͲ͏Ͱ͔͢?
    @nolili
    Noritaka Kamiya

    View full-size slide