Skip to content
Piergiuseppe Longo edited this page Mar 16, 2015 · 6 revisions

Contents

Assuming that you have the following class

@objc(Pokemon)
class Pokemon: NSManagedObject {
	@NSManaged var level: NSNumber
	@NSManaged var name: String
	@NSManaged var id: NSNumber
	@NSManaged var type: Type
}

@objc(Type)
class Type: NSManagedObject {
	@NSManaged var id: NSNumber
	@NSManaged var name: String
	@NSManaged var pokemons: NSSet
}

Entity Creation

Create an Entity

 let fireType = Type.createNewEntity(context: managedObjectContext) as Type;
 fireType.name = "Fire";

 let Charizard = Pokemon.createNewEntity(context: managedObjectContext) as Pokemon;

 Charizard.id = "4"
 Charizard.name = "Charizard"
 Charizard.level = 36
 Charizard.type = fireType

Create an Entity of find one if exists

If you think that the Entity already exists or you don't want duplicates, you can search it using one of its attributes

 let sameCharizardOfBefore = Pokemon.findFirstOrCreateWithAttribute("name", value: "Charizard", 
context: managedObjectContext) // returns the previously created Charizard
 let newCharmender = Pokemon.findFirstOrCreateWithAttribute("name", value: "Charmender", context: managedObjectContext) // returns a new NSManagedObject

Or using a NSPredicate

 let predicate =  NSPredicate(format: "%K = %@", "name","Charizard")
 let anotherCharizard  = Pokemon.findFirstOrCreateWithPredicate(predicate, context: managedObjectContext) as Pokemon

Entity Deletion

SuperRecord makes NSManagedObject deletion very simple

Delete all Entities

 Pokemon.deleteAll(context: managedObjectContext);  

Delete all Entities matching a NSPredicate

 var predicate = NSPredicate (format: "level == %d", 36)
 Pokemon.deleteAll(predicate, context: managedObjectContext)

Entity Search

Find all Entities

Pokemon.findAll(context: managedObjectContext)

Find all Entities matching one attribute

// Assuming you have previously created 4 pokemon
// Charmender -> Fire
// Charmeleon -> Fire
// Charizard -> Fire
// Blastoise -> Water

Pokemon.findAllWithAttribute("name", value: charizard.name, context: managedObjectContext)
//Returns [Charizard]
Pokemon.findAllWithAttribute("type", value: fireType, context: managedObjectContext)
//Returns [Charmender, Charmeleon, Charizard]
Pokemon.findAllWithAttribute("type.name", value: fireType.name, context: managedObjectContext)
//Returns [Charmender, Charmeleon, Charizard]

Find all Entities matching a predicate

If you have to search entities matching more than one attribute you have to use a predicate. (Take a look to PredicateBuilder)

// Assuming you have previously created 3 pokemon
var predicate = NSPredicate (format: "level == %d", 36)
Pokemon.findAllWithPredicate(predicate, context: managedObjectContext)
//Returns [Charizard]

Entity Operation

SuperRecord support different operation:

  • [Count] (#count)
  • [Sum] (#summinmaxavg)
  • [Max] (#summinmaxavg)
  • [Min] (#summinmaxavg)
  • [Avg] (#summinmaxavg)

Count

If you have to count all the entities or only entities matching a predicate in the context is made very simple and in an efficient way:

// Assuming you have previously created 4 pokemon
// Charmender -> Fire
// Charmeleon -> Fire
// Charizard -> Fire
// Blastoise -> Water
Pokemon.count(context: context, error: nil) 
// 4

let levelPredicate = NSPredicate(format: "level >= 36");
Pokemon.count(context: context, predicate: levelPredicate!,  error: nil)
// 2

Sum/Min/Max/Avg

Even if arithmetic operation aren't the main goal of CoreData, with SuperRecord you can compute the sum/min/max/avg of one or more field of an entity.

// Assuming you have previously created 4 pokemon
// Charmender -> Level 1
// Charmeleon -> Level 16
// Charizard -> Level 36
// Blastoise -> Level 36
Pokemon.sum(context:managedObjectContext, fieldName: "level")
// 89
Pokemon.sum(context:managedObjectContext, fieldName: ["level", "id" ])
// [89, 24]
Pokemon.max(context:managedObjectContext, fieldName: "level")
// 36
Pokemon.min(context:managedObjectContext, fieldName: "level")
// 1
Pokemon.avg(context:managedObjectContext, fieldName: "level")
// 22,25

Passing the field as a string the result is a Double, passing an [String] the result is a indexed array with the same order of the input strings.

Soon more operation will be added and also the possibility of groupBy fields.