Giter Club home page Giter Club logo

swiftcoredatasimpledemo's Introduction

Step by Step Do iOS Swift CoreData Simple Demo

This is a simple demo of access iOS core data in swift.

Tested in Xcode7.2.1 and Swift 2 (2016-02-14)

Support multi-thread Contexts

Florian Kugler gaved some advices for core data performance, Concurrent Core Data Stacks – Performance Shootout and Backstage with Nested Managed Object Contexts.

I changed the implements to support this best practice. We should better write data in private background context, and read data from main queue context.

Step by Step

Common Steps

  1. Create an Empty Application, and select in Swift language and use CoreData.
  2. Modify generated code about persistentStoreCoordinator in AppDelegate.swift to CoreDataStore.
  3. Modify generated code about NSManagedObjectContext in AppDelegate.swift to CoreDataHelper.
  4. Adjust AppDelegate.swift to use CoreDataStore and CoreDataHelper.

Data Models

  1. Create some entity in SwiftCoreDataSimpleDemo.xcdatamodeld, such as Family and Member.
  2. Add "members" attribute of Family, set relationship with Member/family, and "to Many", select delete rule as "Cascaded".
  3. Add "family" attribute of Member, set relationship with Family/members, and "to One", unselect the checkbox of "Options".
  4. Generate the NSManagedObject files in Swift.
  5. You have to add sentences like @objc(ClassName) to Member.swift and Family.swift manually. (Thanks for joshhinman's contributions.)

such as:

import CoreData

@objc(Member)
class Member: NSManagedObject {
    @NSManaged var name: String
    @NSManaged var sex: String
    @NSManaged var birthday: NSDate
}

About Demo Code

  1. Add func demoFamily() to AppDelegate.swift, this is a demo of basic CoreData CRUD.
  2. Add func demoMember() to AppDelegate.swift, this is a demo of relationship, and delete cascaded.
  3. If you want to use CoreDataHelper in UIViewController, you'd better add a CoreDataHelper instance for each UIViewController. All CoreDataHelper instance share one CoreDataStore defined in AppDelegate.swift.
  4. Generally, We should better write data in private background context, and read data from main queue context.
  5. BUT please make sure the context of saved the object is same with which fetched it.

Other

There are a detail documents in Chinese at here. Or you can read the pictures and the codes. :)


Author : iascchen(at)gmail(dot)com

Date : 2014-8-28

新浪微博 : @问天鼓


swiftcoredatasimpledemo's People

Contributors

iascchen avatar joshhinman avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.