Created
July 31, 2016 09:55
-
-
Save secwang/48f4deaa32ede5acfcb9e6aa7ee28577 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //: Playground - noun: a place where people can play | |
| import Cocoa | |
| import CoreData | |
| import XCPlayground | |
| var str = "Hello, playground" | |
| struct Entity { | |
| static let user = "user" | |
| static let entry = "entry" | |
| } | |
| struct attribute { | |
| static let name = "name" | |
| static let email = "email" | |
| static let time = "time" | |
| static let duration = "duration" | |
| } | |
| struct relationship { | |
| static let user = "user" | |
| static let entry = "entry" | |
| } | |
| let model = NSManagedObjectModel() | |
| let userEntity = NSEntityDescription() | |
| userEntity.name = Entity.user | |
| let entryEntity = NSEntityDescription() | |
| entryEntity.name = Entity.entry | |
| let nameAttribute = NSAttributeDescription() | |
| nameAttribute.name = attribute.name | |
| nameAttribute.attributeType = NSAttributeType.StringAttributeType | |
| nameAttribute.optional = false | |
| nameAttribute.indexed = false | |
| let emailAttribute = NSAttributeDescription() | |
| emailAttribute.name = attribute.email | |
| emailAttribute.attributeType = NSAttributeType.StringAttributeType | |
| emailAttribute.optional = false | |
| emailAttribute.indexed = false | |
| let timeAttribute = NSAttributeDescription() | |
| timeAttribute.name = attribute.time | |
| timeAttribute.attributeType = NSAttributeType.StringAttributeType | |
| timeAttribute.optional = false | |
| timeAttribute.indexed = false | |
| let durationAttribute = NSAttributeDescription() | |
| durationAttribute.name = attribute.duration | |
| durationAttribute.attributeType = NSAttributeType.StringAttributeType | |
| durationAttribute.optional = false | |
| durationAttribute.indexed = false | |
| let userRelationship = NSRelationshipDescription() | |
| let entryRelationship = NSRelationshipDescription() | |
| userRelationship.name = relationship.user | |
| userRelationship.destinationEntity = userEntity | |
| userRelationship.minCount = 0 | |
| userRelationship.maxCount = 0 | |
| userRelationship.deleteRule = NSDeleteRule.CascadeDeleteRule | |
| userRelationship.inverseRelationship = entryRelationship | |
| entryRelationship.name = relationship.entry | |
| entryRelationship.destinationEntity = entryEntity | |
| entryRelationship.minCount = 0 | |
| entryRelationship.maxCount = 0 | |
| entryRelationship.deleteRule = NSDeleteRule.CascadeDeleteRule | |
| entryRelationship.inverseRelationship = userRelationship | |
| userEntity.properties = [nameAttribute,emailAttribute,userRelationship] | |
| entryEntity.properties = [nameAttribute,timeAttribute,durationAttribute,entryRelationship] | |
| model.entities = [userEntity, entryEntity] | |
| let persistentStoreCoordinator = NSPersistentStoreCoordinator(managedObjectModel:model) | |
| let url = NSURL(string: "db.sqlite") | |
| // | |
| // | |
| //persistentStoreCoordinator.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: url, options: nil) | |
| // | |
| // | |
| do { | |
| try persistentStoreCoordinator.addPersistentStoreWithType(NSInMemoryStoreType, configuration: nil, URL: nil, options: nil) | |
| } | |
| catch { | |
| print("error creating psc: \(error)") | |
| } | |
| let managedObjectContext = NSManagedObjectContext(concurrencyType: NSManagedObjectContextConcurrencyType.MainQueueConcurrencyType) | |
| managedObjectContext.persistentStoreCoordinator = persistentStoreCoordinator | |
| let user = NSEntityDescription.insertNewObjectForEntityForName(Entity.user, inManagedObjectContext: managedObjectContext) | |
| user.setValue("sec", forKeyPath: attribute.name) | |
| user.setValue("[email protected]", forKeyPath: attribute.email) | |
| let entry = [[attribute.name:"lunch", attribute.time: "15m",attribute.duration:"20m"]] | |
| for obj in entry { | |
| var entry = NSEntityDescription.insertNewObjectForEntityForName(Entity.entry, inManagedObjectContext: managedObjectContext) | |
| entry.setValue(obj[attribute.name], forKeyPath: attribute.name) | |
| entry.setValue(obj[attribute.time], forKeyPath: attribute.time) | |
| entry.setValue(obj[attribute.duration], forKeyPath: attribute.duration) | |
| } | |
| class NotificationListener: NSObject { | |
| func handleDidSaveNotification(notification:NSNotification) { | |
| print("did save notification received: \(notification)") | |
| } | |
| } | |
| let delegate = NotificationListener() | |
| NSNotificationCenter.defaultCenter().addObserver(delegate, selector: "handleDidSaveNotification:", name: NSManagedObjectContextDidSaveNotification, object: nil) | |
| do { | |
| try managedObjectContext.save() | |
| } | |
| catch { | |
| print("error saving context: \(error)") | |
| } | |
| var fetchRequest = NSFetchRequest(entityName: Entity.user) | |
| var results: [NSManagedObject] = [] | |
| do { | |
| results = try managedObjectContext.executeFetchRequest(fetchRequest) as! [NSManagedObject] | |
| } | |
| catch { | |
| print("error executing fetch request: \(error)") | |
| } | |
| print(results[0].valueForKey(attribute.email)) | |
| let column = NSTableColumn(identifier: "Name") | |
| column.width = 300 | |
| let tableView = NSTableView(frame: CGRect(x: 0, y: 0, width: 230, height: 300)) | |
| tableView.addTableColumn(column) | |
| tableView.usesAlternatingRowBackgroundColors = true | |
| let arrayController = NSArrayController() | |
| arrayController.managedObjectContext = managedObjectContext | |
| arrayController.entityName = Entity.entry | |
| arrayController.automaticallyPreparesContent = false | |
| arrayController.sortDescriptors = [NSSortDescriptor(key: "name", ascending: true)] | |
| column.bind(NSValueBinding, toObject: arrayController, withKeyPath: "arrangedObjects.time", options: nil) | |
| try arrayController.fetchWithRequest(nil, merge: false) | |
| XCPlaygroundPage.currentPage.liveView = tableView | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment