-
Notifications
You must be signed in to change notification settings - Fork 27
NSManagedObject
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
} 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 = fireTypeIf 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 NSManagedObjectOr using a NSPredicate
let predicate = NSPredicate(format: "%K = %@", "name","Charizard")
let anotherCharizard = Pokemon.findFirstOrCreateWithPredicate(predicate, context: managedObjectContext) as PokemonSuperRecord makes NSManagedObject deletion very simple
Pokemon.deleteAll(context: managedObjectContext); var predicate = NSPredicate (format: "level == %d", 36)
Pokemon.deleteAll(predicate, context: managedObjectContext)Pokemon.findAll(context: managedObjectContext)// 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]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]SuperRecord support different operation:
- [Count] (#count)
- [Sum] (#summinmaxavg)
- [Max] (#summinmaxavg)
- [Min] (#summinmaxavg)
- [Avg] (#summinmaxavg)
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)
// 2Even 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,25Passing 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.