mirror of
https://github.com/JohnEstropia/CoreStore.git
synced 2026-03-17 23:14:05 +01:00
NSManagedObject features are now fully supported for CoreStoreObject types. MacOSX 10.12 onwards now support ListMonitors and ObjectMonitors
This commit is contained in:
99
Sources/Convenience/CoreStoreObject+Convenience.swift
Normal file
99
Sources/Convenience/CoreStoreObject+Convenience.swift
Normal file
@@ -0,0 +1,99 @@
|
||||
//
|
||||
// CoreStoreObject+Convenience.swift
|
||||
// CoreStore
|
||||
//
|
||||
// Copyright © 2017 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: - CoreStoreObject
|
||||
|
||||
public extension CoreStoreObject {
|
||||
|
||||
/**
|
||||
Exposes a `FetchableSource` that can fetch sibling objects of this `CoreStoreObject` instance. This may be the `DataStack`, a `BaseDataTransaction`, the `NSManagedObjectContext` itself, or `nil` if the obejct's parent is already deallocated.
|
||||
- Warning: Future implementations may change the instance returned by this method depending on the timing or condition that `fetchSource()` was called. Do not make assumptions that the instance will be a specific instance. If the `NSManagedObjectContext` instance is desired, use the `FetchableSource.unsafeContext()` method to get the correct instance. Also, do not assume that the `fetchSource()` and `querySource()` return the same instance all the time.
|
||||
- returns: a `FetchableSource` that can fetch sibling objects of this `CoreStoreObject` instance. This may be the `DataStack`, a `BaseDataTransaction`, the `NSManagedObjectContext` itself, or `nil` if the object's parent is already deallocated.
|
||||
*/
|
||||
@nonobjc
|
||||
public func fetchSource() -> FetchableSource? {
|
||||
|
||||
guard let context = self.rawObject!.managedObjectContext else {
|
||||
|
||||
return nil
|
||||
}
|
||||
if context.isTransactionContext {
|
||||
|
||||
return context.parentTransaction
|
||||
}
|
||||
if context.isDataStackContext {
|
||||
|
||||
return context.parentStack
|
||||
}
|
||||
return context
|
||||
}
|
||||
|
||||
/**
|
||||
Exposes a `QueryableSource` that can query attributes and aggregate values. This may be the `DataStack`, a `BaseDataTransaction`, the `NSManagedObjectContext` itself, or `nil` if the obejct's parent is already deallocated.
|
||||
- Warning: Future implementations may change the instance returned by this method depending on the timing or condition that `querySource()` was called. Do not make assumptions that the instance will be a specific instance. If the `NSManagedObjectContext` instance is desired, use the `QueryableSource.unsafeContext()` method to get the correct instance. Also, do not assume that the `fetchSource()` and `querySource()` return the same instance all the time.
|
||||
- returns: a `QueryableSource` that can query attributes and aggregate values. This may be the `DataStack`, a `BaseDataTransaction`, the `NSManagedObjectContext` itself, or `nil` if the object's parent is already deallocated.
|
||||
*/
|
||||
@nonobjc
|
||||
public func querySource() -> QueryableSource? {
|
||||
|
||||
guard let context = self.rawObject!.managedObjectContext else {
|
||||
|
||||
return nil
|
||||
}
|
||||
if context.isTransactionContext {
|
||||
|
||||
return context.parentTransaction
|
||||
}
|
||||
if context.isDataStackContext {
|
||||
|
||||
return context.parentStack
|
||||
}
|
||||
return context
|
||||
}
|
||||
|
||||
/**
|
||||
Re-faults the object to use the latest values from the persistent store
|
||||
*/
|
||||
@nonobjc
|
||||
public func refreshAsFault() {
|
||||
|
||||
let rawObject = self.rawObject!
|
||||
rawObject.managedObjectContext?.refresh(rawObject, mergeChanges: false)
|
||||
}
|
||||
|
||||
/**
|
||||
Re-faults the object to use the latest values from the persistent store and merges previously pending changes back
|
||||
*/
|
||||
@nonobjc
|
||||
public func refreshAndMerge() {
|
||||
|
||||
let rawObject = self.rawObject!
|
||||
rawObject.managedObjectContext?.refresh(rawObject, mergeChanges: true)
|
||||
}
|
||||
}
|
||||
@@ -26,10 +26,10 @@
|
||||
import Foundation
|
||||
import CoreData
|
||||
|
||||
#if os(iOS) || os(watchOS) || os(tvOS)
|
||||
|
||||
// MARK: - DataStack
|
||||
|
||||
@available(OSX 10.12, *)
|
||||
public extension DataStack {
|
||||
|
||||
/**
|
||||
@@ -114,6 +114,7 @@ public extension DataStack {
|
||||
|
||||
// MARK: - UnsafeDataTransaction
|
||||
|
||||
@available(OSX 10.12, *)
|
||||
public extension UnsafeDataTransaction {
|
||||
|
||||
/**
|
||||
@@ -199,6 +200,7 @@ public extension UnsafeDataTransaction {
|
||||
|
||||
// MARK: - Private
|
||||
|
||||
@available(OSX 10.12, *)
|
||||
fileprivate func createFRC<T: NSManagedObject>(fromContext context: NSManagedObjectContext, from: From<T>? = nil, sectionBy: SectionBy? = nil, fetchClauses: [FetchClause]) -> NSFetchedResultsController<T> {
|
||||
|
||||
let controller = CoreStoreFetchedResultsController(
|
||||
@@ -218,5 +220,3 @@ fileprivate func createFRC<T: NSManagedObject>(fromContext context: NSManagedObj
|
||||
)
|
||||
return controller.dynamicCast()
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@@ -77,50 +77,43 @@ public extension NSManagedObject {
|
||||
return context
|
||||
}
|
||||
|
||||
/**
|
||||
Provides a convenience wrapper for accessing `primitiveValueForKey(...)` with proper calls to `willAccessValueForKey(...)` and `didAccessValueForKey(...)`. This is useful when implementing accessor methods for transient attributes.
|
||||
|
||||
- parameter KVCKey: the KVC key
|
||||
- returns: the primitive value for the KVC key
|
||||
*/
|
||||
@nonobjc
|
||||
public func accessValueForKVCKey(_ KVCKey: KeyPath) -> Any? {
|
||||
@nonobjc @inline(__always)
|
||||
public func getValue(forKvcKey kvcKey: KeyPath) -> Any? {
|
||||
|
||||
self.willAccessValue(forKey: KVCKey)
|
||||
self.willAccessValue(forKey: kvcKey)
|
||||
defer {
|
||||
|
||||
self.didAccessValue(forKey: KVCKey)
|
||||
self.didAccessValue(forKey: kvcKey)
|
||||
}
|
||||
return self.primitiveValue(forKey: KVCKey)
|
||||
return self.primitiveValue(forKey: kvcKey)
|
||||
}
|
||||
|
||||
/**
|
||||
Provides a convenience wrapper for accessing `primitiveValueForKey(...)` with proper calls to `willAccessValueForKey(...)` and `didAccessValueForKey(...)`. This is useful when implementing accessor methods for transient attributes.
|
||||
|
||||
- parameter KVCKey: the KVC key
|
||||
- parameter didAccessPrimitiveValue: the closure to access the value. This is called between `willAccessValueForKey(...)` and `didAccessValueForKey(...)`
|
||||
- returns: the primitive value for the KVC key
|
||||
*/
|
||||
@nonobjc @inline(__always)
|
||||
public func getValue<T>(forKvcKey kvcKey: KeyPath, didGetValue: (Any?) throws -> T) rethrows -> T {
|
||||
|
||||
self.willAccessValue(forKey: kvcKey)
|
||||
defer {
|
||||
|
||||
self.didAccessValue(forKey: kvcKey)
|
||||
}
|
||||
return try didGetValue(self.primitiveValue(forKey: kvcKey))
|
||||
}
|
||||
|
||||
@nonobjc @inline(__always)
|
||||
public func getValue<T>(forKvcKey kvcKey: KeyPath, willGetValue: () throws -> Void, didGetValue: (Any?) throws -> T) rethrows -> T {
|
||||
|
||||
self.willAccessValue(forKey: kvcKey)
|
||||
defer {
|
||||
|
||||
self.didAccessValue(forKey: kvcKey)
|
||||
}
|
||||
try willGetValue()
|
||||
return try didGetValue(self.primitiveValue(forKey: kvcKey))
|
||||
}
|
||||
|
||||
@nonobjc @inline(__always)
|
||||
@discardableResult
|
||||
@nonobjc
|
||||
public func accessValueForKVCKey<T>(_ KVCKey: KeyPath, _ didAccessPrimitiveValue: (Any?) throws -> T) rethrows -> T {
|
||||
|
||||
self.willAccessValue(forKey: KVCKey)
|
||||
defer {
|
||||
|
||||
self.didAccessValue(forKey: KVCKey)
|
||||
}
|
||||
return try didAccessPrimitiveValue(self.primitiveValue(forKey: KVCKey))
|
||||
}
|
||||
|
||||
/**
|
||||
Provides a convenience wrapper for setting `setPrimitiveValue(...)` with proper calls to `willChangeValueForKey(...)` and `didChangeValueForKey(...)`. This is useful when implementing mutator methods for transient attributes.
|
||||
|
||||
- parameter value: the value to set the KVC key with
|
||||
- parameter KVCKey: the KVC key
|
||||
*/
|
||||
@nonobjc
|
||||
public func setValue(_ value: Any?, forKVCKey KVCKey: KeyPath) {
|
||||
public func setValue(_ value: Any?, forKvcKey KVCKey: KeyPath) -> Any? {
|
||||
|
||||
self.willChangeValue(forKey: KVCKey)
|
||||
defer {
|
||||
@@ -128,26 +121,33 @@ public extension NSManagedObject {
|
||||
self.didChangeValue(forKey: KVCKey)
|
||||
}
|
||||
self.setPrimitiveValue(value, forKey: KVCKey)
|
||||
return value
|
||||
}
|
||||
|
||||
/**
|
||||
Provides a convenience wrapper for setting `setPrimitiveValue(...)` with proper calls to `willChangeValueForKey(...)` and `didChangeValueForKey(...)`. This is useful when implementing mutator methods for transient attributes.
|
||||
|
||||
- parameter value: the value to set the KVC key with
|
||||
- parameter KVCKey: the KVC key
|
||||
- parameter didSetPrimitiveValue: the closure called between `willChangeValueForKey(...)` and `didChangeValueForKey(...)`
|
||||
*/
|
||||
@nonobjc @inline(__always)
|
||||
@discardableResult
|
||||
@nonobjc
|
||||
public func setValue<T>(_ value: Any?, forKVCKey KVCKey: KeyPath, _ didSetPrimitiveValue: (Any?) throws -> T) rethrows -> T {
|
||||
public func setValue<T>(_ value: T, forKvcKey KVCKey: KeyPath, willSetValue: (T) throws -> Any?) rethrows -> T {
|
||||
|
||||
self.willChangeValue(forKey: KVCKey)
|
||||
defer {
|
||||
|
||||
self.didChangeValue(forKey: KVCKey)
|
||||
}
|
||||
self.setPrimitiveValue(value, forKey: KVCKey)
|
||||
return try didSetPrimitiveValue(value)
|
||||
self.setPrimitiveValue(try willSetValue(value), forKey: KVCKey)
|
||||
return value
|
||||
}
|
||||
|
||||
@nonobjc @inline(__always)
|
||||
@discardableResult
|
||||
public func setValue<T>(_ value: T, forKvcKey KVCKey: KeyPath, willSetValue: (T) throws -> Any?, didSetValue: (T) -> T = { $0 }) rethrows -> T {
|
||||
|
||||
self.willChangeValue(forKey: KVCKey)
|
||||
defer {
|
||||
|
||||
self.didChangeValue(forKey: KVCKey)
|
||||
}
|
||||
self.setPrimitiveValue(try willSetValue(value), forKey: KVCKey)
|
||||
return didSetValue(value)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -167,4 +167,84 @@ public extension NSManagedObject {
|
||||
|
||||
self.managedObjectContext?.refresh(self, mergeChanges: true)
|
||||
}
|
||||
|
||||
|
||||
// MARK: Deprecated
|
||||
|
||||
/**
|
||||
Provides a convenience wrapper for accessing `primitiveValueForKey(...)` with proper calls to `willAccessValueForKey(...)` and `didAccessValueForKey(...)`. This is useful when implementing accessor methods for transient attributes.
|
||||
|
||||
- parameter KVCKey: the KVC key
|
||||
- returns: the primitive value for the KVC key
|
||||
*/
|
||||
@available(*, deprecated: 3.1, renamed: "getValue(forKvcKey:)")
|
||||
@nonobjc
|
||||
public func accessValueForKVCKey(_ KVCKey: KeyPath) -> Any? {
|
||||
|
||||
self.willAccessValue(forKey: KVCKey)
|
||||
defer {
|
||||
|
||||
self.didAccessValue(forKey: KVCKey)
|
||||
}
|
||||
return self.primitiveValue(forKey: KVCKey)
|
||||
}
|
||||
|
||||
/**
|
||||
Provides a convenience wrapper for accessing `primitiveValueForKey(...)` with proper calls to `willAccessValueForKey(...)` and `didAccessValueForKey(...)`. This is useful when implementing accessor methods for transient attributes.
|
||||
|
||||
- parameter KVCKey: the KVC key
|
||||
- parameter didAccessPrimitiveValue: the closure to access the value. This is called between `willAccessValueForKey(...)` and `didAccessValueForKey(...)`
|
||||
- returns: the primitive value for the KVC key
|
||||
*/
|
||||
@available(*, deprecated: 3.1, renamed: "getValue(forKvcKey:didGetValue:)")
|
||||
@discardableResult
|
||||
@nonobjc
|
||||
public func accessValueForKVCKey<T>(_ KVCKey: KeyPath, _ didAccessPrimitiveValue: (Any?) throws -> T) rethrows -> T {
|
||||
|
||||
self.willAccessValue(forKey: KVCKey)
|
||||
defer {
|
||||
|
||||
self.didAccessValue(forKey: KVCKey)
|
||||
}
|
||||
return try didAccessPrimitiveValue(self.primitiveValue(forKey: KVCKey))
|
||||
}
|
||||
|
||||
/**
|
||||
Provides a convenience wrapper for setting `setPrimitiveValue(...)` with proper calls to `willChangeValueForKey(...)` and `didChangeValueForKey(...)`. This is useful when implementing mutator methods for transient attributes.
|
||||
|
||||
- parameter value: the value to set the KVC key with
|
||||
- parameter KVCKey: the KVC key
|
||||
*/
|
||||
@available(*, deprecated: 3.1, renamed: "setValue(_:forKvcKey:)")
|
||||
@nonobjc
|
||||
public func setValue(_ value: Any?, forKVCKey KVCKey: KeyPath) {
|
||||
|
||||
self.willChangeValue(forKey: KVCKey)
|
||||
defer {
|
||||
|
||||
self.didChangeValue(forKey: KVCKey)
|
||||
}
|
||||
self.setPrimitiveValue(value, forKey: KVCKey)
|
||||
}
|
||||
|
||||
/**
|
||||
Provides a convenience wrapper for setting `setPrimitiveValue(...)` with proper calls to `willChangeValueForKey(...)` and `didChangeValueForKey(...)`. This is useful when implementing mutator methods for transient attributes.
|
||||
|
||||
- parameter value: the value to set the KVC key with
|
||||
- parameter KVCKey: the KVC key
|
||||
- parameter didSetPrimitiveValue: the closure called between `willChangeValueForKey(...)` and `didChangeValueForKey(...)`
|
||||
*/
|
||||
@available(*, deprecated: 3.1, renamed: "setValue(_:forKvcKey:didSetValue:)")
|
||||
@discardableResult
|
||||
@nonobjc
|
||||
public func setValue<T>(_ value: Any?, forKVCKey KVCKey: KeyPath, _ didSetPrimitiveValue: (Any?) throws -> T) rethrows -> T {
|
||||
|
||||
self.willChangeValue(forKey: KVCKey)
|
||||
defer {
|
||||
|
||||
self.didChangeValue(forKey: KVCKey)
|
||||
}
|
||||
self.setPrimitiveValue(value, forKey: KVCKey)
|
||||
return try didSetPrimitiveValue(value)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user