O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Learning Core Data for iOS with Swift: A Hands-On Guide to Building Core Data Applications, Second Edition

Book Description

Get Started Fast with Core Data App Development Using iOS 9, Swift, and Xcode 7

Core Data is a remarkably mature, stable, and fast platform for data access, and Swift is a world-class language for applying it. Now, there’s a complete guide to using Core Data and Swift together in production apps. Tim Roadley shows you how to gain the benefits of a relational database without writing SQL queries, so you can get more done faster, with less coding.

This book fully reflects Apple’s latest iOS 9 platform innovations and teaches Core Data entirely with Swift examples. It guides you step-by-step through creating a modern data-driven iOS app that fully integrates iCloud via CloudKit for public data sharing.

Roadley introduces up-to-date patterns and best practices designed to overcome the frustrations of Core Data development. Each chapter builds on the last, introducing new topics in the order you’ll implement them and extending your skills simply and intuitively.

Each chapter offers downloadable project code, along with exercises to help you explore even further, either as a self-learner or a student in an iOS development course. Roadley even shows how to build helper classes that simplify reuse of his example code.

If you’re an experienced iOS developer, here are all the Swift skills and resources you need to integrate data into any app—quickly, easily, and painlessly.

Coverage includes

  • Understanding what Core Data is and what it can (and can’t) do

  • Configuring basic managed object models, and choosing data types

  • Expanding data models without introducing errors

  • Using relationships and entity inheritance to unlock more power

  • Delivering memory-efficient, high performance table views

  • Enabling users to easily modify managed object attributes

  • Generating persistent stores of preloaded default data

  • Using Deep Copy to copy objects and relationships between persistent stores

  • Optimizing performance by eliminating bottlenecks and offloading intensive tasks to the background

  • Implementing efficient search

  • Integrating diverse iCloud accounts and preferences

  • Mastering advanced iCloud integration, including entity-level seeding and unique object de-dupe

  • Leveraging public CloudKit databases to sync data across users with different iCloud accounts

  • About the Website

    All code samples are available for download at timroadley.com.


    Table of Contents

    1. About This E-Book
    2. Title Page
    3. Copyright Page
    4. Dedication Page
    5. Contents at a Glance
    6. Table of Contents
    7. Acknowledgments
    8. About the Author
    9. Preface
      1. Who Is This Book For?
      2. What You’ll Need
      3. How This Book Is Organized
      4. Getting the Sample Code
      5. Conventions Used in This Book
        1. Code Breaks
        2. Sample Code Updates
      6. We Want to Hear from You!
    10. 1. Your First Core Data Application
      1. What Is Core Data?
        1. Persistent Store Coordinator
        2. Managed Object Model
        3. Managed Object Context
      2. When to Use Core Data
      3. Introducing the Groceries Application
      4. Adding Core Data to an Existing Application
        1. Implementing the Core Data Helper
        2. Adding Paths
        3. Adding the Core Data Stack
        4. Adding the Setup Section
        5. Adding the Saving Section
      5. Adding a Managed Object Model File
      6. Summary
      7. Exercises
    11. 2. Managed Object Model Basics
      1. What Is a Managed Object Model?
      2. Entities
      3. Attributes
      4. Integer 16/32/64
      5. Float and Double
        1. Decimal
        2. String
        3. Boolean
        4. Date
        5. Binary Data
        6. Transformable
      6. Attribute Settings
      7. Subclassing NSManagedObject
      8. Scalar Properties for Primitive Data Types
      9. Introducing the Demo Function
      10. Creating a Managed Object
      11. Backend SQL Visibility
      12. Fetching Managed Objects
        1. Sorting Fetch Requests
        2. Filtering Fetch Requests
        3. Fetch Request Templates
      13. Deleting Managed Objects
      14. Summary
      15. Exercises
    12. 3. Managed Object Model Migration
      1. Changing a Managed Object Model
      2. Adding a Model Version
      3. Lightweight Migration
      4. Default Migration
      5. Migration Manager
        1. Introducing MigrationVC.swift (Migration View Controller Code)
        2. Introducing CDMigration.swift (Core Data Migration Code)
      6. Summary
      7. Exercises
    13. 4. Managed Object Model Expansion
      1. Relationships
      2. Delete Rules
        1. Introducing CDOperation.swift (Core Data Operation)
        2. Delete Rule Testing
      3. Entity Inheritance
      4. Summary
      5. Exercises
    14. 5. Table Views
      1. Table View Basics
      2. Core Data Table Views
      3. Introducing CDTableViewController
        1. Expanding CDTableViewController
        2. DATASOURCE: UITableView
        3. DELEGATE: NSFetchedResultsController
        4. CDTableViewController Subclasses
      4. Enhancing PrepareTVC
        1. Preparing Test Data
        2. Deletion and Cell Selection for PrepareTVC
        3. Interaction for PrepareTVC
      5. Introducing ShopTVC
        1. Cell Selection for ShopTVC
        2. INTERACTION for ShopTVC
      6. Summary
      7. Exercises
    15. 6. Views
      1. View Basics
      2. The Target View Hierarchy
      3. Introducing ItemVC
        1. Keeping Reference to a Selected Item
        2. Passing a Selected Item to ItemVC
        3. Configuring the Scroll View and Text Fields
        4. Implementing ItemVC
        5. DELEGATE: UITextField
        6. The VIEW Section
        7. Adding Functionality to NSManagedObject Subclasses
      4. Units, Home Locations, and Shop Locations
        1. Adding and Editing Units
        2. Implementing UnitsTVC
        3. Implementing UnitVC
        4. Segueing from UnitsTVC to UnitVC
        5. Adding and Editing Home or Shop Locations
        6. Configuring the Home Location Views
        7. Configuring the Shop Location Views
      5. Summary
      6. Exercises
    16. 7. Picker Views
      1. Picker View Basics
      2. Introducing CDPickerTextField
      3. Introducing UnitPickerTF
        1. Creating the Unit Picker
        2. Connecting the Unit Picker
        3. Configuring ItemVC for the Unit Picker
      4. Introducing LocationAtHomePickerTF
      5. Introducing LocationAtShopPickerTF
      6. Using the Location Pickers
        1. Connecting the Location Pickers
        2. Configuring ItemVC for the Location Pickers
      7. Picker-Avoiding Text Field
      8. Summary
      9. Exercises
    17. 8. Preloading Data
      1. Including Default Data
      2. Indicating Whether an Import Is Required
      3. Importing from XML
      4. Creating an Import Context
      5. Preventing Duplicate Default Data
      6. Triggering a Default Data Import
      7. Finding or Creating Managed Objects
      8. Mapping XML Data to Entity Attributes
      9. Importing from a Persistent Store
        1. Using the Default Data Store as the Initial Store
      10. Summary
      11. Exercises
    18. 9. Deep Copy
      1. The Deep Copy Process
      2. Configuring a Source Stack
        1. Configuring the Source Coordinator
        2. Configuring the Source Context
        3. Configuring the Source Store
      3. Enhancing CDImporter
        1. Identifying Unique Attributes
        2. Object Info
        3. Copying a Unique Object
        4. Establishing a To-One Relationship
        5. Establishing a To-Many Relationship
        6. Establishing an Ordered To-Many Relationship
        7. Copying Relationships
        8. Deep Copy Entities
      4. Triggering a Deep Copy
      5. Summary
      6. Exercises
    19. 10. Performance
      1. Identifying Performance Issues
      2. Implementing the Camera
        1. Implementing the Image Picker Controller Delegates
      3. Generating Test Data
        1. Merge Policies
      4. Measuring Performance with SQLDebug
      5. Measuring Performance with Instruments
      6. Improving Performance
        1. Model Optimization
        2. Handling Large Objects
      7. Cleaning Up
      8. Summary
      9. Exercises
    20. 11. Background Processing
      1. Implementing Background Save
      2. Configuring an Import Context Parent
      3. Faulting Objects
      4. Generating Thumbnails
      5. Summary
      6. Exercises
    21. 12. Search
      1. Updating CDTableViewController
      2. Updating PrepareTVC
      3. Search Optimization
      4. Summary
      5. Exercises
    22. 13. iCloud
      1. iCloud Basics
      2. Enabling iCloud
      3. Updating CDHelper for iCloud
        1. Adding an iCloud Store
        2. Handling iCloud Notifications
      4. The Debug Navigator
      5. Summary
      6. Exercises
    23. 14. Taming iCloud
      1. De-Duplication
        1. Identifying Duplicates
        2. Deleting Duplicates
        3. Triggering De-duplication
      2. Seeding
        1. Preparing Seed Variables
        2. Adding Seed Helper Functions
      3. Developing with a Clean Slate
      4. Configurations
      5. Summary
      6. Exercises
    24. 15. CloudKit Sync: Uploading Objects
      1. Introducing CloudKit
      2. CloudKit Database Synchronization Limitations
      3. Introducing CDCloudSync
      4. Public Data Groups
      5. Cache Status
      6. CloudKit Building Blocks
      7. Uploading New Records and Relationships
      8. Adding Synchronization Logic
      9. Preparing the Managed Object Model for Sync
      10. Summary
      11. Exercises
    25. 16. CloudKit Sync: Downloading Changes and Handling Deletions
      1. CloudKit Building Blocks (Continued)
      2. Change Synchronization
      3. Deletion Synchronization
      4. Quality Assurance
      5. Testing the Network
      6. Updating Synchronization Logic
      7. Adding CDCloudSync to Your Own Applications
      8. Summary
      9. Exercises
      10. Thank You!
    26. A. Preparing the Groceries Application
      1. New Xcode Project
      2. Storyboard Design
      3. App Icons and Launch Images
      4. Introducing GenericVC
    27. B. Finalizing the Groceries Application
      1. New Features
      2. Photo Library and Photo Deletion Support
      3. Favorites
      4. Icon Badge
      5. Location De-Duplication Logic
    28. Code Snippets