Website Links

Friday, 8 May 2015

Core Data for Swift

Having core data enabled for an xCode project allows you to persist data between user sessions. There is an option you should tick when creating the project, and that is to use core data. However, if you have already created your project and made significant process without turning on this option, you can just create a new project and copy across the relevant code from AppDelegate.swift!

Defining Entities

xCode makes it excessively easy to define entities. First, you will need to open the .xcdatamodeld file and click "Add Entity", your new entity will appear under the list of entities and you can double click on it to rename it. Adding attributes can be done by pressing the "+" in the attributes section. Attributes need to start with a lowercase name, and some names are reserved such as "isDeleted". You can then select what data type that attribute will be stored as. Your finished entity should look something like this:



There is also a view which allows you to edit entities and add relationships in a UML style format. If you would prefer that change the "Editor Style" which is located in the bottom right.

Defining Relationships

You are also able to define relationships between entities. Once a relationship is created, you can view its details and set properties such as how deletes are handled. For example, when a scenario is deleted I want all stones linked to it to also be deleted, so I set the "Delete Rule" to cascade. Once you have finished defining your relationships you should should have something that looks like this:



The above diagram shows that a Stone entity has one Scenario entity, and a Scenario entity has many Stone entities.

Creating Entities


  func createScenario() {
    let appDelegate = UIApplication.sharedApplication().delegate 
         as! AppDelegate
    let context = appDelegate.managedObjectContext!
        
    //Getting entity description for entity named "Scenario"
    //and creating object to save from it     
    let scenarioEntityDescription =  NSEntityDescription
         .entityForName("Scenario", inManagedObjectContext: context)!
    var scenario =  NSManagedObject
                     (entity: scenarioEntityDescription,
                      insertIntoManagedObjectContext: context)
        
    //Setting entity's attributes.  
    scenario.setValue(nameTextField.text, forKey: "name")
    scenario.setValue(descriptionTextField.text, forKey: "scenario")
    scenario.setValue(tagTextField.text, forKey: "tag")

    //Adding stone for stones relationship.
    //Excluded setting of the "Stone" entity's attributes 
    //for brevity.
    //First step is to get a set for the "stones" 
    //relationship from the scenario.
    var stonesSet = scenario.mutableSetValueForKey("stones")
    let stoneEntityDescription = NSEntityDescription
         .entityForName("Stone", inManagedObjectContext: context)!
    var stoneEntity = NSManagedObject
                       (entity: stoneEntityDescription,
                        insertIntoManagedObjectContext: context)
    stonesSet.addObject(stoneEntity)

    //Setting the stones relationship for the scenario.
    //Saving, please note error handling was excluded.
    scenario.setValue(stonesSet, forKey: "stones")
    context.save(nil)
  }


Fetching Entities


  //Returns an NSArray of all "Scenario" entities.
  //If you want to modify this array (add/remove) then you will need
  //a NSMutableArray using scenarios.mutableCopy() as! NSMutableArray.
  func fetchScenarios() -> NSArray {
    let appDelegate = UIApplication.sharedApplication().delegate 
         as! AppDelegate
    let context = appDelegate.managedObjectContext!
        
    //Setting up request for entity named "Scenario"
    //You can also specify search constraints.     
    var request = NSFetchRequest(entityName: "Scenario")
    request.returnsObjectsAsFaults = false
        
    //Fetching all "Scenario" entities.
    //It will also load all "Stone" entities linked
    //with a "Scenario" entity by "stones" relationship.
    return context.executeFetchRequest(request, error: nil)!
  }


Updating Entities


  //Update the "tag" attribute of a given "Scenario" entity.
  func changeScenarioTag(scenario: NSManagedObject, newTag: String) {
    let appDelegate = UIApplication.sharedApplication().delegate 
         as! AppDelegate
    let context = appDelegate.managedObjectContext!
    
    //Making change to the "Scenario" entity's "tag" attribute.
    //Committing the change.
    scenario.setValue(newTag, forKey: "tag")
    context.save(nil)
  }


Deleting Entities


  func deleteScenario(scenario: NSManagedObject) {
    let appDelegate = UIApplication.sharedApplication().delegate 
         as! AppDelegate
    let context = appDelegate.managedObjectContext!
    
    //Telling context we want to delete the scenario object.
    //As we specified a cascade delete in our relationship "stones"
    //the "Stone" entities linked with the "Scenario will be deleted also.
    context.deleteObject(scenario)

    //Commiting delete.
    context.save(nil)
  }


No comments:

Post a Comment