From a1610534cbdedf69b1ac4af329e8545d2377ae65 Mon Sep 17 00:00:00 2001 From: John Rommel Estropia Date: Sat, 9 May 2015 14:10:55 +0900 Subject: [PATCH] updated demo --- .../AsynchronousDataTransaction.swift | 3 + HardcoreData/DataStack+Observing.swift | 1 - HardcoreData/DetachedDataTransaction.swift | 2 + HardcoreData/HardcoreData+Observing.swift | 22 +- .../NSManagedObjectContext+HardcoreData.swift | 6 +- HardcoreData/SynchronousDataTransaction.swift | 28 +- .../project.pbxproj | 64 ++-- .../xcshareddata/HardcoreDataDemo.xccheckout | 6 +- .../HardcoreDataDemo/AppDelegate.swift | 12 +- .../Base.lproj/Main.storyboard | 276 +++++++----------- ...ObjectListObserverDemoViewController.swift | 72 +++-- .../ObjectObserverDemoViewController.swift | 72 +++-- .../Palette.swift | 4 + .../PaletteTableViewCell.swift | 0 14 files changed, 286 insertions(+), 282 deletions(-) rename HardcoreDataDemo/HardcoreDataDemo/{ => List and Object Observers Demo}/ObjectListObserverDemoViewController.swift (71%) rename HardcoreDataDemo/HardcoreDataDemo/{ => List and Object Observers Demo}/ObjectObserverDemoViewController.swift (74%) rename HardcoreDataDemo/HardcoreDataDemo/{ => List and Object Observers Demo}/Palette.swift (98%) rename HardcoreDataDemo/HardcoreDataDemo/{ => List and Object Observers Demo}/PaletteTableViewCell.swift (100%) diff --git a/HardcoreData/AsynchronousDataTransaction.swift b/HardcoreData/AsynchronousDataTransaction.swift index 615010a..e9e0269 100644 --- a/HardcoreData/AsynchronousDataTransaction.swift +++ b/HardcoreData/AsynchronousDataTransaction.swift @@ -24,9 +24,12 @@ // import Foundation +import CoreData import GCDKit +// MARK: - AsynchronousDataTransaction + /** The AsynchronousDataTransaction provides an interface for NSManagedObject creates, updates, and deletes. A transaction object should typically be only used from within a transaction block initiated from DataStack.beginAsynchronous(_:), or from HardcoreData.beginAsynchronous(_:). */ diff --git a/HardcoreData/DataStack+Observing.swift b/HardcoreData/DataStack+Observing.swift index 592a162..3a7b882 100644 --- a/HardcoreData/DataStack+Observing.swift +++ b/HardcoreData/DataStack+Observing.swift @@ -53,7 +53,6 @@ public extension DataStack { HardcoreData.assert(GCDQueue.Main.isCurrentExecutionContext(), "Attempted to observe objects from \(typeName(self)) outside the main queue.") - // TODO: sectionNameKeyPath and cacheResults return ManagedObjectListController( dataStack: self, entity: T.self, diff --git a/HardcoreData/DetachedDataTransaction.swift b/HardcoreData/DetachedDataTransaction.swift index d46e1cd..bcbe91a 100644 --- a/HardcoreData/DetachedDataTransaction.swift +++ b/HardcoreData/DetachedDataTransaction.swift @@ -27,6 +27,8 @@ import Foundation import GCDKit +// MARK: - DetachedDataTransaction + /** The DetachedDataTransaction provides an interface for non-contiguous NSManagedObject creates, updates, and deletes. This is useful for making temporary changes, such as partially filled forms. A detached transaction object should typically be only used from the main queue. */ diff --git a/HardcoreData/HardcoreData+Observing.swift b/HardcoreData/HardcoreData+Observing.swift index df109b6..17b5cb9 100644 --- a/HardcoreData/HardcoreData+Observing.swift +++ b/HardcoreData/HardcoreData+Observing.swift @@ -2,11 +2,29 @@ // HardcoreData+Observing.swift // HardcoreData // -// Created by John Rommel Estropia on 2015/05/06. -// Copyright (c) 2015 John Rommel Estropia. All rights reserved. +// Copyright (c) 2015 John Rommel Estropia +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. // import Foundation +import CoreData // MARK: - HardcoreData diff --git a/HardcoreData/NSManagedObjectContext+HardcoreData.swift b/HardcoreData/NSManagedObjectContext+HardcoreData.swift index ff5fbb8..068fc88 100644 --- a/HardcoreData/NSManagedObjectContext+HardcoreData.swift +++ b/HardcoreData/NSManagedObjectContext+HardcoreData.swift @@ -94,8 +94,10 @@ internal extension NSManagedObjectContext { HardcoreData.handleError( error ?? NSError(hardcoreDataErrorCode: .UnknownError), - "Failed to obtain permanent ID(s) for \(numberOfInsertedObjects) inserted object(s).") - }) + "Failed to obtain permanent ID(s) for \(numberOfInsertedObjects) inserted object(s)." + ) + } + ) } diff --git a/HardcoreData/SynchronousDataTransaction.swift b/HardcoreData/SynchronousDataTransaction.swift index 2105c8d..bcef990 100644 --- a/HardcoreData/SynchronousDataTransaction.swift +++ b/HardcoreData/SynchronousDataTransaction.swift @@ -2,13 +2,37 @@ // SynchronousDataTransaction.swift // HardcoreData // -// Created by John Rommel Estropia on 2015/02/28. -// Copyright (c) 2015 John Rommel Estropia. All rights reserved. +// Copyright (c) 2015 John Rommel Estropia +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. // import Foundation +import CoreData import GCDKit + +// MARK: - SynchronousDataTransaction + +/** +The SynchronousDataTransaction provides an interface for NSManagedObject creates, updates, and deletes. A transaction object should typically be only used from within a transaction block initiated from DataStack.beginSynchronous(_:), or from HardcoreData.beginSynchronous(_:). +*/ public final class SynchronousDataTransaction: BaseDataTransaction { // MARK: Public diff --git a/HardcoreDataDemo/HardcoreDataDemo.xcodeproj/project.pbxproj b/HardcoreDataDemo/HardcoreDataDemo.xcodeproj/project.pbxproj index 1a7f25d..010ec3f 100644 --- a/HardcoreDataDemo/HardcoreDataDemo.xcodeproj/project.pbxproj +++ b/HardcoreDataDemo/HardcoreDataDemo.xcodeproj/project.pbxproj @@ -7,18 +7,18 @@ objects = { /* Begin PBXBuildFile section */ + B503FADF1AFDC71700F90881 /* ObjectListObserverDemoViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = B503FADB1AFDC71700F90881 /* ObjectListObserverDemoViewController.swift */; }; + B503FAE01AFDC71700F90881 /* ObjectObserverDemoViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = B503FADC1AFDC71700F90881 /* ObjectObserverDemoViewController.swift */; }; + B503FAE11AFDC71700F90881 /* Palette.swift in Sources */ = {isa = PBXBuildFile; fileRef = B503FADD1AFDC71700F90881 /* Palette.swift */; }; + B503FAE21AFDC71700F90881 /* PaletteTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = B503FADE1AFDC71700F90881 /* PaletteTableViewCell.swift */; }; B54AAD4F1AF4D26E00848AE0 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = B54AAD4E1AF4D26E00848AE0 /* AppDelegate.swift */; }; B54AAD521AF4D26E00848AE0 /* HardcoreDataDemo.xcdatamodeld in Sources */ = {isa = PBXBuildFile; fileRef = B54AAD501AF4D26E00848AE0 /* HardcoreDataDemo.xcdatamodeld */; }; - B54AAD541AF4D26E00848AE0 /* ObjectListObserverDemoViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = B54AAD531AF4D26E00848AE0 /* ObjectListObserverDemoViewController.swift */; }; B54AAD591AF4D26E00848AE0 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = B54AAD571AF4D26E00848AE0 /* Main.storyboard */; }; B54AAD5B1AF4D26E00848AE0 /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = B54AAD5A1AF4D26E00848AE0 /* Images.xcassets */; }; B54AAD5E1AF4D26E00848AE0 /* LaunchScreen.xib in Resources */ = {isa = PBXBuildFile; fileRef = B54AAD5C1AF4D26E00848AE0 /* LaunchScreen.xib */; }; B54AAD6A1AF4D26E00848AE0 /* HardcoreDataDemoTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B54AAD691AF4D26E00848AE0 /* HardcoreDataDemoTests.swift */; }; B583A9201AF5F542001F76AF /* HardcoreData.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = B583A91B1AF5F4F4001F76AF /* HardcoreData.framework */; }; B583A9211AF5F542001F76AF /* HardcoreData.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = B583A91B1AF5F4F4001F76AF /* HardcoreData.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; }; - B5BC26E41AF8F67900276889 /* PaletteTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5BC26E31AF8F67900276889 /* PaletteTableViewCell.swift */; }; - B5BC26E81AF8FD9600276889 /* Palette.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5BC26E71AF8FD9600276889 /* Palette.swift */; }; - B5BC271B1AFA4E6900276889 /* ObjectObserverDemoViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5BC271A1AFA4E6900276889 /* ObjectObserverDemoViewController.swift */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -74,11 +74,14 @@ /* End PBXCopyFilesBuildPhase section */ /* Begin PBXFileReference section */ + B503FADB1AFDC71700F90881 /* ObjectListObserverDemoViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ObjectListObserverDemoViewController.swift; sourceTree = ""; }; + B503FADC1AFDC71700F90881 /* ObjectObserverDemoViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ObjectObserverDemoViewController.swift; sourceTree = ""; }; + B503FADD1AFDC71700F90881 /* Palette.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Palette.swift; sourceTree = ""; }; + B503FADE1AFDC71700F90881 /* PaletteTableViewCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PaletteTableViewCell.swift; sourceTree = ""; }; B54AAD491AF4D26E00848AE0 /* HardcoreDataDemo.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = HardcoreDataDemo.app; sourceTree = BUILT_PRODUCTS_DIR; }; B54AAD4D1AF4D26E00848AE0 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; B54AAD4E1AF4D26E00848AE0 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; B54AAD511AF4D26E00848AE0 /* HardcoreDataDemo.xcdatamodel */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcdatamodel; path = HardcoreDataDemo.xcdatamodel; sourceTree = ""; }; - B54AAD531AF4D26E00848AE0 /* ObjectListObserverDemoViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ObjectListObserverDemoViewController.swift; sourceTree = ""; }; B54AAD581AF4D26E00848AE0 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; B54AAD5A1AF4D26E00848AE0 /* Images.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Images.xcassets; sourceTree = ""; }; B54AAD5D1AF4D26E00848AE0 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = Base; path = Base.lproj/LaunchScreen.xib; sourceTree = ""; }; @@ -87,9 +90,6 @@ B54AAD691AF4D26E00848AE0 /* HardcoreDataDemoTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HardcoreDataDemoTests.swift; sourceTree = ""; }; B583A9141AF5F4F3001F76AF /* HardcoreData.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = HardcoreData.xcodeproj; path = ../HardcoreData.xcodeproj; sourceTree = ""; }; B583A9251AF5F547001F76AF /* GCDKit.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; name = GCDKit.framework; path = "/Users/johnestropia/Library/Developer/Xcode/DerivedData/HardcoreDataDemo-ftknhsqfpsthfogvisxisgpbbhsj/Build/Products/Debug-iphoneos/GCDKit.framework"; sourceTree = ""; }; - B5BC26E31AF8F67900276889 /* PaletteTableViewCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PaletteTableViewCell.swift; sourceTree = ""; }; - B5BC26E71AF8FD9600276889 /* Palette.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Palette.swift; sourceTree = ""; }; - B5BC271A1AFA4E6900276889 /* ObjectObserverDemoViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ObjectObserverDemoViewController.swift; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -111,6 +111,17 @@ /* End PBXFrameworksBuildPhase section */ /* Begin PBXGroup section */ + B503FADA1AFDC71700F90881 /* List and Object Observers Demo */ = { + isa = PBXGroup; + children = ( + B503FADB1AFDC71700F90881 /* ObjectListObserverDemoViewController.swift */, + B503FADC1AFDC71700F90881 /* ObjectObserverDemoViewController.swift */, + B503FADD1AFDC71700F90881 /* Palette.swift */, + B503FADE1AFDC71700F90881 /* PaletteTableViewCell.swift */, + ); + path = "List and Object Observers Demo"; + sourceTree = ""; + }; B54AAD401AF4D26E00848AE0 = { isa = PBXGroup; children = ( @@ -135,9 +146,7 @@ isa = PBXGroup; children = ( B54AAD4E1AF4D26E00848AE0 /* AppDelegate.swift */, - B5BC26E51AF8F67D00276889 /* Cells */, - B5BC27191AFA4E0500276889 /* Demo */, - B5BC26E61AF8F68200276889 /* Entities */, + B503FADA1AFDC71700F90881 /* List and Object Observers Demo */, B54AAD571AF4D26E00848AE0 /* Main.storyboard */, B54AAD5A1AF4D26E00848AE0 /* Images.xcassets */, B54AAD5C1AF4D26E00848AE0 /* LaunchScreen.xib */, @@ -181,31 +190,6 @@ name = Products; sourceTree = ""; }; - B5BC26E51AF8F67D00276889 /* Cells */ = { - isa = PBXGroup; - children = ( - B5BC26E31AF8F67900276889 /* PaletteTableViewCell.swift */, - ); - name = Cells; - sourceTree = ""; - }; - B5BC26E61AF8F68200276889 /* Entities */ = { - isa = PBXGroup; - children = ( - B5BC26E71AF8FD9600276889 /* Palette.swift */, - ); - name = Entities; - sourceTree = ""; - }; - B5BC27191AFA4E0500276889 /* Demo */ = { - isa = PBXGroup; - children = ( - B54AAD531AF4D26E00848AE0 /* ObjectListObserverDemoViewController.swift */, - B5BC271A1AFA4E6900276889 /* ObjectObserverDemoViewController.swift */, - ); - name = Demo; - sourceTree = ""; - }; /* End PBXGroup section */ /* Begin PBXNativeTarget section */ @@ -332,12 +316,12 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( + B503FAE01AFDC71700F90881 /* ObjectObserverDemoViewController.swift in Sources */, B54AAD521AF4D26E00848AE0 /* HardcoreDataDemo.xcdatamodeld in Sources */, + B503FAE11AFDC71700F90881 /* Palette.swift in Sources */, + B503FAE21AFDC71700F90881 /* PaletteTableViewCell.swift in Sources */, + B503FADF1AFDC71700F90881 /* ObjectListObserverDemoViewController.swift in Sources */, B54AAD4F1AF4D26E00848AE0 /* AppDelegate.swift in Sources */, - B5BC271B1AFA4E6900276889 /* ObjectObserverDemoViewController.swift in Sources */, - B5BC26E41AF8F67900276889 /* PaletteTableViewCell.swift in Sources */, - B5BC26E81AF8FD9600276889 /* Palette.swift in Sources */, - B54AAD541AF4D26E00848AE0 /* ObjectListObserverDemoViewController.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/HardcoreDataDemo/HardcoreDataDemo.xcodeproj/project.xcworkspace/xcshareddata/HardcoreDataDemo.xccheckout b/HardcoreDataDemo/HardcoreDataDemo.xcodeproj/project.xcworkspace/xcshareddata/HardcoreDataDemo.xccheckout index b520449..3247538 100644 --- a/HardcoreDataDemo/HardcoreDataDemo.xcodeproj/project.xcworkspace/xcshareddata/HardcoreDataDemo.xccheckout +++ b/HardcoreDataDemo/HardcoreDataDemo.xcodeproj/project.xcworkspace/xcshareddata/HardcoreDataDemo.xccheckout @@ -7,7 +7,7 @@ IDESourceControlProjectIdentifier 7C5E31AC-5DD0-43DA-A5C6-AF73B4532D86 IDESourceControlProjectName - project + HardcoreDataDemo IDESourceControlProjectOriginsDictionary 4B60F1BCB491FF717C56441AE7783C74F417BE48 @@ -16,13 +16,13 @@ github.com:JohnEstropia/GCDKit.git IDESourceControlProjectPath - HardcoreDataDemo/HardcoreDataDemo.xcodeproj/project.xcworkspace + HardcoreDataDemo/HardcoreDataDemo.xcodeproj IDESourceControlProjectRelativeInstallPathDictionary 4B60F1BCB491FF717C56441AE7783C74F417BE48 ../../.. 8B2E522D57154DFA93A06982C36315ECBEA4FA97 - ../../../Libraries/GCDKit + ../../..Libraries/GCDKit/ IDESourceControlProjectURL github.com:JohnEstropia/HardcoreData.git diff --git a/HardcoreDataDemo/HardcoreDataDemo/AppDelegate.swift b/HardcoreDataDemo/HardcoreDataDemo/AppDelegate.swift index 355aea4..a00c390 100644 --- a/HardcoreDataDemo/HardcoreDataDemo/AppDelegate.swift +++ b/HardcoreDataDemo/HardcoreDataDemo/AppDelegate.swift @@ -10,17 +10,6 @@ import UIKit import HardcoreData -let paletteList: ManagedObjectListController = { - - HardcoreData.defaultStack.addSQLiteStore() - return HardcoreData.observeObjectList( - From(Palette), - GroupBy("colorName"), - SortedBy(.Ascending("hue"), .Ascending("dateAdded")) - ) -}() - - // MARK: - AppDelegate @UIApplicationMain @@ -32,6 +21,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate { func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { + HardcoreData.defaultStack.addSQLiteStore() return true } } diff --git a/HardcoreDataDemo/HardcoreDataDemo/Base.lproj/Main.storyboard b/HardcoreDataDemo/HardcoreDataDemo/Base.lproj/Main.storyboard index 959cdd6..c7cdc67 100644 --- a/HardcoreDataDemo/HardcoreDataDemo/Base.lproj/Main.storyboard +++ b/HardcoreDataDemo/HardcoreDataDemo/Base.lproj/Main.storyboard @@ -1,5 +1,5 @@ - + @@ -15,20 +15,20 @@ - + - + - - + @@ -59,9 +59,6 @@ - - - @@ -77,72 +74,72 @@ - + - + - + - - + + + + + - - - + @@ -199,7 +189,6 @@ - @@ -207,28 +196,28 @@ - + - + - + - + - + - + - + @@ -238,95 +227,22 @@ - + + - - + - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -346,49 +262,30 @@ - - - - - - - - - - - - - - - - - - - - + - - + + - + - + @@ -414,35 +311,39 @@ + + + + - + - + - - + + - + - + @@ -467,7 +368,11 @@ - + + + + + @@ -476,7 +381,7 @@ - + @@ -492,9 +397,30 @@ + + + + + + + + + + + + + + + + + + + - + + + diff --git a/HardcoreDataDemo/HardcoreDataDemo/ObjectListObserverDemoViewController.swift b/HardcoreDataDemo/HardcoreDataDemo/List and Object Observers Demo/ObjectListObserverDemoViewController.swift similarity index 71% rename from HardcoreDataDemo/HardcoreDataDemo/ObjectListObserverDemoViewController.swift rename to HardcoreDataDemo/HardcoreDataDemo/List and Object Observers Demo/ObjectListObserverDemoViewController.swift index 74de276..3821eb3 100644 --- a/HardcoreDataDemo/HardcoreDataDemo/ObjectListObserverDemoViewController.swift +++ b/HardcoreDataDemo/HardcoreDataDemo/List and Object Observers Demo/ObjectListObserverDemoViewController.swift @@ -10,6 +10,16 @@ import UIKit import HardcoreData +struct Shared { + + static let palettes = HardcoreData.observeObjectList( + From(Palette), + GroupBy("colorName"), + SortedBy(.Ascending("hue"), .Ascending("dateAdded")) + ) +} + + // MARK: - ObjectListObserverDemoViewController class ObjectListObserverDemoViewController: UITableViewController, ManagedObjectListSectionObserver { @@ -18,7 +28,7 @@ class ObjectListObserverDemoViewController: UITableViewController, ManagedObject deinit { - paletteList.removeObserver(self) + Shared.palettes.removeObserver(self) } @@ -41,21 +51,22 @@ class ObjectListObserverDemoViewController: UITableViewController, ManagedObject target: self, action: "addBarButtonItemTouched:" ) + + Shared.palettes.addObserver(self) } - override func viewWillAppear(animated: Bool) { + override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { - super.viewWillAppear(animated) + super.prepareForSegue(segue, sender: sender) - paletteList.addObserver(self) - self.tableView.reloadData() - } - - override func viewDidDisappear(animated: Bool) { - - super.viewDidDisappear(animated) - - paletteList.removeObserver(self) + switch (segue.identifier, segue.destinationViewController, sender) { + + case (.Some("ObjectObserverDemoViewController"), let destinationViewController as ObjectObserverDemoViewController, let palette as Palette): + destinationViewController.palette = palette + + default: + break + } } @@ -63,19 +74,19 @@ class ObjectListObserverDemoViewController: UITableViewController, ManagedObject override func numberOfSectionsInTableView(tableView: UITableView) -> Int { - return paletteList.numberOfSections() + return Shared.palettes.numberOfSections() } override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { - return paletteList.numberOfObjectsInSection(section) + return Shared.palettes.numberOfObjectsInSection(section) } override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("PaletteTableViewCell") as! PaletteTableViewCell - let palette = paletteList[indexPath] + let palette = Shared.palettes[indexPath] cell.colorView?.backgroundColor = palette.color cell.label?.text = palette.colorText @@ -85,12 +96,22 @@ class ObjectListObserverDemoViewController: UITableViewController, ManagedObject // MARK: UITableViewDelegate + override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { + + tableView.deselectRowAtIndexPath(indexPath, animated: true) + + self.performSegueWithIdentifier( + "ObjectObserverDemoViewController", + sender: Shared.palettes[indexPath] + ) + } + override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) { switch editingStyle { case .Delete: - let palette = paletteList[indexPath] + let palette = Shared.palettes[indexPath] HardcoreData.beginAsynchronous{ (transaction) -> Void in transaction.delete(palette) @@ -104,7 +125,7 @@ class ObjectListObserverDemoViewController: UITableViewController, ManagedObject override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? { - return paletteList.sectionInfoAtIndex(section).name + return Shared.palettes.sectionInfoAtIndex(section).name } @@ -135,12 +156,18 @@ class ObjectListObserverDemoViewController: UITableViewController, ManagedObject func managedObjectList(listController: ManagedObjectListController, didUpdateObject object: Palette, atIndexPath indexPath: NSIndexPath) { - self.tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic) + if let cell = self.tableView.cellForRowAtIndexPath(indexPath) as? PaletteTableViewCell { + + let palette = Shared.palettes[indexPath] + cell.colorView?.backgroundColor = palette.color + cell.label?.text = palette.colorText + } } func managedObjectList(listController: ManagedObjectListController, didMoveObject object: Palette, fromIndexPath: NSIndexPath, toIndexPath: NSIndexPath) { - self.tableView.moveRowAtIndexPath(fromIndexPath, toIndexPath: toIndexPath) + self.tableView.deleteRowsAtIndexPaths([fromIndexPath], withRowAnimation: .Automatic) + self.tableView.insertRowsAtIndexPaths([toIndexPath], withRowAnimation: .Automatic) } @@ -172,11 +199,8 @@ class ObjectListObserverDemoViewController: UITableViewController, ManagedObject HardcoreData.beginAsynchronous { (transaction) -> Void in - for _ in 0 ... 2 { - - let palette = transaction.create(Palette) - palette.setInitialValues() - } + let palette = transaction.create(Palette) + palette.setInitialValues() transaction.commit { (result) -> Void in } } diff --git a/HardcoreDataDemo/HardcoreDataDemo/ObjectObserverDemoViewController.swift b/HardcoreDataDemo/HardcoreDataDemo/List and Object Observers Demo/ObjectObserverDemoViewController.swift similarity index 74% rename from HardcoreDataDemo/HardcoreDataDemo/ObjectObserverDemoViewController.swift rename to HardcoreDataDemo/HardcoreDataDemo/List and Object Observers Demo/ObjectObserverDemoViewController.swift index 02db67f..d6dded8 100644 --- a/HardcoreDataDemo/HardcoreDataDemo/ObjectObserverDemoViewController.swift +++ b/HardcoreDataDemo/HardcoreDataDemo/List and Object Observers Demo/ObjectObserverDemoViewController.swift @@ -14,11 +14,30 @@ import HardcoreData class ObjectObserverDemoViewController: UIViewController, ManagedObjectObserver { + var palette: Palette? { + + get { + + return self.objectController?.object + } + set { + + if let palette = newValue { + + self.objectController = HardcoreData.observeObject(palette) + } + else { + + self.objectController = nil + } + } + } + // MARK: NSObject deinit { - self.objectController.removeObserver(self) + self.objectController?.removeObserver(self) } @@ -50,14 +69,11 @@ class ObjectObserverDemoViewController: UIViewController, ManagedObjectObserver override func viewDidLoad() { super.viewDidLoad() - self.objectController.addObserver(self) + self.objectController?.addObserver(self) - if let palette = self.objectController.object { + if let palette = self.objectController?.object { self.reloadPaletteInfo(palette) - self.hueSlider?.value = Float(palette.hue) - self.saturationSlider?.value = palette.saturation - self.brightnessSlider?.value = palette.brightness } } @@ -66,6 +82,7 @@ class ObjectObserverDemoViewController: UIViewController, ManagedObjectObserver func managedObjectWillUpdate(objectController: ManagedObjectController, object: Palette) { + // none } func managedObjectWasUpdated(objectController: ManagedObjectController, object: Palette) { @@ -75,10 +92,13 @@ class ObjectObserverDemoViewController: UIViewController, ManagedObjectObserver func managedObjectWasDeleted(objectController: ManagedObjectController, object: Palette) { + self.navigationItem.rightBarButtonItem?.enabled = false + self.colorNameLabel?.alpha = 0.3 self.colorView?.alpha = 0.3 - self.dateLabel?.text = "Deleted" - self.dateLabel?.textColor = UIColor.redColor() + + self.hsbLabel?.text = "Deleted" + self.hsbLabel?.textColor = UIColor.redColor() self.hueSlider?.enabled = false self.saturationSlider?.enabled = false @@ -88,7 +108,7 @@ class ObjectObserverDemoViewController: UIViewController, ManagedObjectObserver // MARK: Private - let objectController: ManagedObjectController + var objectController: ManagedObjectController? @IBOutlet weak var colorNameLabel: UILabel? @IBOutlet weak var colorView: UIView? @@ -103,7 +123,7 @@ class ObjectObserverDemoViewController: UIViewController, ManagedObjectObserver let hue = self.hueSlider?.value ?? 0 HardcoreData.beginAsynchronous { [weak self] (transaction) -> Void in - if let palette = transaction.fetch(self?.objectController.object) { + if let palette = transaction.fetch(self?.objectController?.object) { palette.hue = Int32(hue) palette.dateAdded = NSDate() @@ -117,7 +137,7 @@ class ObjectObserverDemoViewController: UIViewController, ManagedObjectObserver let saturation = self.saturationSlider?.value ?? 0 HardcoreData.beginAsynchronous { [weak self] (transaction) -> Void in - if let palette = transaction.fetch(self?.objectController.object) { + if let palette = transaction.fetch(self?.objectController?.object) { palette.saturation = saturation palette.dateAdded = NSDate() @@ -131,7 +151,7 @@ class ObjectObserverDemoViewController: UIViewController, ManagedObjectObserver let brightness = self.brightnessSlider?.value ?? 0 HardcoreData.beginAsynchronous { [weak self] (transaction) -> Void in - if let palette = transaction.fetch(self?.objectController.object) { + if let palette = transaction.fetch(self?.objectController?.object) { palette.brightness = brightness palette.dateAdded = NSDate() @@ -144,11 +164,9 @@ class ObjectObserverDemoViewController: UIViewController, ManagedObjectObserver HardcoreData.beginAsynchronous { [weak self] (transaction) -> Void in - transaction.delete(self?.objectController.object) + transaction.delete(self?.objectController?.object) transaction.commit { (result) -> Void in } } - - (sender as? UIBarButtonItem)?.enabled = false } func reloadPaletteInfo(palette: Palette) { @@ -159,13 +177,23 @@ class ObjectObserverDemoViewController: UIViewController, ManagedObjectObserver self.colorNameLabel?.textColor = color self.colorView?.backgroundColor = color - self.hsbLabel?.text = "H: \(palette.hue)˚, S: \(round(palette.saturation * 100.0))%, B: \(round(palette.brightness * 100.0))%" + let hue = palette.hue + let saturation = palette.saturation + let brightness = palette.brightness - let dateString = NSDateFormatter.localizedStringFromDate( - palette.dateAdded, - dateStyle: .ShortStyle, - timeStyle: .LongStyle - ) - self.dateLabel?.text = "Updated: \(dateString)" + self.hsbLabel?.text = "H: \(hue)˚, S: \(Int(saturation * 100))%, B: \(Int(brightness * 100))%" + + if Int32(self.hueSlider?.value ?? 0) != hue { + + self.hueSlider?.value = Float(hue) + } + if self.saturationSlider?.value != saturation { + + self.saturationSlider?.value = saturation + } + if self.brightnessSlider?.value != brightness { + + self.brightnessSlider?.value = brightness + } } } diff --git a/HardcoreDataDemo/HardcoreDataDemo/Palette.swift b/HardcoreDataDemo/HardcoreDataDemo/List and Object Observers Demo/Palette.swift similarity index 98% rename from HardcoreDataDemo/HardcoreDataDemo/Palette.swift rename to HardcoreDataDemo/HardcoreDataDemo/List and Object Observers Demo/Palette.swift index 443a2a4..fc59d41 100644 --- a/HardcoreDataDemo/HardcoreDataDemo/Palette.swift +++ b/HardcoreDataDemo/HardcoreDataDemo/List and Object Observers Demo/Palette.swift @@ -9,6 +9,10 @@ import Foundation import UIKit import CoreData +import HardcoreData + + +// MARK: - Palette class Palette: NSManagedObject { diff --git a/HardcoreDataDemo/HardcoreDataDemo/PaletteTableViewCell.swift b/HardcoreDataDemo/HardcoreDataDemo/List and Object Observers Demo/PaletteTableViewCell.swift similarity index 100% rename from HardcoreDataDemo/HardcoreDataDemo/PaletteTableViewCell.swift rename to HardcoreDataDemo/HardcoreDataDemo/List and Object Observers Demo/PaletteTableViewCell.swift