|
9 | 9 | import Foundation |
10 | 10 | import CoreData |
11 | 11 |
|
| 12 | +// MODEL |
12 | 13 | class Item: NSManagedObject { |
| 14 | + |
| 15 | + @NSManaged var name: String |
| 16 | + |
| 17 | + /// Function to initialize a new Item |
| 18 | + convenience init(name: String, inManagedObjectContext managedObjectContext: NSManagedObjectContext) { |
| 19 | + let entity = NSEntityDescription.entityForName("Item", inManagedObjectContext: managedObjectContext)! |
| 20 | + self.init(entity: entity, insertIntoManagedObjectContext: managedObjectContext) |
| 21 | + self.name = name |
| 22 | + } |
| 23 | + |
| 24 | + /// Function to get all CoreData values |
| 25 | + /// |
| 26 | + /// :param: managedObjectContext CoreData Connection |
| 27 | + /// |
| 28 | + class func fetchAll(managedObjectContext: NSManagedObjectContext) -> [Item] { |
| 29 | + let listagemCoreData = NSFetchRequest(entityName: "Item") |
| 30 | + |
| 31 | + // Sort alphabetical by field "name" |
| 32 | + let orderByName = NSSortDescriptor(key: "name", ascending: true, selector: "caseInsensitiveCompare:") |
| 33 | + listagemCoreData.sortDescriptors = [orderByName] |
| 34 | + |
| 35 | + // Get items from CoreData |
| 36 | + return managedObjectContext.executeFetchRequest(listagemCoreData, error: nil) as? [Item] ?? [] |
| 37 | + } |
| 38 | + |
| 39 | + /// Function to search item by name |
| 40 | + /// |
| 41 | + /// :param: name Item name |
| 42 | + /// :param: managedObjectContext CoreData Connection |
| 43 | + /// |
| 44 | + class func search(name: String, inManagedObjectContext managedObjectContext: NSManagedObjectContext) -> Item? { |
| 45 | + let fetchRequest = NSFetchRequest(entityName: "Item") |
| 46 | + fetchRequest.predicate = NSPredicate(format: "name = %@", name) |
| 47 | + |
| 48 | + let result = managedObjectContext.executeFetchRequest(fetchRequest, error: nil) as? [Item] |
| 49 | + return result?.first |
| 50 | + } |
| 51 | + |
| 52 | + /// Function to check duplicate item |
| 53 | + /// |
| 54 | + /// :param: name Item name |
| 55 | + /// :param: managedObjectContext CoreData Connection |
| 56 | + /// |
| 57 | + class func checkDuplicate(name: String, inManagedObjectContext managedObjectContext: NSManagedObjectContext) -> Bool { |
| 58 | + return search(name, inManagedObjectContext: managedObjectContext) != nil |
| 59 | + } |
13 | 60 |
|
14 | | - @NSManaged var name: String |
15 | | - |
| 61 | + /// Function to delete a item |
| 62 | + /// |
| 63 | + /// :param: managedObjectContext CoreData Connection |
| 64 | + /// |
| 65 | + func destroy(managedObjectContext: NSManagedObjectContext) { |
| 66 | + managedObjectContext.deleteObject(self) |
| 67 | + } |
| 68 | + |
| 69 | + /// Function to save CoreData values |
| 70 | + /// |
| 71 | + /// :param: managedObjectContext CoreData Connection |
| 72 | + /// |
| 73 | + func save(managedObjectContext: NSManagedObjectContext) { |
| 74 | + var error: NSErrorPointer = nil |
| 75 | + |
| 76 | + if(managedObjectContext.save(error)) { |
| 77 | + if (error != nil) { |
| 78 | + println("Error on save: \(error.debugDescription)") |
| 79 | + } |
| 80 | + } |
| 81 | + } |
16 | 82 | } |
0 commit comments