mirror of
https://github.com/JohnEstropia/CoreStore.git
synced 2026-01-15 13:43:43 +01:00
ListMonitor and ObjectMonitor objective C bridge
This commit is contained in:
@@ -373,6 +373,22 @@
|
||||
B5DC47CA1C93D9C800FA3BF3 /* StorageInterfaceTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5DC47C91C93D9C800FA3BF3 /* StorageInterfaceTests.swift */; };
|
||||
B5DC47CB1C93D9C800FA3BF3 /* StorageInterfaceTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5DC47C91C93D9C800FA3BF3 /* StorageInterfaceTests.swift */; };
|
||||
B5DC47CC1C93D9C800FA3BF3 /* StorageInterfaceTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5DC47C91C93D9C800FA3BF3 /* StorageInterfaceTests.swift */; };
|
||||
B5E1B5931CAA0C15007FD580 /* CSObjectMonitor.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5E1B5921CAA0C15007FD580 /* CSObjectMonitor.swift */; };
|
||||
B5E1B5941CAA0C15007FD580 /* CSObjectMonitor.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5E1B5921CAA0C15007FD580 /* CSObjectMonitor.swift */; };
|
||||
B5E1B5951CAA0C15007FD580 /* CSObjectMonitor.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5E1B5921CAA0C15007FD580 /* CSObjectMonitor.swift */; };
|
||||
B5E1B5961CAA0C15007FD580 /* CSObjectMonitor.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5E1B5921CAA0C15007FD580 /* CSObjectMonitor.swift */; };
|
||||
B5E1B5981CAA0C23007FD580 /* CSObjectObserver.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5E1B5971CAA0C23007FD580 /* CSObjectObserver.swift */; };
|
||||
B5E1B5991CAA0C23007FD580 /* CSObjectObserver.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5E1B5971CAA0C23007FD580 /* CSObjectObserver.swift */; };
|
||||
B5E1B59A1CAA0C23007FD580 /* CSObjectObserver.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5E1B5971CAA0C23007FD580 /* CSObjectObserver.swift */; };
|
||||
B5E1B59B1CAA0C23007FD580 /* CSObjectObserver.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5E1B5971CAA0C23007FD580 /* CSObjectObserver.swift */; };
|
||||
B5E1B59D1CAA2568007FD580 /* CSDataStack+Observing.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5E1B59C1CAA2568007FD580 /* CSDataStack+Observing.swift */; };
|
||||
B5E1B59E1CAA2568007FD580 /* CSDataStack+Observing.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5E1B59C1CAA2568007FD580 /* CSDataStack+Observing.swift */; };
|
||||
B5E1B59F1CAA2568007FD580 /* CSDataStack+Observing.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5E1B59C1CAA2568007FD580 /* CSDataStack+Observing.swift */; };
|
||||
B5E1B5A01CAA2568007FD580 /* CSDataStack+Observing.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5E1B59C1CAA2568007FD580 /* CSDataStack+Observing.swift */; };
|
||||
B5E1B5A21CAA4365007FD580 /* CSCoreStore+Observing.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5E1B5A11CAA4365007FD580 /* CSCoreStore+Observing.swift */; };
|
||||
B5E1B5A31CAA4365007FD580 /* CSCoreStore+Observing.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5E1B5A11CAA4365007FD580 /* CSCoreStore+Observing.swift */; };
|
||||
B5E1B5A41CAA4365007FD580 /* CSCoreStore+Observing.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5E1B5A11CAA4365007FD580 /* CSCoreStore+Observing.swift */; };
|
||||
B5E1B5A51CAA4365007FD580 /* CSCoreStore+Observing.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5E1B5A11CAA4365007FD580 /* CSCoreStore+Observing.swift */; };
|
||||
B5E222231CA4E12600BA2E95 /* CSSynchronousDataTransaction.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5E222221CA4E12600BA2E95 /* CSSynchronousDataTransaction.swift */; };
|
||||
B5E222241CA4E12600BA2E95 /* CSSynchronousDataTransaction.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5E222221CA4E12600BA2E95 /* CSSynchronousDataTransaction.swift */; };
|
||||
B5E222251CA4E12600BA2E95 /* CSSynchronousDataTransaction.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5E222221CA4E12600BA2E95 /* CSSynchronousDataTransaction.swift */; };
|
||||
@@ -612,6 +628,10 @@
|
||||
B5DBE2DE1C9939E100B5CEFA /* BridgingTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BridgingTests.m; sourceTree = "<group>"; };
|
||||
B5DC47C51C93D22900FA3BF3 /* MigrationChainTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MigrationChainTests.swift; sourceTree = "<group>"; };
|
||||
B5DC47C91C93D9C800FA3BF3 /* StorageInterfaceTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StorageInterfaceTests.swift; sourceTree = "<group>"; };
|
||||
B5E1B5921CAA0C15007FD580 /* CSObjectMonitor.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CSObjectMonitor.swift; sourceTree = "<group>"; };
|
||||
B5E1B5971CAA0C23007FD580 /* CSObjectObserver.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CSObjectObserver.swift; sourceTree = "<group>"; };
|
||||
B5E1B59C1CAA2568007FD580 /* CSDataStack+Observing.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "CSDataStack+Observing.swift"; sourceTree = "<group>"; };
|
||||
B5E1B5A11CAA4365007FD580 /* CSCoreStore+Observing.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "CSCoreStore+Observing.swift"; sourceTree = "<group>"; };
|
||||
B5E222221CA4E12600BA2E95 /* CSSynchronousDataTransaction.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CSSynchronousDataTransaction.swift; sourceTree = "<group>"; };
|
||||
B5E222291CA51B6E00BA2E95 /* CSUnsafeDataTransaction.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CSUnsafeDataTransaction.swift; sourceTree = "<group>"; };
|
||||
B5E834B81B76311F001D3D50 /* BaseDataTransaction+Importing.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "BaseDataTransaction+Importing.swift"; sourceTree = "<group>"; };
|
||||
@@ -860,8 +880,12 @@
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
B501FDDC1CA8D05000BE22EF /* CSSectionBy.swift */,
|
||||
B5E1B59C1CAA2568007FD580 /* CSDataStack+Observing.swift */,
|
||||
B5E1B5A11CAA4365007FD580 /* CSCoreStore+Observing.swift */,
|
||||
B501FDE11CA8D1F500BE22EF /* CSListMonitor.swift */,
|
||||
B501FDE61CA8D20500BE22EF /* CSListObserver.swift */,
|
||||
B5E1B5921CAA0C15007FD580 /* CSObjectMonitor.swift */,
|
||||
B5E1B5971CAA0C23007FD580 /* CSObjectObserver.swift */,
|
||||
);
|
||||
name = Observing;
|
||||
sourceTree = "<group>";
|
||||
@@ -1442,6 +1466,7 @@
|
||||
B5D3F6451C887C0A00C7492A /* LegacySQLiteStore.swift in Sources */,
|
||||
B5ECDBFF1CA80CBA00C7F112 /* CSWhere.swift in Sources */,
|
||||
B5ECDC051CA8138100C7F112 /* CSOrderBy.swift in Sources */,
|
||||
B5E1B5981CAA0C23007FD580 /* CSObjectObserver.swift in Sources */,
|
||||
B5519A5F1CA21954002BEF78 /* CSAsynchronousDataTransaction.swift in Sources */,
|
||||
B54A6A551BA15F2A007870FD /* FetchedResultsControllerDelegate.swift in Sources */,
|
||||
B5A261211B64BFDB006EB6D3 /* MigrationType.swift in Sources */,
|
||||
@@ -1452,8 +1477,10 @@
|
||||
B5ECDC1D1CA81A2100C7F112 /* CSDataStack+Querying.swift in Sources */,
|
||||
B5C976E31C6C9F6A00B1AF90 /* UnsafeDataTransaction+Observing.swift in Sources */,
|
||||
B56007161B4018AB00A9A8F9 /* MigrationChain.swift in Sources */,
|
||||
B5E1B59D1CAA2568007FD580 /* CSDataStack+Observing.swift in Sources */,
|
||||
B5ECDC231CA81A3900C7F112 /* CSCoreStore+Querying.swift in Sources */,
|
||||
B5E84F0E1AFF847B0064E85B /* Tweak.swift in Sources */,
|
||||
B5E1B5931CAA0C15007FD580 /* CSObjectMonitor.swift in Sources */,
|
||||
B5ECDC291CA81CC700C7F112 /* CSDataStack+Transaction.swift in Sources */,
|
||||
B5E84F121AFF847B0064E85B /* OrderBy.swift in Sources */,
|
||||
B546F9581C99B17400D5AC55 /* CSCoreStore+Setup.swift in Sources */,
|
||||
@@ -1484,6 +1511,7 @@
|
||||
B5FAD6AC1B51285300714891 /* MigrationManager.swift in Sources */,
|
||||
B5E84EF61AFF846E0064E85B /* DataStack+Transaction.swift in Sources */,
|
||||
B5FEC18E1C9166E200532541 /* NSPersistentStore+Setup.swift in Sources */,
|
||||
B5E1B5A21CAA4365007FD580 /* CSCoreStore+Observing.swift in Sources */,
|
||||
B5E84EDF1AFF84500064E85B /* DataStack.swift in Sources */,
|
||||
B59AFF411C6593E400C0ABE2 /* NSPersistentStoreCoordinator+Setup.swift in Sources */,
|
||||
B5E834BB1B7691F3001D3D50 /* Functions.swift in Sources */,
|
||||
@@ -1557,6 +1585,7 @@
|
||||
82BA18CE1C4BBD7100A0916E /* FetchedResultsControllerDelegate.swift in Sources */,
|
||||
B5ECDC011CA80CBA00C7F112 /* CSWhere.swift in Sources */,
|
||||
B5ECDC071CA8138100C7F112 /* CSOrderBy.swift in Sources */,
|
||||
B5E1B59A1CAA0C23007FD580 /* CSObjectObserver.swift in Sources */,
|
||||
B5519A601CA21954002BEF78 /* CSAsynchronousDataTransaction.swift in Sources */,
|
||||
B5FE4DAD1C85D44E00FA6A91 /* SQLiteStore.swift in Sources */,
|
||||
82BA18C51C4BBD5300A0916E /* ListObserver.swift in Sources */,
|
||||
@@ -1567,8 +1596,10 @@
|
||||
B5ECDC1F1CA81A2100C7F112 /* CSDataStack+Querying.swift in Sources */,
|
||||
B5C976E41C6C9F9A00B1AF90 /* UnsafeDataTransaction+Observing.swift in Sources */,
|
||||
B5D3F6461C887C0A00C7492A /* LegacySQLiteStore.swift in Sources */,
|
||||
B5E1B59F1CAA2568007FD580 /* CSDataStack+Observing.swift in Sources */,
|
||||
B5ECDC251CA81A3900C7F112 /* CSCoreStore+Querying.swift in Sources */,
|
||||
82BA18B31C4BBD3900A0916E /* ImportableUniqueObject.swift in Sources */,
|
||||
B5E1B5951CAA0C15007FD580 /* CSObjectMonitor.swift in Sources */,
|
||||
B5ECDC2B1CA81CC700C7F112 /* CSDataStack+Transaction.swift in Sources */,
|
||||
82BA18A11C4BBD1D00A0916E /* CoreStore.swift in Sources */,
|
||||
B546F9591C99B17400D5AC55 /* CSCoreStore+Setup.swift in Sources */,
|
||||
@@ -1599,6 +1630,7 @@
|
||||
82BA18C61C4BBD5900A0916E /* DataStack+Migration.swift in Sources */,
|
||||
82BA18CD1C4BBD7100A0916E /* AssociatedObjects.swift in Sources */,
|
||||
B59851491C90289D00C99590 /* NSPersistentStoreCoordinator+Setup.swift in Sources */,
|
||||
B5E1B5A41CAA4365007FD580 /* CSCoreStore+Observing.swift in Sources */,
|
||||
B5FEC18F1C9166E600532541 /* NSPersistentStore+Setup.swift in Sources */,
|
||||
82BA18B71C4BBD3F00A0916E /* CoreStore+Querying.swift in Sources */,
|
||||
82BA18AA1C4BBD3100A0916E /* BaseDataTransaction.swift in Sources */,
|
||||
@@ -1773,6 +1805,7 @@
|
||||
B56321AA1BD6521C006C9394 /* AssociatedObjects.swift in Sources */,
|
||||
B5ECDC021CA80CBA00C7F112 /* CSWhere.swift in Sources */,
|
||||
B5ECDC081CA8138100C7F112 /* CSOrderBy.swift in Sources */,
|
||||
B5E1B59B1CAA0C23007FD580 /* CSObjectObserver.swift in Sources */,
|
||||
B5519A611CA21954002BEF78 /* CSAsynchronousDataTransaction.swift in Sources */,
|
||||
B5FE4DAE1C85D44E00FA6A91 /* SQLiteStore.swift in Sources */,
|
||||
B563218C1BD65216006C9394 /* DataStack+Transaction.swift in Sources */,
|
||||
@@ -1783,8 +1816,10 @@
|
||||
B5ECDC201CA81A2100C7F112 /* CSDataStack+Querying.swift in Sources */,
|
||||
B5C976E51C6C9F9B00B1AF90 /* UnsafeDataTransaction+Observing.swift in Sources */,
|
||||
B5D3F6471C887C0A00C7492A /* LegacySQLiteStore.swift in Sources */,
|
||||
B5E1B5A01CAA2568007FD580 /* CSDataStack+Observing.swift in Sources */,
|
||||
B5ECDC261CA81A3900C7F112 /* CSCoreStore+Querying.swift in Sources */,
|
||||
B563217F1BD65216006C9394 /* CoreStore.swift in Sources */,
|
||||
B5E1B5961CAA0C15007FD580 /* CSObjectMonitor.swift in Sources */,
|
||||
B5ECDC2C1CA81CC700C7F112 /* CSDataStack+Transaction.swift in Sources */,
|
||||
B56321911BD65216006C9394 /* BaseDataTransaction+Importing.swift in Sources */,
|
||||
B546F95A1C99B17400D5AC55 /* CSCoreStore+Setup.swift in Sources */,
|
||||
@@ -1815,6 +1850,7 @@
|
||||
B598514A1C90289E00C99590 /* NSPersistentStoreCoordinator+Setup.swift in Sources */,
|
||||
B5FEC1901C9166E700532541 /* NSPersistentStore+Setup.swift in Sources */,
|
||||
B56321A11BD65216006C9394 /* ListMonitor.swift in Sources */,
|
||||
B5E1B5A51CAA4365007FD580 /* CSCoreStore+Observing.swift in Sources */,
|
||||
B56321881BD65216006C9394 /* BaseDataTransaction.swift in Sources */,
|
||||
B56321A31BD65216006C9394 /* DataStack+Migration.swift in Sources */,
|
||||
B56321901BD65216006C9394 /* ImportableUniqueObject.swift in Sources */,
|
||||
@@ -1874,6 +1910,7 @@
|
||||
B501FDE81CA8D20500BE22EF /* CSListObserver.swift in Sources */,
|
||||
B5D9E2F01CA2C317007A9D52 /* CoreStoreFetchedResultsController.swift in Sources */,
|
||||
B5D9E3461CA2C6C4007A9D52 /* GCDQueue.swift in Sources */,
|
||||
B5E1B5A31CAA4365007FD580 /* CSCoreStore+Observing.swift in Sources */,
|
||||
B5D9E2F11CA2C317007A9D52 /* ImportableUniqueObject.swift in Sources */,
|
||||
B5D9E2F21CA2C317007A9D52 /* CoreStore+Setup.swift in Sources */,
|
||||
B5D7A5B31CA3B738005C752B /* LegacySQLiteStore.swift in Sources */,
|
||||
@@ -1888,9 +1925,11 @@
|
||||
B5D9E2F81CA2C317007A9D52 /* SectionBy.swift in Sources */,
|
||||
B5D9E2F91CA2C317007A9D52 /* NSManagedObjectContext+Transaction.swift in Sources */,
|
||||
B5D9E2FA1CA2C317007A9D52 /* UnsafeDataTransaction+Observing.swift in Sources */,
|
||||
B5E1B5991CAA0C23007FD580 /* CSObjectObserver.swift in Sources */,
|
||||
B5ECDC061CA8138100C7F112 /* CSOrderBy.swift in Sources */,
|
||||
B5ECDC401CA836C800C7F112 /* CSInMemoryStore.swift in Sources */,
|
||||
B5D9E2FB1CA2C317007A9D52 /* MigrationChain.swift in Sources */,
|
||||
B5E1B5941CAA0C15007FD580 /* CSObjectMonitor.swift in Sources */,
|
||||
B5D9E2FC1CA2C317007A9D52 /* Tweak.swift in Sources */,
|
||||
B5D9E2FD1CA2C317007A9D52 /* OrderBy.swift in Sources */,
|
||||
B5D9E2FE1CA2C317007A9D52 /* NSManagedObjectContext+Setup.swift in Sources */,
|
||||
@@ -1925,6 +1964,7 @@
|
||||
B5ECDBE01CA6BB2B00C7F112 /* CSBaseDataTransaction+Querying.swift in Sources */,
|
||||
B5D9E3131CA2C317007A9D52 /* DataStack+Migration.swift in Sources */,
|
||||
B5D9E3141CA2C317007A9D52 /* BaseDataTransaction.swift in Sources */,
|
||||
B5E1B59E1CAA2568007FD580 /* CSDataStack+Observing.swift in Sources */,
|
||||
B5ECDC241CA81A3900C7F112 /* CSCoreStore+Querying.swift in Sources */,
|
||||
B5D9E3151CA2C317007A9D52 /* SaveResult.swift in Sources */,
|
||||
B529C2051CA4A2DB007E7EBD /* CSSaveResult.swift in Sources */,
|
||||
|
||||
@@ -42,7 +42,10 @@ public extension NSFetchedResultsController {
|
||||
fetchRequest: fetchRequest,
|
||||
from: from,
|
||||
sectionBy: sectionBy,
|
||||
fetchClauses: fetchClauses
|
||||
applyFetchClauses: { fetchRequest in
|
||||
|
||||
fetchClauses.forEach { $0.applyToFetchRequest(fetchRequest) }
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
@@ -57,7 +60,10 @@ public extension NSFetchedResultsController {
|
||||
fetchRequest: fetchRequest,
|
||||
from: from,
|
||||
sectionBy: sectionBy,
|
||||
fetchClauses: fetchClauses
|
||||
applyFetchClauses: { fetchRequest in
|
||||
|
||||
fetchClauses.forEach { $0.applyToFetchRequest(fetchRequest) }
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -46,6 +46,7 @@ public struct Tweak: FetchClause, QueryClause, DeleteClause {
|
||||
|
||||
/**
|
||||
Initializes a `Tweak` clause with a closure where the `NSFetchRequest` may be configured.
|
||||
- Important: `Tweak`'s closure is executed only just before the fetch occurs, so make sure that any values captured by the closure is not prone to race conditions. Also, some utilities (such as `ListMonitor`s) may keep `FetchClause`s in memory and may thus introduce retain cycles if reference captures are not handled properly.
|
||||
|
||||
- parameter customization: a list of key path strings to group results with
|
||||
*/
|
||||
|
||||
@@ -34,24 +34,21 @@ internal final class CoreStoreFetchedResultsController: NSFetchedResultsControll
|
||||
|
||||
// MARK: Internal
|
||||
|
||||
internal convenience init<T: NSManagedObject>(dataStack: DataStack, fetchRequest: NSFetchRequest, from: From<T>? = nil, sectionBy: SectionBy? = nil, fetchClauses: [FetchClause]) {
|
||||
internal convenience init<T: NSManagedObject>(dataStack: DataStack, fetchRequest: NSFetchRequest, from: From<T>? = nil, sectionBy: SectionBy? = nil, applyFetchClauses: (fetchRequest: NSFetchRequest) -> Void) {
|
||||
|
||||
self.init(
|
||||
context: dataStack.mainContext,
|
||||
fetchRequest: fetchRequest,
|
||||
from: from,
|
||||
sectionBy: sectionBy,
|
||||
fetchClauses: fetchClauses
|
||||
applyFetchClauses: applyFetchClauses
|
||||
)
|
||||
}
|
||||
|
||||
internal init<T: NSManagedObject>(context: NSManagedObjectContext, fetchRequest: NSFetchRequest, from: From<T>? = nil, sectionBy: SectionBy? = nil, fetchClauses: [FetchClause]) {
|
||||
internal init<T: NSManagedObject>(context: NSManagedObjectContext, fetchRequest: NSFetchRequest, from: From<T>? = nil, sectionBy: SectionBy? = nil, applyFetchClauses: (fetchRequest: NSFetchRequest) -> Void) {
|
||||
|
||||
from?.applyToFetchRequest(fetchRequest, context: context, applyAffectedStores: false)
|
||||
for clause in fetchClauses {
|
||||
|
||||
clause.applyToFetchRequest(fetchRequest)
|
||||
}
|
||||
applyFetchClauses(fetchRequest: fetchRequest)
|
||||
|
||||
if let from = from {
|
||||
|
||||
|
||||
116
Sources/ObjectiveC/CSCoreStore+Observing.swift
Normal file
116
Sources/ObjectiveC/CSCoreStore+Observing.swift
Normal file
@@ -0,0 +1,116 @@
|
||||
//
|
||||
// CSCoreStore+Observing.swift
|
||||
// CoreStore
|
||||
//
|
||||
// Copyright © 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: - CSCoreStore
|
||||
|
||||
@available(OSX, unavailable)
|
||||
public extension CSCoreStore {
|
||||
|
||||
/**
|
||||
Using the `defaultStack`, creates an `CSObjectMonitor` for the specified `NSManagedObject`. Multiple `CSObjectObserver`s may then register themselves to be notified when changes are made to the `NSManagedObject`.
|
||||
|
||||
- parameter object: the `NSManagedObject` to observe changes from
|
||||
- returns: a `CSObjectMonitor` that monitors changes to `object`
|
||||
*/
|
||||
@objc
|
||||
@warn_unused_result
|
||||
public static func monitorObject(object: NSManagedObject) -> CSObjectMonitor {
|
||||
|
||||
return self.defaultStack.monitorObject(object)
|
||||
}
|
||||
|
||||
/**
|
||||
Using the `defaultStack`, creates a `CSListMonitor` for a list of `NSManagedObject`s that satisfy the specified fetch clauses. Multiple `CSListObserver`s may then register themselves to be notified when changes are made to the list.
|
||||
|
||||
- parameter from: a `CSFrom` clause indicating the entity type
|
||||
- parameter fetchClauses: a series of `CSFetchClause` instances for fetching the object list. Accepts `CSWhere`, `CSOrderBy`, and `CSTweak` clauses.
|
||||
- returns: a `CSListMonitor` instance that monitors changes to the list
|
||||
*/
|
||||
@objc
|
||||
@warn_unused_result
|
||||
public static func monitorListFrom(from: CSFrom, fetchClauses: [CSFetchClause]) -> CSListMonitor {
|
||||
|
||||
return self.defaultStack.monitorListFrom(from, fetchClauses: fetchClauses)
|
||||
}
|
||||
|
||||
/**
|
||||
Using the `defaultStack`, asynchronously creates a `CSListMonitor` for a list of `NSManagedObject`s that satisfy the specified fetch clauses. Multiple `CSListObserver`s may then register themselves to be notified when changes are made to the list. Since `NSFetchedResultsController` greedily locks the persistent store on initial fetch, you may prefer this method instead of the synchronous counterpart to avoid deadlocks while background updates/saves are being executed.
|
||||
|
||||
- parameter createAsynchronously: the closure that receives the created `CSListMonitor` instance
|
||||
- parameter from: a `CSFrom` clause indicating the entity type
|
||||
- parameter fetchClauses: a series of `CSFetchClause` instances for fetching the object list. Accepts `CSWhere`, `CSOrderBy`, and `CSTweak` clauses.
|
||||
*/
|
||||
@objc
|
||||
public static func monitorListByCreatingAsynchronously(createAsynchronously: (CSListMonitor) -> Void, from: CSFrom, fetchClauses: [CSFetchClause]) {
|
||||
|
||||
return self.defaultStack.monitorListByCreatingAsynchronously(
|
||||
createAsynchronously,
|
||||
from: from,
|
||||
fetchClauses: fetchClauses
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
Using the `defaultStack`, creates a `CSListMonitor` for a sectioned list of `NSManagedObject`s that satisfy the specified fetch clauses. Multiple `CSListObserver`s may then register themselves to be notified when changes are made to the list.
|
||||
|
||||
- parameter from: a `CSFrom` clause indicating the entity type
|
||||
- parameter sectionBy: a `CSSectionBy` clause indicating the keyPath for the attribute to use when sorting the list into sections.
|
||||
- parameter fetchClauses: a series of `CSFetchClause` instances for fetching the object list. Accepts `CSWhere`, `CSOrderBy`, and `CSTweak` clauses.
|
||||
- returns: a `CSListMonitor` instance that monitors changes to the list
|
||||
*/
|
||||
@objc
|
||||
@warn_unused_result
|
||||
public static func monitorSectionedListFrom(from: CSFrom, sectionBy: CSSectionBy, fetchClauses: [CSFetchClause]) -> CSListMonitor {
|
||||
|
||||
return self.defaultStack.monitorSectionedListFrom(
|
||||
from,
|
||||
sectionBy: sectionBy,
|
||||
fetchClauses: fetchClauses
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
Using the `defaultStack`, asynchronously creates a `CSListMonitor` for a sectioned list of `NSManagedObject`s that satisfy the specified fetch clauses. Multiple `CSListObserver`s may then register themselves to be notified when changes are made to the list. Since `NSFetchedResultsController` greedily locks the persistent store on initial fetch, you may prefer this method instead of the synchronous counterpart to avoid deadlocks while background updates/saves are being executed.
|
||||
|
||||
- parameter createAsynchronously: the closure that receives the created `CSListMonitor` instance
|
||||
- parameter from: a `CSFrom` clause indicating the entity type
|
||||
- parameter sectionBy: a `CSSectionBy` clause indicating the keyPath for the attribute to use when sorting the list into sections.
|
||||
- parameter fetchClauses: a series of `CSFetchClause` instances for fetching the object list. Accepts `CSWhere`, `CSOrderBy`, and `CSTweak` clauses.
|
||||
*/
|
||||
@objc
|
||||
public static func monitorSectionedListByCreatingAsynchronously(createAsynchronously: (CSListMonitor) -> Void, from: CSFrom, sectionBy: CSSectionBy, fetchClauses: [CSFetchClause]) {
|
||||
|
||||
self.defaultStack.monitorSectionedListByCreatingAsynchronously(
|
||||
createAsynchronously,
|
||||
from: from,
|
||||
sectionBy: sectionBy,
|
||||
fetchClauses: fetchClauses
|
||||
)
|
||||
}
|
||||
}
|
||||
183
Sources/ObjectiveC/CSDataStack+Observing.swift
Normal file
183
Sources/ObjectiveC/CSDataStack+Observing.swift
Normal file
@@ -0,0 +1,183 @@
|
||||
//
|
||||
// CSDataStack+Observing.swift
|
||||
// CoreStore
|
||||
//
|
||||
// Copyright © 2016 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: - CSDataStack
|
||||
|
||||
@available(OSX, unavailable)
|
||||
public extension CSDataStack {
|
||||
|
||||
/**
|
||||
Creates a `CSObjectMonitor` for the specified `NSManagedObject`. Multiple `ObjectObserver`s may then register themselves to be notified when changes are made to the `NSManagedObject`.
|
||||
|
||||
- parameter object: the `NSManagedObject` to observe changes from
|
||||
- returns: a `ObjectMonitor` that monitors changes to `object`
|
||||
*/
|
||||
@objc
|
||||
@warn_unused_result
|
||||
public func monitorObject(object: NSManagedObject) -> CSObjectMonitor {
|
||||
|
||||
return bridge {
|
||||
|
||||
self.bridgeToSwift.monitorObject(object)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
Creates a `CSListMonitor` for a list of `NSManagedObject`s that satisfy the specified fetch clauses. Multiple `CSListObserver`s may then register themselves to be notified when changes are made to the list.
|
||||
|
||||
- parameter from: a `CSFrom` clause indicating the entity type
|
||||
- parameter fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `CSWhere`, `CSOrderBy`, and `CSTweak` clauses.
|
||||
- returns: a `CSListMonitor` instance that monitors changes to the list
|
||||
*/
|
||||
@objc
|
||||
@warn_unused_result
|
||||
public func monitorListFrom(from: CSFrom, fetchClauses: [CSFetchClause]) -> CSListMonitor {
|
||||
|
||||
CoreStore.assert(
|
||||
NSThread.isMainThread(),
|
||||
"Attempted to observe objects from \(typeName(self)) outside the main thread."
|
||||
)
|
||||
CoreStore.assert(
|
||||
fetchClauses.contains { $0 is CSOrderBy },
|
||||
"A CSListMonitor requires a CSOrderBy clause."
|
||||
)
|
||||
return bridge {
|
||||
|
||||
ListMonitor(
|
||||
dataStack: self.bridgeToSwift,
|
||||
from: from.bridgeToSwift,
|
||||
sectionBy: nil,
|
||||
applyFetchClauses: { fetchRequest in
|
||||
|
||||
fetchClauses.forEach { $0.applyToFetchRequest(fetchRequest) }
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
Asynchronously creates a `CSListMonitor` for a list of `NSManagedObject`s that satisfy the specified fetch clauses. Multiple `CSListObserver`s may then register themselves to be notified when changes are made to the list. Since `NSFetchedResultsController` greedily locks the persistent store on initial fetch, you may prefer this method instead of the synchronous counterpart to avoid deadlocks while background updates/saves are being executed.
|
||||
|
||||
- parameter createAsynchronously: the closure that receives the created `CSListMonitor` instance
|
||||
- parameter from: a `CSFrom` clause indicating the entity type
|
||||
- parameter fetchClauses: a series of `CSFetchClause` instances for fetching the object list. Accepts `CSWhere`, `CSOrderBy`, and `CSTweak` clauses.
|
||||
*/
|
||||
@objc
|
||||
public func monitorListByCreatingAsynchronously(createAsynchronously: (CSListMonitor) -> Void, from: CSFrom, fetchClauses: [CSFetchClause]) {
|
||||
|
||||
CoreStore.assert(
|
||||
NSThread.isMainThread(),
|
||||
"Attempted to observe objects from \(typeName(self)) outside the main thread."
|
||||
)
|
||||
CoreStore.assert(
|
||||
fetchClauses.contains { $0 is CSOrderBy },
|
||||
"A CSListMonitor requires an CSOrderBy clause."
|
||||
)
|
||||
_ = ListMonitor(
|
||||
dataStack: self.bridgeToSwift,
|
||||
from: from.bridgeToSwift,
|
||||
sectionBy: nil,
|
||||
applyFetchClauses: { fetchRequest in
|
||||
|
||||
fetchClauses.forEach { $0.applyToFetchRequest(fetchRequest) }
|
||||
},
|
||||
createAsynchronously: {
|
||||
|
||||
createAsynchronously($0.bridgeToObjectiveC)
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
Creates a `CSListMonitor` for a sectioned list of `NSManagedObject`s that satisfy the specified fetch clauses. Multiple `ListObserver`s may then register themselves to be notified when changes are made to the list.
|
||||
|
||||
- parameter from: a `CSFrom` clause indicating the entity type
|
||||
- parameter sectionBy: a `CSSectionBy` clause indicating the keyPath for the attribute to use when sorting the list into sections.
|
||||
- parameter fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `CSWhere`, `CSOrderBy`, and `CSTweak` clauses.
|
||||
- returns: a `CSListMonitor` instance that monitors changes to the list
|
||||
*/
|
||||
@objc
|
||||
@warn_unused_result
|
||||
public func monitorSectionedListFrom(from: CSFrom, sectionBy: CSSectionBy, fetchClauses: [CSFetchClause]) -> CSListMonitor {
|
||||
|
||||
CoreStore.assert(
|
||||
NSThread.isMainThread(),
|
||||
"Attempted to observe objects from \(typeName(self)) outside the main thread."
|
||||
)
|
||||
CoreStore.assert(
|
||||
fetchClauses.contains { $0 is CSOrderBy },
|
||||
"A CSListMonitor requires an CSOrderBy clause."
|
||||
)
|
||||
return bridge {
|
||||
|
||||
ListMonitor(
|
||||
dataStack: self.bridgeToSwift,
|
||||
from: from.bridgeToSwift,
|
||||
sectionBy: sectionBy.bridgeToSwift,
|
||||
applyFetchClauses: { fetchRequest in
|
||||
|
||||
fetchClauses.forEach { $0.applyToFetchRequest(fetchRequest) }
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
Asynchronously creates a `CSListMonitor` for a sectioned list of `NSManagedObject`s that satisfy the specified fetch clauses. Multiple `CSListObserver`s may then register themselves to be notified when changes are made to the list. Since `NSFetchedResultsController` greedily locks the persistent store on initial fetch, you may prefer this method instead of the synchronous counterpart to avoid deadlocks while background updates/saves are being executed.
|
||||
|
||||
- parameter createAsynchronously: the closure that receives the created `CSListMonitor` instance
|
||||
- parameter from: a `CSFrom` clause indicating the entity type
|
||||
- parameter sectionBy: a `CSSectionBy` clause indicating the keyPath for the attribute to use when sorting the list into sections.
|
||||
- parameter fetchClauses: a series of `CSFetchClause` instances for fetching the object list. Accepts `CSWhere`, `CSOrderBy`, and `CSTweak` clauses.
|
||||
*/
|
||||
public func monitorSectionedListByCreatingAsynchronously(createAsynchronously: (CSListMonitor) -> Void, from: CSFrom, sectionBy: CSSectionBy, fetchClauses: [CSFetchClause]) {
|
||||
|
||||
CoreStore.assert(
|
||||
NSThread.isMainThread(),
|
||||
"Attempted to observe objects from \(typeName(self)) outside the main thread."
|
||||
)
|
||||
CoreStore.assert(
|
||||
fetchClauses.contains { $0 is CSOrderBy },
|
||||
"A CSListMonitor requires an CSOrderBy clause."
|
||||
)
|
||||
_ = ListMonitor(
|
||||
dataStack: self.bridgeToSwift,
|
||||
from: from.bridgeToSwift,
|
||||
sectionBy: sectionBy.bridgeToSwift,
|
||||
applyFetchClauses: { fetchRequest in
|
||||
|
||||
fetchClauses.forEach { $0.applyToFetchRequest(fetchRequest) }
|
||||
},
|
||||
createAsynchronously: {
|
||||
|
||||
createAsynchronously($0.bridgeToObjectiveC)
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -485,7 +485,7 @@ public final class CSListMonitor: NSObject, CoreStoreObjectiveCType {
|
||||
- parameter observer: a `CSListObserver` to unregister notifications to
|
||||
*/
|
||||
@objc
|
||||
public func removeObserver(observer: CSListObserver) {
|
||||
public func removeListObserver(observer: CSListObserver) {
|
||||
|
||||
self.bridgeToSwift.unregisterObserver(observer)
|
||||
}
|
||||
@@ -509,6 +509,7 @@ public final class CSListMonitor: NSObject, CoreStoreObjectiveCType {
|
||||
|
||||
- parameter fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses. Note that only specified clauses will be changed; unspecified clauses will use previous values.
|
||||
*/
|
||||
@objc
|
||||
public func refetch(fetchClauses: [CSFetchClause]) {
|
||||
|
||||
self.bridgeToSwift.refetch { (fetchRequest) in
|
||||
@@ -537,6 +538,7 @@ public final class CSListMonitor: NSObject, CoreStoreObjectiveCType {
|
||||
|
||||
// MARK: CoreStoreObjectiveCType
|
||||
|
||||
@nonobjc
|
||||
public let bridgeToSwift: ListMonitor<NSManagedObject>
|
||||
|
||||
@nonobjc
|
||||
|
||||
141
Sources/ObjectiveC/CSObjectMonitor.swift
Normal file
141
Sources/ObjectiveC/CSObjectMonitor.swift
Normal file
@@ -0,0 +1,141 @@
|
||||
//
|
||||
// CSObjectMonitor.swift
|
||||
// CoreStore
|
||||
//
|
||||
// Copyright © 2016 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: - CSObjectMonitor
|
||||
|
||||
/**
|
||||
The `CSObjectMonitor` serves as the Objective-C bridging type for `ObjectMonitor<T>`.
|
||||
*/
|
||||
@available(OSX, unavailable)
|
||||
@objc
|
||||
public final class CSObjectMonitor: NSObject, CoreStoreObjectiveCType {
|
||||
|
||||
/**
|
||||
Returns the `NSManagedObject` instance being observed, or `nil` if the object was already deleted.
|
||||
*/
|
||||
public var object: NSManagedObject? {
|
||||
|
||||
return self.bridgeToSwift.object
|
||||
}
|
||||
|
||||
/**
|
||||
Returns `YES` if the `NSManagedObject` instance being observed still exists, or `NO` if the object was already deleted.
|
||||
*/
|
||||
public var isObjectDeleted: Bool {
|
||||
|
||||
return self.bridgeToSwift.isObjectDeleted
|
||||
}
|
||||
|
||||
/**
|
||||
Registers a `CSObjectObserver` to be notified when changes to the receiver's `object` are made.
|
||||
|
||||
To prevent retain-cycles, `CSObjectMonitor` only keeps `weak` references to its observers.
|
||||
|
||||
For thread safety, this method needs to be called from the main thread. An assertion failure will occur (on debug builds only) if called from any thread other than the main thread.
|
||||
|
||||
Calling `-addObjectObserver:` multiple times on the same observer is safe, as `CSObjectMonitor` unregisters previous notifications to the observer before re-registering them.
|
||||
|
||||
- parameter observer: an `CSObjectObserver` to send change notifications to
|
||||
*/
|
||||
public func addObjectObserver(observer: CSObjectObserver) {
|
||||
|
||||
let swift = self.bridgeToSwift
|
||||
swift.unregisterObserver(observer)
|
||||
swift.registerObserver(
|
||||
observer,
|
||||
willChangeObject: { (observer, monitor, object) in
|
||||
|
||||
observer.objectMonitor?(monitor.bridgeToObjectiveC, willUpdateObject: object)
|
||||
},
|
||||
didDeleteObject: { (observer, monitor, object) in
|
||||
|
||||
observer.objectMonitor?(monitor.bridgeToObjectiveC, didDeleteObject: object)
|
||||
},
|
||||
didUpdateObject: { (observer, monitor, object, changedPersistentKeys) in
|
||||
|
||||
observer.objectMonitor?(monitor.bridgeToObjectiveC, didUpdateObject: object, changedPersistentKeys: changedPersistentKeys)
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
Unregisters an `CSObjectObserver` from receiving notifications for changes to the receiver's `object`.
|
||||
|
||||
For thread safety, this method needs to be called from the main thread. An assertion failure will occur (on debug builds only) if called from any thread other than the main thread.
|
||||
|
||||
- parameter observer: an `CSObjectObserver` to unregister notifications to
|
||||
*/
|
||||
public func removeObjectObserver(observer: CSObjectObserver) {
|
||||
|
||||
self.bridgeToSwift.unregisterObserver(observer)
|
||||
}
|
||||
|
||||
|
||||
// MARK: NSObject
|
||||
|
||||
public override var hash: Int {
|
||||
|
||||
return self.bridgeToSwift.hashValue
|
||||
}
|
||||
|
||||
public override func isEqual(object: AnyObject?) -> Bool {
|
||||
|
||||
guard let object = object as? CSObjectMonitor else {
|
||||
|
||||
return false
|
||||
}
|
||||
return self.bridgeToSwift == object.bridgeToSwift
|
||||
}
|
||||
|
||||
|
||||
// MARK: CoreStoreObjectiveCType
|
||||
|
||||
@nonobjc
|
||||
public let bridgeToSwift: ObjectMonitor<NSManagedObject>
|
||||
|
||||
@nonobjc
|
||||
public required init<T: NSManagedObject>(_ swiftValue: ObjectMonitor<T>) {
|
||||
|
||||
self.bridgeToSwift = swiftValue.upcast()
|
||||
super.init()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: - ObjectMonitor
|
||||
|
||||
extension ObjectMonitor: CoreStoreSwiftType {
|
||||
|
||||
// MARK: CoreStoreSwiftType
|
||||
|
||||
public var bridgeToObjectiveC: CSObjectMonitor {
|
||||
|
||||
return CSObjectMonitor(self)
|
||||
}
|
||||
}
|
||||
70
Sources/ObjectiveC/CSObjectObserver.swift
Normal file
70
Sources/ObjectiveC/CSObjectObserver.swift
Normal file
@@ -0,0 +1,70 @@
|
||||
//
|
||||
// CSObjectObserver.swift
|
||||
// CoreStore
|
||||
//
|
||||
// Copyright © 2016 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: - CSObjectObserver
|
||||
|
||||
/**
|
||||
Implement the `CSObjectObserver` protocol to observe changes to a single `NSManagedObject` instance. `CSObjectObserver`s may register themselves to a `CSObjectMonitor`'s `-addObjectObserver:` method:
|
||||
```
|
||||
CSObjectMonitor *monitor = [CSCoreStore monitorObject:myObject];
|
||||
[monitor addObjectObserver:self];
|
||||
```
|
||||
*/
|
||||
@available(OSX, unavailable)
|
||||
@objc
|
||||
public protocol CSObjectObserver: class, AnyObject {
|
||||
|
||||
/**
|
||||
Handles processing just before a change to the observed `object` occurs
|
||||
|
||||
- parameter monitor: the `CSObjectMonitor` monitoring the object being observed
|
||||
- parameter object: the `NSManagedObject` instance being observed
|
||||
*/
|
||||
@objc
|
||||
optional func objectMonitor(monitor: CSObjectMonitor, willUpdateObject object: NSManagedObject)
|
||||
|
||||
/**
|
||||
Handles processing right after a change to the observed `object` occurs
|
||||
|
||||
- parameter monitor: the `CSObjectMonitor` monitoring the object being observed
|
||||
- parameter object: the `NSManagedObject` instance being observed
|
||||
- parameter changedPersistentKeys: an `NSSet` of key paths for the attributes that were changed. Note that `changedPersistentKeys` only contains keys for attributes/relationships present in the persistent store, thus transient properties will not be reported.
|
||||
*/
|
||||
@objc
|
||||
optional func objectMonitor(monitor: CSObjectMonitor, didUpdateObject object: NSManagedObject, changedPersistentKeys: Set<KeyPath>)
|
||||
|
||||
/**
|
||||
Handles processing right after `object` is deleted
|
||||
|
||||
- parameter monitor: the `CSObjectMonitor` monitoring the object being observed
|
||||
- parameter object: the `NSManagedObject` instance being observed
|
||||
*/
|
||||
@objc
|
||||
optional func objectMonitor(monitor: CSObjectMonitor, didDeleteObject object: NSManagedObject)
|
||||
}
|
||||
@@ -33,7 +33,7 @@ import CoreData
|
||||
public extension CoreStore {
|
||||
|
||||
/**
|
||||
Using the `defaultStack`, creates a `ObjectMonitor` for the specified `NSManagedObject`. Multiple `ObjectObserver`s may then register themselves to be notified when changes are made to the `NSManagedObject`.
|
||||
Using the `defaultStack`, creates an `ObjectMonitor` for the specified `NSManagedObject`. Multiple `ObjectObserver`s may then register themselves to be notified when changes are made to the `NSManagedObject`.
|
||||
|
||||
- parameter object: the `NSManagedObject` to observe changes from
|
||||
- returns: a `ObjectMonitor` that monitors changes to `object`
|
||||
@@ -52,9 +52,9 @@ public extension CoreStore {
|
||||
- returns: a `ListMonitor` instance that monitors changes to the list
|
||||
*/
|
||||
@warn_unused_result
|
||||
public static func monitorList<T: NSManagedObject>(from: From<T>, _ queryClauses: FetchClause...) -> ListMonitor<T> {
|
||||
public static func monitorList<T: NSManagedObject>(from: From<T>, _ fetchClauses: FetchClause...) -> ListMonitor<T> {
|
||||
|
||||
return self.defaultStack.monitorList(from, queryClauses)
|
||||
return self.defaultStack.monitorList(from, fetchClauses)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -65,9 +65,9 @@ public extension CoreStore {
|
||||
- returns: a `ListMonitor` instance that monitors changes to the list
|
||||
*/
|
||||
@warn_unused_result
|
||||
public static func monitorList<T: NSManagedObject>(from: From<T>, _ queryClauses: [FetchClause]) -> ListMonitor<T> {
|
||||
public static func monitorList<T: NSManagedObject>(from: From<T>, _ fetchClauses: [FetchClause]) -> ListMonitor<T> {
|
||||
|
||||
return self.defaultStack.monitorList(from, queryClauses)
|
||||
return self.defaultStack.monitorList(from, fetchClauses)
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -36,7 +36,7 @@ import CoreData
|
||||
public extension DataStack {
|
||||
|
||||
/**
|
||||
Creates a `ObjectMonitor` for the specified `NSManagedObject`. Multiple `ObjectObserver`s may then register themselves to be notified when changes are made to the `NSManagedObject`.
|
||||
Creates an `ObjectMonitor` for the specified `NSManagedObject`. Multiple `ObjectObserver`s may then register themselves to be notified when changes are made to the `NSManagedObject`.
|
||||
|
||||
- parameter object: the `NSManagedObject` to observe changes from
|
||||
- returns: a `ObjectMonitor` that monitors changes to `object`
|
||||
@@ -48,11 +48,7 @@ public extension DataStack {
|
||||
NSThread.isMainThread(),
|
||||
"Attempted to observe objects from \(typeName(self)) outside the main thread."
|
||||
)
|
||||
|
||||
return ObjectMonitor(
|
||||
dataStack: self,
|
||||
object: object
|
||||
)
|
||||
return ObjectMonitor(dataStack: self, object: object)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -83,15 +79,17 @@ public extension DataStack {
|
||||
"Attempted to observe objects from \(typeName(self)) outside the main thread."
|
||||
)
|
||||
CoreStore.assert(
|
||||
fetchClauses.filter { $0 is OrderBy }.count > 0,
|
||||
fetchClauses.contains { $0 is OrderBy },
|
||||
"A ListMonitor requires an OrderBy clause."
|
||||
)
|
||||
|
||||
return ListMonitor(
|
||||
dataStack: self,
|
||||
from: from,
|
||||
sectionBy: nil,
|
||||
fetchClauses: fetchClauses
|
||||
applyFetchClauses: { fetchRequest in
|
||||
|
||||
fetchClauses.forEach { $0.applyToFetchRequest(fetchRequest) }
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
@@ -121,15 +119,17 @@ public extension DataStack {
|
||||
"Attempted to observe objects from \(typeName(self)) outside the main thread."
|
||||
)
|
||||
CoreStore.assert(
|
||||
fetchClauses.filter { $0 is OrderBy }.count > 0,
|
||||
fetchClauses.contains { $0 is OrderBy },
|
||||
"A ListMonitor requires an OrderBy clause."
|
||||
)
|
||||
|
||||
_ = ListMonitor(
|
||||
dataStack: self,
|
||||
from: from,
|
||||
sectionBy: nil,
|
||||
fetchClauses: fetchClauses,
|
||||
applyFetchClauses: { fetchRequest in
|
||||
|
||||
fetchClauses.forEach { $0.applyToFetchRequest(fetchRequest) }
|
||||
},
|
||||
createAsynchronously: createAsynchronously
|
||||
)
|
||||
}
|
||||
@@ -164,7 +164,7 @@ public extension DataStack {
|
||||
"Attempted to observe objects from \(typeName(self)) outside the main thread."
|
||||
)
|
||||
CoreStore.assert(
|
||||
fetchClauses.filter { $0 is OrderBy }.count > 0,
|
||||
fetchClauses.contains { $0 is OrderBy },
|
||||
"A ListMonitor requires an OrderBy clause."
|
||||
)
|
||||
|
||||
@@ -172,7 +172,10 @@ public extension DataStack {
|
||||
dataStack: self,
|
||||
from: from,
|
||||
sectionBy: sectionBy,
|
||||
fetchClauses: fetchClauses
|
||||
applyFetchClauses: { fetchRequest in
|
||||
|
||||
fetchClauses.forEach { $0.applyToFetchRequest(fetchRequest) }
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
@@ -204,7 +207,7 @@ public extension DataStack {
|
||||
"Attempted to observe objects from \(typeName(self)) outside the main thread."
|
||||
)
|
||||
CoreStore.assert(
|
||||
fetchClauses.filter { $0 is OrderBy }.count > 0,
|
||||
fetchClauses.contains { $0 is OrderBy },
|
||||
"A ListMonitor requires an OrderBy clause."
|
||||
)
|
||||
|
||||
@@ -212,7 +215,10 @@ public extension DataStack {
|
||||
dataStack: self,
|
||||
from: from,
|
||||
sectionBy: sectionBy,
|
||||
fetchClauses: fetchClauses,
|
||||
applyFetchClauses: { fetchRequest in
|
||||
|
||||
fetchClauses.forEach { $0.applyToFetchRequest(fetchRequest) }
|
||||
},
|
||||
createAsynchronously: createAsynchronously
|
||||
)
|
||||
}
|
||||
|
||||
@@ -647,50 +647,50 @@ public final class ListMonitor<T: NSManagedObject>: Hashable {
|
||||
internal var didInsertSectionKey: Void?
|
||||
internal var didDeleteSectionKey: Void?
|
||||
|
||||
internal convenience init(dataStack: DataStack, from: From<T>, sectionBy: SectionBy?, fetchClauses: [FetchClause]) {
|
||||
internal convenience init(dataStack: DataStack, from: From<T>, sectionBy: SectionBy?, applyFetchClauses: (fetchRequest: NSFetchRequest) -> Void) {
|
||||
|
||||
self.init(
|
||||
context: dataStack.mainContext,
|
||||
transactionQueue: dataStack.childTransactionQueue,
|
||||
from: from,
|
||||
sectionBy: sectionBy,
|
||||
fetchClauses: fetchClauses,
|
||||
applyFetchClauses: applyFetchClauses,
|
||||
createAsynchronously: nil
|
||||
)
|
||||
}
|
||||
|
||||
internal convenience init(dataStack: DataStack, from: From<T>, sectionBy: SectionBy?, fetchClauses: [FetchClause], createAsynchronously: (ListMonitor<T>) -> Void) {
|
||||
internal convenience init(dataStack: DataStack, from: From<T>, sectionBy: SectionBy?, applyFetchClauses: (fetchRequest: NSFetchRequest) -> Void, createAsynchronously: (ListMonitor<T>) -> Void) {
|
||||
|
||||
self.init(
|
||||
context: dataStack.mainContext,
|
||||
transactionQueue: dataStack.childTransactionQueue,
|
||||
from: from,
|
||||
sectionBy: sectionBy,
|
||||
fetchClauses: fetchClauses,
|
||||
applyFetchClauses: applyFetchClauses,
|
||||
createAsynchronously: createAsynchronously
|
||||
)
|
||||
}
|
||||
|
||||
internal convenience init(unsafeTransaction: UnsafeDataTransaction, from: From<T>, sectionBy: SectionBy?, fetchClauses: [FetchClause]) {
|
||||
internal convenience init(unsafeTransaction: UnsafeDataTransaction, from: From<T>, sectionBy: SectionBy?, applyFetchClauses: (fetchRequest: NSFetchRequest) -> Void) {
|
||||
|
||||
self.init(
|
||||
context: unsafeTransaction.context,
|
||||
transactionQueue: unsafeTransaction.transactionQueue,
|
||||
from: from,
|
||||
sectionBy: sectionBy,
|
||||
fetchClauses: fetchClauses,
|
||||
applyFetchClauses: applyFetchClauses,
|
||||
createAsynchronously: nil
|
||||
)
|
||||
}
|
||||
|
||||
internal convenience init(unsafeTransaction: UnsafeDataTransaction, from: From<T>, sectionBy: SectionBy?, fetchClauses: [FetchClause], createAsynchronously: (ListMonitor<T>) -> Void) {
|
||||
internal convenience init(unsafeTransaction: UnsafeDataTransaction, from: From<T>, sectionBy: SectionBy?, applyFetchClauses: (fetchRequest: NSFetchRequest) -> Void, createAsynchronously: (ListMonitor<T>) -> Void) {
|
||||
|
||||
self.init(
|
||||
context: unsafeTransaction.context,
|
||||
transactionQueue: unsafeTransaction.transactionQueue,
|
||||
from: from,
|
||||
sectionBy: sectionBy,
|
||||
fetchClauses: fetchClauses,
|
||||
applyFetchClauses: applyFetchClauses,
|
||||
createAsynchronously: createAsynchronously
|
||||
)
|
||||
}
|
||||
@@ -984,7 +984,7 @@ public final class ListMonitor<T: NSManagedObject>: Hashable {
|
||||
setAssociatedRetainedObject(nilValue, forKey: &self.didDeleteSectionKey, inObject: observer)
|
||||
}
|
||||
|
||||
internal func refetch(applyClauses: (fetchRequest: NSFetchRequest) -> Void) {
|
||||
internal func refetch(applyFetchClauses: (fetchRequest: NSFetchRequest) -> Void) {
|
||||
|
||||
CoreStore.assert(
|
||||
NSThread.isMainThread(),
|
||||
@@ -1000,6 +1000,7 @@ public final class ListMonitor<T: NSManagedObject>: Hashable {
|
||||
object: self
|
||||
)
|
||||
}
|
||||
self.applyFetchClauses = applyFetchClauses
|
||||
|
||||
self.taskGroup.notify(.Main) { [weak self] () -> Void in
|
||||
|
||||
@@ -1009,7 +1010,7 @@ public final class ListMonitor<T: NSManagedObject>: Hashable {
|
||||
}
|
||||
|
||||
self.fetchedResultsControllerDelegate.enabled = false
|
||||
applyClauses(fetchRequest: self.fetchedResultsController.fetchRequest)
|
||||
self.applyFetchClauses(fetchRequest: self.fetchedResultsController.fetchRequest)
|
||||
|
||||
self.transactionQueue.async { [weak self] in
|
||||
|
||||
@@ -1055,6 +1056,7 @@ public final class ListMonitor<T: NSManagedObject>: Hashable {
|
||||
private var observerForDidChangePersistentStore: NotificationObserver!
|
||||
private let taskGroup = GCDGroup()
|
||||
private let transactionQueue: GCDQueue
|
||||
private var applyFetchClauses: (fetchRequest: NSFetchRequest) -> Void
|
||||
|
||||
private var isPersistentStoreChanging: Bool = false {
|
||||
|
||||
@@ -1077,7 +1079,7 @@ public final class ListMonitor<T: NSManagedObject>: Hashable {
|
||||
}
|
||||
}
|
||||
|
||||
private init(context: NSManagedObjectContext, transactionQueue: GCDQueue, from: From<T>, sectionBy: SectionBy?, fetchClauses: [FetchClause], createAsynchronously: ((ListMonitor<T>) -> Void)?) {
|
||||
private init(context: NSManagedObjectContext, transactionQueue: GCDQueue, from: From<T>, sectionBy: SectionBy?, applyFetchClauses: (fetchRequest: NSFetchRequest) -> Void, createAsynchronously: ((ListMonitor<T>) -> Void)?) {
|
||||
|
||||
let fetchRequest = NSFetchRequest()
|
||||
fetchRequest.fetchLimit = 0
|
||||
@@ -1091,7 +1093,7 @@ public final class ListMonitor<T: NSManagedObject>: Hashable {
|
||||
fetchRequest: fetchRequest,
|
||||
from: from,
|
||||
sectionBy: sectionBy,
|
||||
fetchClauses: fetchClauses
|
||||
applyFetchClauses: applyFetchClauses
|
||||
)
|
||||
|
||||
let fetchedResultsControllerDelegate = FetchedResultsControllerDelegate()
|
||||
@@ -1108,6 +1110,7 @@ public final class ListMonitor<T: NSManagedObject>: Hashable {
|
||||
self.sectionIndexTransformer = { $0 }
|
||||
}
|
||||
self.transactionQueue = transactionQueue
|
||||
self.applyFetchClauses = applyFetchClauses
|
||||
|
||||
fetchedResultsControllerDelegate.handler = self
|
||||
fetchedResultsControllerDelegate.fetchedResultsController = fetchedResultsController
|
||||
@@ -1134,7 +1137,7 @@ public final class ListMonitor<T: NSManagedObject>: Hashable {
|
||||
|
||||
return
|
||||
}
|
||||
self.refetch(fetchClauses)
|
||||
self.refetch(self.applyFetchClauses)
|
||||
}
|
||||
)
|
||||
|
||||
@@ -1157,7 +1160,7 @@ public final class ListMonitor<T: NSManagedObject>: Hashable {
|
||||
|
||||
if previousStores != currentStores {
|
||||
|
||||
self.refetch(fetchClauses)
|
||||
self.refetch(self.applyFetchClauses)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -74,13 +74,63 @@ public final class ObjectMonitor<T: NSManagedObject> {
|
||||
*/
|
||||
public func addObserver<U: ObjectObserver where U.ObjectEntityType == T>(observer: U) {
|
||||
|
||||
self.unregisterObserver(observer)
|
||||
self.registerObserver(
|
||||
observer,
|
||||
willChangeObject: { (observer, monitor, object) in
|
||||
|
||||
observer.objectMonitor(monitor, willUpdateObject: object)
|
||||
},
|
||||
didDeleteObject: { (observer, monitor, object) in
|
||||
|
||||
observer.objectMonitor(monitor, didDeleteObject: object)
|
||||
},
|
||||
didUpdateObject: { (observer, monitor, object, changedPersistentKeys) in
|
||||
|
||||
observer.objectMonitor(monitor, didUpdateObject: object, changedPersistentKeys: changedPersistentKeys)
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
Unregisters an `ObjectObserver` from receiving notifications for changes to the receiver's `object`.
|
||||
|
||||
For thread safety, this method needs to be called from the main thread. An assertion failure will occur (on debug builds only) if called from any thread other than the main thread.
|
||||
|
||||
- parameter observer: an `ObjectObserver` to unregister notifications to
|
||||
*/
|
||||
public func removeObserver<U: ObjectObserver where U.ObjectEntityType == T>(observer: U) {
|
||||
|
||||
self.unregisterObserver(observer)
|
||||
}
|
||||
|
||||
|
||||
// MARK: Hashable
|
||||
|
||||
public var hashValue: Int {
|
||||
|
||||
return ObjectIdentifier(self).hashValue
|
||||
}
|
||||
|
||||
|
||||
// MARK: Internal
|
||||
|
||||
internal convenience init(dataStack: DataStack, object: T) {
|
||||
|
||||
self.init(context: dataStack.mainContext, object: object)
|
||||
}
|
||||
|
||||
internal convenience init(unsafeTransaction: UnsafeDataTransaction, object: T) {
|
||||
|
||||
self.init(context: unsafeTransaction.context, object: object)
|
||||
}
|
||||
|
||||
internal func registerObserver<U: AnyObject>(observer: U, willChangeObject: (observer: U, monitor: ObjectMonitor<T>, object: T) -> Void, didDeleteObject: (observer: U, monitor: ObjectMonitor<T>, object: T) -> Void, didUpdateObject: (observer: U, monitor: ObjectMonitor<T>, object: T, changedPersistentKeys: Set<String>) -> Void) {
|
||||
|
||||
CoreStore.assert(
|
||||
NSThread.isMainThread(),
|
||||
"Attempted to add an observer of type \(typeName(observer)) outside the main thread."
|
||||
)
|
||||
|
||||
self.removeObserver(observer)
|
||||
|
||||
self.registerChangeNotification(
|
||||
&self.willChangeObjectKey,
|
||||
name: ObjectMonitorWillChangeObjectNotification,
|
||||
@@ -91,7 +141,7 @@ public final class ObjectMonitor<T: NSManagedObject> {
|
||||
|
||||
return
|
||||
}
|
||||
observer.objectMonitor(monitor, willUpdateObject: object)
|
||||
willChangeObject(observer: observer, monitor: monitor, object: object)
|
||||
}
|
||||
)
|
||||
self.registerObjectNotification(
|
||||
@@ -104,7 +154,7 @@ public final class ObjectMonitor<T: NSManagedObject> {
|
||||
|
||||
return
|
||||
}
|
||||
observer.objectMonitor(monitor, didDeleteObject: object)
|
||||
didDeleteObject(observer: observer, monitor: monitor, object: object)
|
||||
}
|
||||
)
|
||||
self.registerObjectNotification(
|
||||
@@ -131,23 +181,17 @@ public final class ObjectMonitor<T: NSManagedObject> {
|
||||
}
|
||||
|
||||
self.lastCommittedAttributes = currentCommitedAttributes
|
||||
observer.objectMonitor(
|
||||
monitor,
|
||||
didUpdateObject: object,
|
||||
didUpdateObject(
|
||||
observer: observer,
|
||||
monitor: monitor,
|
||||
object: object,
|
||||
changedPersistentKeys: changedKeys
|
||||
)
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
Unregisters an `ObjectObserver` from receiving notifications for changes to the receiver's `object`.
|
||||
|
||||
For thread safety, this method needs to be called from the main thread. An assertion failure will occur (on debug builds only) if called from any thread other than the main thread.
|
||||
|
||||
- parameter observer: an `ObjectObserver` to unregister notifications to
|
||||
*/
|
||||
public func removeObserver<U: ObjectObserver where U.ObjectEntityType == T>(observer: U) {
|
||||
internal func unregisterObserver(observer: AnyObject) {
|
||||
|
||||
CoreStore.assert(
|
||||
NSThread.isMainThread(),
|
||||
@@ -160,45 +204,9 @@ public final class ObjectMonitor<T: NSManagedObject> {
|
||||
setAssociatedRetainedObject(nilValue, forKey: &self.didUpdateObjectKey, inObject: observer)
|
||||
}
|
||||
|
||||
|
||||
// MARK: Internal
|
||||
|
||||
internal convenience init(dataStack: DataStack, object: T) {
|
||||
internal func upcast() -> ObjectMonitor<NSManagedObject> {
|
||||
|
||||
self.init(context: dataStack.mainContext, object: object)
|
||||
}
|
||||
|
||||
internal convenience init(unsafeTransaction: UnsafeDataTransaction, object: T) {
|
||||
|
||||
self.init(context: unsafeTransaction.context, object: object)
|
||||
}
|
||||
|
||||
private init(context: NSManagedObjectContext, object: T) {
|
||||
|
||||
let fetchRequest = NSFetchRequest()
|
||||
fetchRequest.entity = object.entity
|
||||
fetchRequest.fetchLimit = 0
|
||||
fetchRequest.resultType = .ManagedObjectResultType
|
||||
fetchRequest.sortDescriptors = []
|
||||
fetchRequest.includesPendingChanges = false
|
||||
fetchRequest.shouldRefreshRefetchedObjects = true
|
||||
|
||||
let fetchedResultsController = CoreStoreFetchedResultsController(
|
||||
context: context,
|
||||
fetchRequest: fetchRequest,
|
||||
fetchClauses: [Where("SELF", isEqualTo: object.objectID)]
|
||||
)
|
||||
|
||||
let fetchedResultsControllerDelegate = FetchedResultsControllerDelegate()
|
||||
|
||||
self.fetchedResultsController = fetchedResultsController
|
||||
self.fetchedResultsControllerDelegate = fetchedResultsControllerDelegate
|
||||
|
||||
fetchedResultsControllerDelegate.handler = self
|
||||
fetchedResultsControllerDelegate.fetchedResultsController = fetchedResultsController
|
||||
try! fetchedResultsController.performFetchFromSpecifiedStores()
|
||||
|
||||
self.lastCommittedAttributes = (self.object?.committedValuesForKeys(nil) as? [String: NSObject]) ?? [:]
|
||||
return unsafeBitCast(self, ObjectMonitor<NSManagedObject>.self)
|
||||
}
|
||||
|
||||
deinit {
|
||||
@@ -217,6 +225,35 @@ public final class ObjectMonitor<T: NSManagedObject> {
|
||||
private var didDeleteObjectKey: Void?
|
||||
private var didUpdateObjectKey: Void?
|
||||
|
||||
private init(context: NSManagedObjectContext, object: T) {
|
||||
|
||||
let fetchRequest = NSFetchRequest()
|
||||
fetchRequest.entity = object.entity
|
||||
fetchRequest.fetchLimit = 0
|
||||
fetchRequest.resultType = .ManagedObjectResultType
|
||||
fetchRequest.sortDescriptors = []
|
||||
fetchRequest.includesPendingChanges = false
|
||||
fetchRequest.shouldRefreshRefetchedObjects = true
|
||||
|
||||
let objectID = object.objectID
|
||||
let fetchedResultsController = CoreStoreFetchedResultsController(
|
||||
context: context,
|
||||
fetchRequest: fetchRequest,
|
||||
applyFetchClauses: Where("SELF", isEqualTo: objectID).applyToFetchRequest
|
||||
)
|
||||
|
||||
let fetchedResultsControllerDelegate = FetchedResultsControllerDelegate()
|
||||
|
||||
self.fetchedResultsController = fetchedResultsController
|
||||
self.fetchedResultsControllerDelegate = fetchedResultsControllerDelegate
|
||||
|
||||
fetchedResultsControllerDelegate.handler = self
|
||||
fetchedResultsControllerDelegate.fetchedResultsController = fetchedResultsController
|
||||
try! fetchedResultsController.performFetchFromSpecifiedStores()
|
||||
|
||||
self.lastCommittedAttributes = (self.object?.committedValuesForKeys(nil) as? [String: NSObject]) ?? [:]
|
||||
}
|
||||
|
||||
private func registerChangeNotification(notificationKey: UnsafePointer<Void>, name: String, toObserver observer: AnyObject, callback: (monitor: ObjectMonitor<T>) -> Void) {
|
||||
|
||||
setAssociatedRetainedObject(
|
||||
|
||||
@@ -54,6 +54,7 @@ public struct SectionBy {
|
||||
|
||||
/**
|
||||
Initializes a `SectionBy` clause with the key path to use to group `ListMonitor` objects into sections, and a closure to transform the value for the key path to an appropriate section name
|
||||
- Important: Some utilities (such as `ListMonitor`s) may keep `SectionBy`s in memory and may thus introduce retain cycles if reference captures are not handled properly.
|
||||
|
||||
- parameter sectionKeyPath: the key path to use to group the objects into sections
|
||||
- parameter sectionIndexTransformer: a closure to transform the value for the key path to an appropriate section name
|
||||
|
||||
@@ -82,7 +82,10 @@ public extension UnsafeDataTransaction {
|
||||
unsafeTransaction: self,
|
||||
from: from,
|
||||
sectionBy: nil,
|
||||
fetchClauses: fetchClauses
|
||||
applyFetchClauses: { fetchRequest in
|
||||
|
||||
fetchClauses.forEach { $0.applyToFetchRequest(fetchRequest) }
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
@@ -116,7 +119,10 @@ public extension UnsafeDataTransaction {
|
||||
unsafeTransaction: self,
|
||||
from: from,
|
||||
sectionBy: nil,
|
||||
fetchClauses: fetchClauses,
|
||||
applyFetchClauses: { fetchRequest in
|
||||
|
||||
fetchClauses.forEach { $0.applyToFetchRequest(fetchRequest) }
|
||||
},
|
||||
createAsynchronously: createAsynchronously
|
||||
)
|
||||
}
|
||||
@@ -155,7 +161,10 @@ public extension UnsafeDataTransaction {
|
||||
unsafeTransaction: self,
|
||||
from: from,
|
||||
sectionBy: sectionBy,
|
||||
fetchClauses: fetchClauses
|
||||
applyFetchClauses: { fetchRequest in
|
||||
|
||||
fetchClauses.forEach { $0.applyToFetchRequest(fetchRequest) }
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
@@ -191,7 +200,10 @@ public extension UnsafeDataTransaction {
|
||||
unsafeTransaction: self,
|
||||
from: from,
|
||||
sectionBy: sectionBy,
|
||||
fetchClauses: fetchClauses,
|
||||
applyFetchClauses: { fetchRequest in
|
||||
|
||||
fetchClauses.forEach { $0.applyToFetchRequest(fetchRequest) }
|
||||
},
|
||||
createAsynchronously: createAsynchronously
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user