mirror of
https://github.com/JohnEstropia/CoreStore.git
synced 2026-04-21 00:11:25 +02:00
resurrect utilities for creating NSFetchedResultsController (fixes #119)
This commit is contained in:
@@ -362,6 +362,9 @@
|
|||||||
B580857B1CDF808D004C2EEB /* SetupTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B58085741CDF7F00004C2EEB /* SetupTests.swift */; };
|
B580857B1CDF808D004C2EEB /* SetupTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B58085741CDF7F00004C2EEB /* SetupTests.swift */; };
|
||||||
B580857C1CDF808F004C2EEB /* SetupTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B58085741CDF7F00004C2EEB /* SetupTests.swift */; };
|
B580857C1CDF808F004C2EEB /* SetupTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B58085741CDF7F00004C2EEB /* SetupTests.swift */; };
|
||||||
B58B22F51C93C1BA00521925 /* CoreStore.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 2F03A53019C5C6DA005002A5 /* CoreStore.framework */; };
|
B58B22F51C93C1BA00521925 /* CoreStore.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 2F03A53019C5C6DA005002A5 /* CoreStore.framework */; };
|
||||||
|
B596BBB21DD5A014001DCDD9 /* ConvenienceTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B596BBAD1DD59FDB001DCDD9 /* ConvenienceTests.swift */; };
|
||||||
|
B596BBB31DD5A014001DCDD9 /* ConvenienceTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B596BBAD1DD59FDB001DCDD9 /* ConvenienceTests.swift */; };
|
||||||
|
B596BBB41DD5A016001DCDD9 /* ConvenienceTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B596BBAD1DD59FDB001DCDD9 /* ConvenienceTests.swift */; };
|
||||||
B59851491C90289D00C99590 /* NSPersistentStoreCoordinator+Setup.swift in Sources */ = {isa = PBXBuildFile; fileRef = B59AFF401C6593E400C0ABE2 /* NSPersistentStoreCoordinator+Setup.swift */; };
|
B59851491C90289D00C99590 /* NSPersistentStoreCoordinator+Setup.swift in Sources */ = {isa = PBXBuildFile; fileRef = B59AFF401C6593E400C0ABE2 /* NSPersistentStoreCoordinator+Setup.swift */; };
|
||||||
B598514A1C90289E00C99590 /* NSPersistentStoreCoordinator+Setup.swift in Sources */ = {isa = PBXBuildFile; fileRef = B59AFF401C6593E400C0ABE2 /* NSPersistentStoreCoordinator+Setup.swift */; };
|
B598514A1C90289E00C99590 /* NSPersistentStoreCoordinator+Setup.swift in Sources */ = {isa = PBXBuildFile; fileRef = B59AFF401C6593E400C0ABE2 /* NSPersistentStoreCoordinator+Setup.swift */; };
|
||||||
B598514B1C90289F00C99590 /* NSPersistentStoreCoordinator+Setup.swift in Sources */ = {isa = PBXBuildFile; fileRef = B59AFF401C6593E400C0ABE2 /* NSPersistentStoreCoordinator+Setup.swift */; };
|
B598514B1C90289F00C99590 /* NSPersistentStoreCoordinator+Setup.swift in Sources */ = {isa = PBXBuildFile; fileRef = B59AFF401C6593E400C0ABE2 /* NSPersistentStoreCoordinator+Setup.swift */; };
|
||||||
@@ -648,6 +651,7 @@
|
|||||||
B57D27BD1D0BBE8200539C58 /* BaseTestDataTestCase.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BaseTestDataTestCase.swift; sourceTree = "<group>"; };
|
B57D27BD1D0BBE8200539C58 /* BaseTestDataTestCase.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BaseTestDataTestCase.swift; sourceTree = "<group>"; };
|
||||||
B57D27C11D0BC20100539C58 /* QueryTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = QueryTests.swift; sourceTree = "<group>"; };
|
B57D27C11D0BC20100539C58 /* QueryTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = QueryTests.swift; sourceTree = "<group>"; };
|
||||||
B58085741CDF7F00004C2EEB /* SetupTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SetupTests.swift; sourceTree = "<group>"; };
|
B58085741CDF7F00004C2EEB /* SetupTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SetupTests.swift; sourceTree = "<group>"; };
|
||||||
|
B596BBAD1DD59FDB001DCDD9 /* ConvenienceTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ConvenienceTests.swift; sourceTree = "<group>"; };
|
||||||
B59AFF401C6593E400C0ABE2 /* NSPersistentStoreCoordinator+Setup.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "NSPersistentStoreCoordinator+Setup.swift"; sourceTree = "<group>"; };
|
B59AFF401C6593E400C0ABE2 /* NSPersistentStoreCoordinator+Setup.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "NSPersistentStoreCoordinator+Setup.swift"; sourceTree = "<group>"; };
|
||||||
B59FA0AD1CCBAC95007C9BCA /* ICloudStore.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ICloudStore.swift; sourceTree = "<group>"; };
|
B59FA0AD1CCBAC95007C9BCA /* ICloudStore.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ICloudStore.swift; sourceTree = "<group>"; };
|
||||||
B5A261201B64BFDB006EB6D3 /* MigrationType.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MigrationType.swift; sourceTree = "<group>"; };
|
B5A261201B64BFDB006EB6D3 /* MigrationType.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MigrationType.swift; sourceTree = "<group>"; };
|
||||||
@@ -872,6 +876,7 @@
|
|||||||
B52557831D02A07400E51965 /* SectionByTests.swift */,
|
B52557831D02A07400E51965 /* SectionByTests.swift */,
|
||||||
B525576F1D02561A00E51965 /* SelectTests.swift */,
|
B525576F1D02561A00E51965 /* SelectTests.swift */,
|
||||||
B58085741CDF7F00004C2EEB /* SetupTests.swift */,
|
B58085741CDF7F00004C2EEB /* SetupTests.swift */,
|
||||||
|
B596BBAD1DD59FDB001DCDD9 /* ConvenienceTests.swift */,
|
||||||
B5DC47C91C93D9C800FA3BF3 /* StorageInterfaceTests.swift */,
|
B5DC47C91C93D9C800FA3BF3 /* StorageInterfaceTests.swift */,
|
||||||
2F03A53D19C5C6DA005002A5 /* Supporting Files */,
|
2F03A53D19C5C6DA005002A5 /* Supporting Files */,
|
||||||
B5489F3C1CF5EEBC008B4978 /* TestEntities */,
|
B5489F3C1CF5EEBC008B4978 /* TestEntities */,
|
||||||
@@ -1640,6 +1645,7 @@
|
|||||||
buildActionMask = 2147483647;
|
buildActionMask = 2147483647;
|
||||||
files = (
|
files = (
|
||||||
B52557841D02A07400E51965 /* SectionByTests.swift in Sources */,
|
B52557841D02A07400E51965 /* SectionByTests.swift in Sources */,
|
||||||
|
B596BBB21DD5A014001DCDD9 /* ConvenienceTests.swift in Sources */,
|
||||||
B5220E101D0DA6AB009BC71E /* ListObserverTests.swift in Sources */,
|
B5220E101D0DA6AB009BC71E /* ListObserverTests.swift in Sources */,
|
||||||
B5519A401CA1B17B002BEF78 /* ErrorTests.swift in Sources */,
|
B5519A401CA1B17B002BEF78 /* ErrorTests.swift in Sources */,
|
||||||
B525577C1D0291FE00E51965 /* GroupByTests.swift in Sources */,
|
B525577C1D0291FE00E51965 /* GroupByTests.swift in Sources */,
|
||||||
@@ -1789,6 +1795,7 @@
|
|||||||
buildActionMask = 2147483647;
|
buildActionMask = 2147483647;
|
||||||
files = (
|
files = (
|
||||||
B52557851D02A07400E51965 /* SectionByTests.swift in Sources */,
|
B52557851D02A07400E51965 /* SectionByTests.swift in Sources */,
|
||||||
|
B596BBB31DD5A014001DCDD9 /* ConvenienceTests.swift in Sources */,
|
||||||
B5220E111D0DA6AB009BC71E /* ListObserverTests.swift in Sources */,
|
B5220E111D0DA6AB009BC71E /* ListObserverTests.swift in Sources */,
|
||||||
B5519A411CA1B17B002BEF78 /* ErrorTests.swift in Sources */,
|
B5519A411CA1B17B002BEF78 /* ErrorTests.swift in Sources */,
|
||||||
B525577D1D0291FE00E51965 /* GroupByTests.swift in Sources */,
|
B525577D1D0291FE00E51965 /* GroupByTests.swift in Sources */,
|
||||||
@@ -1938,6 +1945,7 @@
|
|||||||
buildActionMask = 2147483647;
|
buildActionMask = 2147483647;
|
||||||
files = (
|
files = (
|
||||||
B5519A421CA1B17B002BEF78 /* ErrorTests.swift in Sources */,
|
B5519A421CA1B17B002BEF78 /* ErrorTests.swift in Sources */,
|
||||||
|
B596BBB41DD5A016001DCDD9 /* ConvenienceTests.swift in Sources */,
|
||||||
B5220E271D1308D1009BC71E /* ObjectObserverTests.swift in Sources */,
|
B5220E271D1308D1009BC71E /* ObjectObserverTests.swift in Sources */,
|
||||||
B525577E1D0291FE00E51965 /* GroupByTests.swift in Sources */,
|
B525577E1D0291FE00E51965 /* GroupByTests.swift in Sources */,
|
||||||
B52557761D02791400E51965 /* WhereTests.swift in Sources */,
|
B52557761D02791400E51965 /* WhereTests.swift in Sources */,
|
||||||
|
|||||||
@@ -233,5 +233,37 @@
|
|||||||
}
|
}
|
||||||
[self waitForExpectationsWithTimeout:10 handler:nil];
|
[self waitForExpectationsWithTimeout:10 handler:nil];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if TARGET_OS_IOS || TARGET_OS_WATCHOS || TARGET_OS_TV
|
||||||
|
|
||||||
|
- (void)test_ThatDataStacks_CanCreateCustomFetchedResultsControllers {
|
||||||
|
|
||||||
|
[CSCoreStore
|
||||||
|
setDefaultStack:[[CSDataStack alloc]
|
||||||
|
initWithModelName:@"Model"
|
||||||
|
bundle:[NSBundle bundleForClass:[self class]]
|
||||||
|
versionChain:nil]];
|
||||||
|
[CSCoreStore
|
||||||
|
addInMemoryStorageAndWait:[CSInMemoryStore new]
|
||||||
|
error:nil];
|
||||||
|
NSFetchedResultsController *controller =
|
||||||
|
[[CSCoreStore defaultStack]
|
||||||
|
createFetchedResultsControllerFrom:CSFromClass([TestEntity1 class])
|
||||||
|
sectionBy:[CSSectionBy keyPath:CSKeyPath(TestEntity1, testString)]
|
||||||
|
fetchClauses:@[CSWhereFormat(@"%K > %d", CSKeyPath(TestEntity1, testEntityID), 100),
|
||||||
|
CSOrderByKeys(CSSortAscending(CSKeyPath(TestEntity1, testString)), nil),
|
||||||
|
CSTweakRequest(^(NSFetchRequest *fetchRequest) { fetchRequest.fetchLimit = 10; })]];
|
||||||
|
|
||||||
|
XCTAssertNotNil(controller);
|
||||||
|
XCTAssertEqualObjects(controller.fetchRequest.entity.managedObjectClassName, [[TestEntity1 class] description]);
|
||||||
|
XCTAssertEqualObjects(controller.sectionNameKeyPath, CSKeyPath(TestEntity1, testString));
|
||||||
|
XCTAssertEqualObjects(controller.fetchRequest.predicate,
|
||||||
|
CSWhereFormat(@"%K > %d", CSKeyPath(TestEntity1, testEntityID), 100).predicate);
|
||||||
|
XCTAssertEqualObjects(controller.fetchRequest.sortDescriptors,
|
||||||
|
CSOrderByKeys(CSSortAscending(CSKeyPath(TestEntity1, testString)), nil).sortDescriptors);
|
||||||
|
XCTAssertEqual(controller.fetchRequest.fetchLimit, 10);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|||||||
94
CoreStoreTests/ConvenienceTests.swift
Normal file
94
CoreStoreTests/ConvenienceTests.swift
Normal file
@@ -0,0 +1,94 @@
|
|||||||
|
//
|
||||||
|
// ConvenienceTests.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.
|
||||||
|
//
|
||||||
|
|
||||||
|
@testable
|
||||||
|
import CoreStore
|
||||||
|
|
||||||
|
|
||||||
|
#if os(iOS) || os(watchOS) || os(tvOS)
|
||||||
|
|
||||||
|
// MARK: - ConvenienceTests
|
||||||
|
|
||||||
|
class ConvenienceTests: BaseTestCase {
|
||||||
|
|
||||||
|
@objc
|
||||||
|
dynamic func test_ThatDataStacks_CanCreateFetchedResultsControllers() {
|
||||||
|
|
||||||
|
self.prepareStack { (stack) in
|
||||||
|
|
||||||
|
let controller = stack.createFetchedResultsController(
|
||||||
|
From<TestEntity1>(),
|
||||||
|
SectionBy(#keyPath(TestEntity1.testString)),
|
||||||
|
Where("%@ > %d", #keyPath(TestEntity1.testEntityID), 100),
|
||||||
|
OrderBy(.ascending(#keyPath(TestEntity1.testString))),
|
||||||
|
Tweak { $0.fetchLimit = 10 }
|
||||||
|
)
|
||||||
|
XCTAssertEqual(controller.managedObjectContext, stack.mainContext)
|
||||||
|
XCTAssertEqual(controller.fetchRequest.entity?.managedObjectClassName, NSStringFromClass(TestEntity1.self))
|
||||||
|
XCTAssertEqual(controller.sectionNameKeyPath, #keyPath(TestEntity1.testString))
|
||||||
|
XCTAssertEqual(
|
||||||
|
controller.fetchRequest.sortDescriptors!,
|
||||||
|
OrderBy(.ascending(#keyPath(TestEntity1.testString))).sortDescriptors
|
||||||
|
)
|
||||||
|
XCTAssertEqual(
|
||||||
|
controller.fetchRequest.predicate,
|
||||||
|
Where("%@ > %d", #keyPath(TestEntity1.testEntityID), 100).predicate
|
||||||
|
)
|
||||||
|
XCTAssertEqual(controller.fetchRequest.fetchLimit, 10)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@objc
|
||||||
|
dynamic func test_ThatUnsafeDataTransactions_CanCreateFetchedResultsControllers() {
|
||||||
|
|
||||||
|
self.prepareStack { (stack) in
|
||||||
|
|
||||||
|
_ = withExtendedLifetime(stack.beginUnsafe()) { (transaction: UnsafeDataTransaction) in
|
||||||
|
|
||||||
|
let controller = transaction.createFetchedResultsController(
|
||||||
|
From<TestEntity1>(),
|
||||||
|
SectionBy(#keyPath(TestEntity1.testString)),
|
||||||
|
Where("%@ > %d", #keyPath(TestEntity1.testEntityID), 100),
|
||||||
|
OrderBy(.ascending(#keyPath(TestEntity1.testString))),
|
||||||
|
Tweak { $0.fetchLimit = 10 }
|
||||||
|
)
|
||||||
|
XCTAssertEqual(controller.managedObjectContext, transaction.context)
|
||||||
|
XCTAssertEqual(controller.fetchRequest.entity?.managedObjectClassName, NSStringFromClass(TestEntity1.self))
|
||||||
|
XCTAssertEqual(controller.sectionNameKeyPath, #keyPath(TestEntity1.testString))
|
||||||
|
XCTAssertEqual(
|
||||||
|
controller.fetchRequest.sortDescriptors!,
|
||||||
|
OrderBy(.ascending(#keyPath(TestEntity1.testString))).sortDescriptors
|
||||||
|
)
|
||||||
|
XCTAssertEqual(
|
||||||
|
controller.fetchRequest.predicate,
|
||||||
|
Where("%@ > %d", #keyPath(TestEntity1.testEntityID), 100).predicate
|
||||||
|
)
|
||||||
|
XCTAssertEqual(controller.fetchRequest.fetchLimit, 10)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -26,199 +26,189 @@
|
|||||||
import Foundation
|
import Foundation
|
||||||
import CoreData
|
import CoreData
|
||||||
|
|
||||||
// TODO: Uncomment
|
#if os(iOS) || os(watchOS) || os(tvOS)
|
||||||
//#if os(iOS) || os(watchOS) || os(tvOS)
|
|
||||||
//
|
// MARK: - DataStack
|
||||||
//// MARK: - NSFetchedResultsController
|
|
||||||
//
|
public extension DataStack {
|
||||||
//public extension NSFetchedResultsController {
|
|
||||||
//
|
/**
|
||||||
// /**
|
Utility for creating an `NSFetchedResultsController` from the `DataStack`. This is useful when an `NSFetchedResultsController` is preferred over the overhead of `ListMonitor`s abstraction.
|
||||||
// Utility for creating an `NSFetchedResultsController` from a `DataStack`. This is useful when an `NSFetchedResultsController` is preferred over the overhead of `ListMonitor`s abstraction.
|
|
||||||
//
|
- parameter from: a `From` clause indicating the entity type
|
||||||
// - parameter dataStack: the `DataStack` to observe objects from
|
- parameter sectionBy: a `SectionBy` clause indicating the keyPath for the attribute to use when sorting the list into sections
|
||||||
// - parameter from: a `From` clause indicating the entity type
|
- parameter fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
|
||||||
// - parameter sectionBy: a `SectionBy` clause indicating the keyPath for the attribute to use when sorting the list into sections
|
- returns: an `NSFetchedResultsController` that observes the `DataStack`
|
||||||
// - parameter fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
|
*/
|
||||||
// - returns: an `NSFetchedResultsController` that observes a `DataStack`
|
@nonobjc
|
||||||
// */
|
public func createFetchedResultsController<T: NSManagedObject>(_ from: From<T>, _ sectionBy: SectionBy, _ fetchClauses: FetchClause...) -> NSFetchedResultsController<T> {
|
||||||
// @nonobjc
|
|
||||||
// public static func createFor<T: NSManagedObject>(_ dataStack: DataStack, _ from: From<T>, _ sectionBy: SectionBy, _ fetchClauses: FetchClause...) -> NSFetchedResultsController<NSFetchRequestResult> {
|
return createFRC(
|
||||||
//
|
fromContext: self.mainContext,
|
||||||
// return self.createFromContext(
|
from: from,
|
||||||
// dataStack.mainContext,
|
sectionBy: sectionBy,
|
||||||
// fetchRequest: CoreStoreFetchRequest(),
|
fetchClauses: fetchClauses
|
||||||
// from: from,
|
)
|
||||||
// sectionBy: sectionBy,
|
}
|
||||||
// fetchClauses: fetchClauses
|
|
||||||
// )
|
/**
|
||||||
// }
|
Utility for creating an `NSFetchedResultsController` from a `DataStack`. This is useful when an `NSFetchedResultsController` is preferred over the overhead of `ListMonitor`s abstraction.
|
||||||
//
|
|
||||||
// /**
|
- parameter from: a `From` clause indicating the entity type
|
||||||
// Utility for creating an `NSFetchedResultsController` from a `DataStack`. This is useful when an `NSFetchedResultsController` is preferred over the overhead of `ListMonitor`s abstraction.
|
- parameter sectionBy: a `SectionBy` 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 `Where`, `OrderBy`, and `Tweak` clauses.
|
||||||
// - parameter dataStack: the `DataStack` to observe objects from
|
- returns: an `NSFetchedResultsController` that observes the `DataStack`
|
||||||
// - parameter from: a `From` clause indicating the entity type
|
*/
|
||||||
// - parameter sectionBy: a `SectionBy` clause indicating the keyPath for the attribute to use when sorting the list into sections
|
@nonobjc
|
||||||
// - parameter fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
|
public func createFetchedResultsController<T: NSManagedObject>(_ from: From<T>, _ sectionBy: SectionBy, _ fetchClauses: [FetchClause]) -> NSFetchedResultsController<T> {
|
||||||
// - returns: an `NSFetchedResultsController` that observes a `DataStack`
|
|
||||||
// */
|
return createFRC(
|
||||||
// @nonobjc
|
fromContext: self.mainContext,
|
||||||
// public static func createFor<T: NSManagedObject>(_ dataStack: DataStack, _ from: From<T>, _ sectionBy: SectionBy, _ fetchClauses: [FetchClause]) -> NSFetchedResultsController {
|
from: from,
|
||||||
//
|
sectionBy: sectionBy,
|
||||||
// return self.createFromContext(
|
fetchClauses: fetchClauses
|
||||||
// dataStack.mainContext,
|
)
|
||||||
// fetchRequest: CoreStoreFetchRequest(),
|
}
|
||||||
// from: from,
|
|
||||||
// sectionBy: sectionBy,
|
/**
|
||||||
// fetchClauses: fetchClauses
|
Utility for creating an `NSFetchedResultsController` from the `DataStack`. This is useful when an `NSFetchedResultsController` is preferred over the overhead of `ListMonitor`s abstraction.
|
||||||
// )
|
|
||||||
// }
|
- parameter from: a `From` clause indicating the entity type
|
||||||
//
|
- parameter fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
|
||||||
// /**
|
- returns: an `NSFetchedResultsController` that observes the `DataStack`
|
||||||
// Utility for creating an `NSFetchedResultsController` from a `DataStack`. This is useful when an `NSFetchedResultsController` is preferred over the overhead of `ListMonitor`s abstraction.
|
*/
|
||||||
//
|
@nonobjc
|
||||||
// - parameter dataStack: the `DataStack` to observe objects from
|
public func createFetchedResultsController<T: NSManagedObject>(_ from: From<T>, _ fetchClauses: FetchClause...) -> NSFetchedResultsController<T> {
|
||||||
// - parameter from: a `From` clause indicating the entity type
|
|
||||||
// - parameter fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
|
return createFRC(
|
||||||
// - returns: an `NSFetchedResultsController` that observes a `DataStack`
|
fromContext: self.mainContext,
|
||||||
// */
|
from: from,
|
||||||
// @nonobjc
|
sectionBy: nil,
|
||||||
// public static func createFor<T: NSManagedObject>(_ dataStack: DataStack, _ from: From<T>, _ fetchClauses: FetchClause...) -> NSFetchedResultsController {
|
fetchClauses: fetchClauses
|
||||||
//
|
)
|
||||||
// return self.createFromContext(
|
}
|
||||||
// dataStack.mainContext,
|
|
||||||
// fetchRequest: CoreStoreFetchRequest(),
|
/**
|
||||||
// from: from,
|
Utility for creating an `NSFetchedResultsController` from the `DataStack`. This is useful when an `NSFetchedResultsController` is preferred over the overhead of `ListMonitor`s abstraction.
|
||||||
// sectionBy: nil,
|
|
||||||
// fetchClauses: fetchClauses
|
- parameter from: a `From` clause indicating the entity type
|
||||||
// )
|
- parameter fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
|
||||||
// }
|
- returns: an `NSFetchedResultsController` that observes the `DataStack`
|
||||||
//
|
*/
|
||||||
// /**
|
@nonobjc
|
||||||
// Utility for creating an `NSFetchedResultsController` from a `DataStack`. This is useful when an `NSFetchedResultsController` is preferred over the overhead of `ListMonitor`s abstraction.
|
public func createFetchedResultsController<T: NSManagedObject>(forDataStack dataStack: DataStack, _ from: From<T>, _ fetchClauses: [FetchClause]) -> NSFetchedResultsController<T> {
|
||||||
//
|
|
||||||
// - parameter dataStack: the `DataStack` to observe objects from
|
return createFRC(
|
||||||
// - parameter from: a `From` clause indicating the entity type
|
fromContext: self.mainContext,
|
||||||
// - parameter fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
|
from: from,
|
||||||
// - returns: an `NSFetchedResultsController` that observes a `DataStack`
|
sectionBy: nil,
|
||||||
// */
|
fetchClauses: fetchClauses
|
||||||
// @nonobjc
|
)
|
||||||
// public static func createFor<T: NSManagedObject>(_ dataStack: DataStack, _ from: From<T>, _ fetchClauses: [FetchClause]) -> NSFetchedResultsController {
|
}
|
||||||
//
|
}
|
||||||
// return self.createFromContext(
|
|
||||||
// dataStack.mainContext,
|
|
||||||
// fetchRequest: CoreStoreFetchRequest(),
|
// MARK: - UnsafeDataTransaction
|
||||||
// from: from,
|
|
||||||
// sectionBy: nil,
|
public extension UnsafeDataTransaction {
|
||||||
// fetchClauses: fetchClauses
|
|
||||||
// )
|
/**
|
||||||
// }
|
Utility for creating an `NSFetchedResultsController` from the `UnsafeDataTransaction`. This is useful when an `NSFetchedResultsController` is preferred over the overhead of `ListMonitor`s abstraction.
|
||||||
//
|
|
||||||
// /**
|
- parameter from: a `From` clause indicating the entity type
|
||||||
// Utility for creating an `NSFetchedResultsController` from an `UnsafeDataTransaction`. This is useful when an `NSFetchedResultsController` is preferred over the overhead of `ListMonitor`s abstraction.
|
- parameter sectionBy: a `SectionBy` 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 `Where`, `OrderBy`, and `Tweak` clauses.
|
||||||
// - parameter transaction: the `UnsafeDataTransaction` to observe objects from
|
- returns: an `NSFetchedResultsController` that observes the `UnsafeDataTransaction`
|
||||||
// - parameter from: a `From` clause indicating the entity type
|
*/
|
||||||
// - parameter sectionBy: a `SectionBy` clause indicating the keyPath for the attribute to use when sorting the list into sections
|
@nonobjc
|
||||||
// - parameter fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
|
public func createFetchedResultsController<T: NSManagedObject>(_ from: From<T>, _ sectionBy: SectionBy, _ fetchClauses: FetchClause...) -> NSFetchedResultsController<T> {
|
||||||
// - returns: an `NSFetchedResultsController` that observes an `UnsafeDataTransaction`
|
|
||||||
// */
|
return createFRC(
|
||||||
// @nonobjc
|
fromContext: self.context,
|
||||||
// public static func createFor<T: NSManagedObject>(_ transaction: UnsafeDataTransaction, _ from: From<T>, _ sectionBy: SectionBy, _ fetchClauses: FetchClause...) -> NSFetchedResultsController {
|
from: from,
|
||||||
//
|
sectionBy: sectionBy,
|
||||||
// return self.createFromContext(
|
fetchClauses: fetchClauses
|
||||||
// transaction.context,
|
)
|
||||||
// fetchRequest: CoreStoreFetchRequest(),
|
}
|
||||||
// from: from,
|
|
||||||
// sectionBy: sectionBy,
|
/**
|
||||||
// fetchClauses: fetchClauses
|
Utility for creating an `NSFetchedResultsController` from the `UnsafeDataTransaction`. This is useful when an `NSFetchedResultsController` is preferred over the overhead of `ListMonitor`s abstraction.
|
||||||
// )
|
|
||||||
// }
|
- parameter from: a `From` clause indicating the entity type
|
||||||
//
|
- parameter sectionBy: a `SectionBy` 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 `Where`, `OrderBy`, and `Tweak` clauses.
|
||||||
// Utility for creating an `NSFetchedResultsController` from an `UnsafeDataTransaction`. This is useful when an `NSFetchedResultsController` is preferred over the overhead of `ListMonitor`s abstraction.
|
- returns: an `NSFetchedResultsController` that observes the `UnsafeDataTransaction`
|
||||||
//
|
*/
|
||||||
// - parameter transaction: the `UnsafeDataTransaction` to observe objects from
|
@nonobjc
|
||||||
// - parameter from: a `From` clause indicating the entity type
|
public func createFetchedResultsController<T: NSManagedObject>(_ from: From<T>, _ sectionBy: SectionBy, _ fetchClauses: [FetchClause]) -> NSFetchedResultsController<T> {
|
||||||
// - parameter sectionBy: a `SectionBy` 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 `Where`, `OrderBy`, and `Tweak` clauses.
|
return createFRC(
|
||||||
// - returns: an `NSFetchedResultsController` that observes an `UnsafeDataTransaction`
|
fromContext: self.context,
|
||||||
// */
|
from: from,
|
||||||
// @nonobjc
|
sectionBy: sectionBy,
|
||||||
// public static func createFor<T: NSManagedObject>(_ transaction: UnsafeDataTransaction, _ from: From<T>, _ sectionBy: SectionBy, _ fetchClauses: [FetchClause]) -> NSFetchedResultsController {
|
fetchClauses: fetchClauses
|
||||||
//
|
)
|
||||||
// return self.createFromContext(
|
}
|
||||||
// transaction.context,
|
|
||||||
// fetchRequest: CoreStoreFetchRequest(),
|
/**
|
||||||
// from: from,
|
Utility for creating an `NSFetchedResultsController` from the `UnsafeDataTransaction`. This is useful when an `NSFetchedResultsController` is preferred over the overhead of `ListMonitor`s abstraction.
|
||||||
// sectionBy: sectionBy,
|
|
||||||
// fetchClauses: fetchClauses
|
- parameter from: a `From` clause indicating the entity type
|
||||||
// )
|
- parameter fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
|
||||||
// }
|
- returns: an `NSFetchedResultsController` that observes the `UnsafeDataTransaction`
|
||||||
//
|
*/
|
||||||
// /**
|
@nonobjc
|
||||||
// Utility for creating an `NSFetchedResultsController` from an `UnsafeDataTransaction`. This is useful when an `NSFetchedResultsController` is preferred over the overhead of `ListMonitor`s abstraction.
|
public func createFetchedResultsController<T: NSManagedObject>(_ from: From<T>, _ fetchClauses: FetchClause...) -> NSFetchedResultsController<T> {
|
||||||
//
|
|
||||||
// - parameter transaction: the `UnsafeDataTransaction` to observe objects from
|
return createFRC(
|
||||||
// - parameter from: a `From` clause indicating the entity type
|
fromContext: self.context,
|
||||||
// - parameter fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
|
from: from,
|
||||||
// - returns: an `NSFetchedResultsController` that observes an `UnsafeDataTransaction`
|
sectionBy: nil,
|
||||||
// */
|
fetchClauses: fetchClauses
|
||||||
// @nonobjc
|
)
|
||||||
// public static func createFor<T: NSManagedObject>(_ transaction: UnsafeDataTransaction, _ from: From<T>, _ fetchClauses: FetchClause...) -> NSFetchedResultsController {
|
}
|
||||||
//
|
|
||||||
// return self.createFromContext(
|
/**
|
||||||
// transaction.context,
|
Utility for creating an `NSFetchedResultsController` from the `UnsafeDataTransaction`. This is useful when an `NSFetchedResultsController` is preferred over the overhead of `ListMonitor`s abstraction.
|
||||||
// fetchRequest: CoreStoreFetchRequest(),
|
|
||||||
// from: from,
|
- parameter from: a `From` clause indicating the entity type
|
||||||
// sectionBy: nil,
|
- parameter fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
|
||||||
// fetchClauses: fetchClauses
|
- returns: an `NSFetchedResultsController` that observes the `UnsafeDataTransaction`
|
||||||
// )
|
*/
|
||||||
// }
|
@nonobjc
|
||||||
//
|
public func createFetchedResultsController<T: NSManagedObject>(_ from: From<T>, _ fetchClauses: [FetchClause]) -> NSFetchedResultsController<T> {
|
||||||
// /**
|
|
||||||
// Utility for creating an `NSFetchedResultsController` from an `UnsafeDataTransaction`. This is useful when an `NSFetchedResultsController` is preferred over the overhead of `ListMonitor`s abstraction.
|
return createFRC(
|
||||||
//
|
fromContext: self.context,
|
||||||
// - parameter transaction: the `UnsafeDataTransaction` to observe objects from
|
from: from,
|
||||||
// - parameter from: a `From` clause indicating the entity type
|
sectionBy: nil,
|
||||||
// - parameter fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
|
fetchClauses: fetchClauses
|
||||||
// */
|
)
|
||||||
// @nonobjc
|
}
|
||||||
// public static func createFor<T: NSManagedObject>(_ transaction: UnsafeDataTransaction, _ from: From<T>, _ fetchClauses: [FetchClause]) -> NSFetchedResultsController {
|
}
|
||||||
//
|
|
||||||
// return self.createFromContext(
|
|
||||||
// transaction.context,
|
|
||||||
// fetchRequest: CoreStoreFetchRequest(),
|
// MARK: - Private
|
||||||
// from: from,
|
|
||||||
// sectionBy: nil,
|
fileprivate func createFRC<T: NSManagedObject>(fromContext context: NSManagedObjectContext, from: From<T>? = nil, sectionBy: SectionBy? = nil, fetchClauses: [FetchClause]) -> NSFetchedResultsController<T> {
|
||||||
// fetchClauses: fetchClauses
|
|
||||||
// )
|
let controller = CoreStoreFetchedResultsController(
|
||||||
// }
|
context: context,
|
||||||
//
|
fetchRequest: CoreStoreFetchRequest().dynamicCast(),
|
||||||
//
|
from: from,
|
||||||
// // MARK: Internal
|
sectionBy: sectionBy,
|
||||||
//
|
applyFetchClauses: { (fetchRequest) in
|
||||||
// @nonobjc
|
|
||||||
// internal static func createFromContext<T: NSManagedObject>(_ context: NSManagedObjectContext, fetchRequest: CoreStoreFetchRequest<T>, from: From<T>? = nil, sectionBy: SectionBy? = nil, fetchClauses: [FetchClause]) -> NSFetchedResultsController<NSFetchRequestResult> {
|
fetchClauses.forEach { $0.applyToFetchRequest(fetchRequest as! NSFetchRequest<NSFetchRequestResult>) }
|
||||||
//
|
|
||||||
// let controller = CoreStoreFetchedResultsController(
|
CoreStore.assert(
|
||||||
// context: context,
|
fetchRequest.sortDescriptors?.isEmpty == false,
|
||||||
// fetchRequest: fetchRequest,
|
"An \(cs_typeName(NSFetchedResultsController<NSManagedObject>.self)) requires a sort information. Specify from a \(cs_typeName(OrderBy.self)) clause or any custom \(cs_typeName(FetchClause.self)) that provides a sort descriptor."
|
||||||
// from: from,
|
)
|
||||||
// sectionBy: sectionBy,
|
}
|
||||||
// applyFetchClauses: { fetchRequest in
|
)
|
||||||
//
|
return controller.dynamicCast()
|
||||||
// fetchClauses.forEach { $0.applyToFetchRequest(fetchRequest) }
|
}
|
||||||
//
|
|
||||||
// CoreStore.assert(
|
#endif
|
||||||
// fetchRequest.sortDescriptors?.isEmpty == false,
|
|
||||||
// "An \(cs_typeName(NSFetchedResultsController<T>.self)) requires a sort information. Specify from a \(cs_typeName(OrderBy.self)) clause or any custom \(cs_typeName(FetchClause.self)) that provides a sort descriptor."
|
|
||||||
// )
|
|
||||||
// }
|
|
||||||
// )
|
|
||||||
// return controller.upcast()
|
|
||||||
// }
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//#endif
|
|
||||||
|
|||||||
@@ -184,9 +184,8 @@ public extension BaseDataTransaction {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
Updates existing `ImportableUniqueObject`s or creates them by importing from the specified array of import sources.
|
Updates existing `ImportableUniqueObject`s or creates them by importing from the specified array of import sources.
|
||||||
Objects are called with `ImportableUniqueObject` methods in the same order as in import sources array.
|
`ImportableUniqueObject` methods are called on the objects in the same order as they are in the `sourceArray`, and are returned in an array with that same order.
|
||||||
The array returned from `importUniqueObjects(...)` correctly maps to the order of `sourceArray`.
|
- Warning: If `sourceArray` contains multiple import sources with same ID, no merging will occur and ONLY THE LAST duplicate will be imported.
|
||||||
If `sourceArray` contains multiple import sources with same ID, the last one will be imported.
|
|
||||||
|
|
||||||
- parameter into: an `Into` clause specifying the entity type
|
- parameter into: an `Into` clause specifying the entity type
|
||||||
- parameter sourceArray: the array of objects to import values from
|
- parameter sourceArray: the array of objects to import values from
|
||||||
@@ -217,8 +216,7 @@ public extension BaseDataTransaction {
|
|||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
// each subsequent import source with the same ID will replace the existing one
|
importSourceByID[uniqueIDValue] = source // effectively replaces duplicate with the latest
|
||||||
importSourceByID[uniqueIDValue] = source
|
|
||||||
return uniqueIDValue
|
return uniqueIDValue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -227,17 +225,19 @@ public extension BaseDataTransaction {
|
|||||||
|
|
||||||
var existingObjectsByID = Dictionary<T.UniqueIDType, T>()
|
var existingObjectsByID = Dictionary<T.UniqueIDType, T>()
|
||||||
self.fetchAll(From(entityType), Where(entityType.uniqueIDKeyPath, isMemberOf: sortedIDs))?
|
self.fetchAll(From(entityType), Where(entityType.uniqueIDKeyPath, isMemberOf: sortedIDs))?
|
||||||
.forEach { existingObjectsByID[$0.uniqueIDValue] = $0 }
|
.forEach { existingObjectsByID[$0.uniqueIDValue] = $0 }
|
||||||
|
|
||||||
var processedObjectIDs = Set<T.UniqueIDType>()
|
var processedObjectIDs = Set<T.UniqueIDType>()
|
||||||
var result = [T]()
|
var result = [T]()
|
||||||
|
|
||||||
for objectID in sortedIDs {
|
for objectID in sortedIDs where !processedObjectIDs.contains(objectID) {
|
||||||
|
|
||||||
|
guard let source = importSourceByID[objectID] else {
|
||||||
|
|
||||||
|
continue
|
||||||
|
}
|
||||||
try autoreleasepool {
|
try autoreleasepool {
|
||||||
|
|
||||||
guard let source = importSourceByID[objectID], !processedObjectIDs.contains(objectID) else { return }
|
|
||||||
|
|
||||||
if let object = existingObjectsByID[objectID] {
|
if let object = existingObjectsByID[objectID] {
|
||||||
guard entityType.shouldUpdate(from: source, in: self) else { return }
|
guard entityType.shouldUpdate(from: source, in: self) else { return }
|
||||||
|
|
||||||
@@ -256,7 +256,6 @@ public extension BaseDataTransaction {
|
|||||||
processedObjectIDs.insert(objectID)
|
processedObjectIDs.insert(objectID)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,7 +15,7 @@
|
|||||||
<key>CFBundlePackageType</key>
|
<key>CFBundlePackageType</key>
|
||||||
<string>FMWK</string>
|
<string>FMWK</string>
|
||||||
<key>CFBundleShortVersionString</key>
|
<key>CFBundleShortVersionString</key>
|
||||||
<string>2.1.1</string>
|
<string>2.2.0</string>
|
||||||
<key>CFBundleSignature</key>
|
<key>CFBundleSignature</key>
|
||||||
<string>????</string>
|
<string>????</string>
|
||||||
<key>CFBundleVersion</key>
|
<key>CFBundleVersion</key>
|
||||||
|
|||||||
@@ -27,40 +27,78 @@ import Foundation
|
|||||||
import CoreData
|
import CoreData
|
||||||
|
|
||||||
|
|
||||||
//#if os(iOS) || os(watchOS) || os(tvOS)
|
#if os(iOS) || os(watchOS) || os(tvOS)
|
||||||
//
|
|
||||||
//// MARK: - NSFetchedResultsController
|
// MARK: - CSDataStack
|
||||||
//
|
|
||||||
//public extension NSFetchedResultsController {
|
public extension CSDataStack {
|
||||||
//
|
|
||||||
// /**
|
/**
|
||||||
// Utility for creating an `NSFetchedResultsController` from a `CSDataStack`. This is useful when an `NSFetchedResultsController` is preferred over the overhead of `CSListMonitor`s abstractio
|
Utility for creating an `NSFetchedResultsController` from the `CSDataStack`. This is useful when an `NSFetchedResultsController` is preferred over the overhead of `CSListMonitor`s abstraction.
|
||||||
//
|
|
||||||
// - parameter dataStack: the `CSDataStack` to observe objects from
|
- parameter from: a `CSFrom` clause indicating the entity type
|
||||||
// - parameter fetchRequest: the `NSFetchRequest` instance to use with the `NSFetchedResultsController`
|
- parameter sectionBy: a `CSSectionBy` clause indicating the keyPath for the attribute to use when sorting the list into sections.
|
||||||
// - parameter from: an optional `CSFrom` clause indicating the entity type. If not specified, the `fetchRequest` argument's `entity` property should already be set.
|
- parameter fetchClauses: a series of `CSFetchClause` instances for fetching the object list. Accepts `CSWhere`, `CSOrderBy`, and `CSTweak` clauses.
|
||||||
// - parameter sectionBy: a `CSSectionBy` clause indicating the keyPath for the attribute to use when sorting the list into sections.
|
- returns: an `NSFetchedResultsController` that observes the `CSDataStack`
|
||||||
// - parameter fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `CSWhere`, `CSOrderBy`, and `CSTweak` clauses.
|
*/
|
||||||
// */
|
@objc
|
||||||
// @objc
|
public func createFetchedResultsControllerFrom(_ from: CSFrom, sectionBy: CSSectionBy, fetchClauses: [CSFetchClause]) -> NSFetchedResultsController<NSManagedObject> {
|
||||||
// public static func cs_createForStack(_ dataStack: CSDataStack, fetchRequest: NSFetchRequest<NSFetchRequestResult>, from: CSFrom?, sectionBy: CSSectionBy?, fetchClauses: [CSFetchClause]) -> NSFetchedResultsController {
|
|
||||||
//
|
return createFRC(
|
||||||
// return CoreStoreFetchedResultsController(
|
fromContext: self.bridgeToSwift.mainContext,
|
||||||
// context: dataStack.bridgeToSwift.mainContext,
|
from: from,
|
||||||
// fetchRequest: fetchRequest,
|
sectionBy: sectionBy,
|
||||||
// from: from?.bridgeToSwift,
|
fetchClauses: fetchClauses
|
||||||
// sectionBy: sectionBy?.bridgeToSwift,
|
)
|
||||||
// applyFetchClauses: { fetchRequest in
|
}
|
||||||
//
|
}
|
||||||
// fetchClauses.forEach { $0.applyToFetchRequest(fetchRequest) }
|
|
||||||
//
|
|
||||||
// CoreStore.assert(
|
// MARK: - CSUnsafeDataTransaction
|
||||||
// fetchRequest.sortDescriptors?.isEmpty == false,
|
|
||||||
// "An \(cs_typeName(NSFetchedResultsController)) requires a sort information. Specify from a \(cs_typeName(CSOrderBy)) clause or any custom \(cs_typeName(CSFetchClause)) that provides a sort descriptor."
|
public extension CSUnsafeDataTransaction {
|
||||||
// )
|
|
||||||
// }
|
/**
|
||||||
// )
|
Utility for creating an `NSFetchedResultsController` from the `CSUnsafeDataTransaction`. This is useful when an `NSFetchedResultsController` is preferred over the overhead of `CSListMonitor`s abstraction.
|
||||||
// }
|
|
||||||
//}
|
- 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
|
||||||
//#endif
|
- parameter fetchClauses: a series of `FetchClause` instances for fetching the object list. Accepts `Where`, `OrderBy`, and `Tweak` clauses.
|
||||||
|
- returns: an `NSFetchedResultsController` that observes an `CSUnsafeDataTransaction`
|
||||||
|
*/
|
||||||
|
@objc
|
||||||
|
public func createFetchedResultsControllerFrom(_ from: CSFrom, sectionBy: CSSectionBy, fetchClauses: [CSFetchClause]) -> NSFetchedResultsController<NSManagedObject> {
|
||||||
|
|
||||||
|
return createFRC(
|
||||||
|
fromContext: self.bridgeToSwift.context,
|
||||||
|
from: from,
|
||||||
|
sectionBy: sectionBy,
|
||||||
|
fetchClauses: fetchClauses
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// MARK: - Private
|
||||||
|
|
||||||
|
fileprivate func createFRC(fromContext context: NSManagedObjectContext, from: CSFrom? = nil, sectionBy: CSSectionBy?, fetchClauses: [CSFetchClause]) -> NSFetchedResultsController<NSManagedObject> {
|
||||||
|
|
||||||
|
let controller = CoreStoreFetchedResultsController(
|
||||||
|
context: context,
|
||||||
|
fetchRequest: CoreStoreFetchRequest().dynamicCast(),
|
||||||
|
from: from?.bridgeToSwift.upcast(),
|
||||||
|
sectionBy: sectionBy?.bridgeToSwift,
|
||||||
|
applyFetchClauses: { (fetchRequest) in
|
||||||
|
|
||||||
|
fetchClauses.forEach { $0.applyToFetchRequest(fetchRequest as! NSFetchRequest<NSFetchRequestResult>) }
|
||||||
|
|
||||||
|
CoreStore.assert(
|
||||||
|
fetchRequest.sortDescriptors?.isEmpty == false,
|
||||||
|
"An \(cs_typeName(NSFetchedResultsController<NSManagedObject>.self)) requires a sort information. Specify from a \(cs_typeName(CSOrderBy.self)) clause or any custom \(cs_typeName(CSFetchClause.self)) that provides a sort descriptor."
|
||||||
|
)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
return controller.dynamicCast()
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|||||||
Reference in New Issue
Block a user