@@ -33,7 +33,7 @@ public class ResultsController<T: NSManagedObject & ReadOnlyConvertible> {
3333 return request
3434 } ( )
3535
36- ///
36+ /// Internal NSFetchedResultsController Instance.
3737 ///
3838 private lazy var controller : NSFetchedResultsController < T > = {
3939 return NSFetchedResultsController ( fetchRequest: fetchRequest,
@@ -42,23 +42,23 @@ public class ResultsController<T: NSManagedObject & ReadOnlyConvertible> {
4242 cacheName: nil )
4343 } ( )
4444
45- ///
45+ /// FetchedResultsController Delegate Wrapper.
4646 ///
4747 private let internalDelegate = FetchedResultsControllerDelegateWrapper ( )
4848
49- ///
49+ /// Closure to be executed before the results are changed.
5050 ///
5151 public var onWillChangeContent : ( ( ) -> Void ) ?
5252
53- ///
53+ /// Closure to be executed after the results are changed.
5454 ///
5555 public var onDidChangeContent : ( ( ) -> Void ) ?
5656
57- ///
57+ /// Closure to be executed whenever an Object is updated.
5858 ///
5959 public var onDidChangeObject : ( ( _ object: T . ReadOnlyType , _ indexPath: IndexPath ? , _ type: ChangeType , _ newIndexPath: IndexPath ? ) -> Void ) ?
6060
61- ///
61+ /// Closure to be executed whenever an entire Section is updated.
6262 ///
6363 public var onDidChangeSection : ( ( _ sectionInfo: SectionInfo , _ sectionIndex: Int , _ type: ChangeType ) -> Void ) ?
6464
@@ -76,8 +76,8 @@ public class ResultsController<T: NSManagedObject & ReadOnlyConvertible> {
7676 self . predicate = predicate
7777 self . sortDescriptors = descriptors
7878
79- setupEventsForwarding ( )
8079 setupResultsController ( )
80+ setupEventsForwarding ( )
8181 }
8282
8383
@@ -97,24 +97,23 @@ public class ResultsController<T: NSManagedObject & ReadOnlyConvertible> {
9797
9898 /// Returns an array of SectionInfo Entitites.
9999 ///
100- private( set) public lazy var sections : [ SectionInfo ] = {
101- return controller. sections? . compactMap { mutableSection in
102- self . readonlySection ( from: mutableSection)
103- } ?? [ ]
104- } ( )
100+ public var sections : [ SectionInfo ] {
101+ let readOnlySections = controller. sections? . compactMap { mutableSection in
102+ SectionInfo ( mutableSection: mutableSection)
103+ }
105104
105+ return readOnlySections ?? [ ]
106+ }
106107
107- ///
108- ///
109- private func readonlySection( from mutableSection: NSFetchedResultsSectionInfo ) -> SectionInfo {
110- let mutableObjects = mutableSection. objects as? [ T ] ?? [ ]
111- let readonlyObjects = mutableObjects. map { $0. toReadOnly ( ) }
112108
113- return SectionInfo ( name: mutableSection. name, objects: readonlyObjects)
109+ /// Initializes the FetchedResultsController
110+ ///
111+ private func setupResultsController( ) {
112+ controller. delegate = internalDelegate
114113 }
115114
116115
117- ///
116+ /// Initializes FRC's Event Forwarding.
118117 ///
119118 private func setupEventsForwarding( ) {
120119 internalDelegate. onWillChangeContent = { [ weak self] in
@@ -139,18 +138,10 @@ public class ResultsController<T: NSManagedObject & ReadOnlyConvertible> {
139138 return
140139 }
141140
142- let readOnlySection = self . readonlySection ( from: mutableSection)
143- self . sections [ sectionIndex] = readOnlySection
141+ let readOnlySection = SectionInfo ( mutableSection: mutableSection)
144142 self . onDidChangeSection ? ( readOnlySection, sectionIndex, type)
145143 }
146144 }
147-
148-
149- /// Initializes the FetchedResultsController
150- ///
151- private func setupResultsController( ) {
152- controller. delegate = internalDelegate
153- }
154145}
155146
156147
@@ -165,7 +156,7 @@ public extension ResultsController {
165156
166157 // MARK: - ResultsController.SectionInfo
167158 //
168- public struct SectionInfo {
159+ public class SectionInfo {
169160
170161 /// Name of the section
171162 ///
@@ -174,11 +165,26 @@ public extension ResultsController {
174165 /// Number of objects in the current section
175166 ///
176167 public var numberOfObjects : Int {
177- return objects . count
168+ return mutableObjects . count
178169 }
179170
180171 /// Returns the array of (ReadOnly) objects in the section.
181172 ///
182- private( set) public var objects : [ T . ReadOnlyType ]
173+ private( set) public lazy var objects : [ T . ReadOnlyType ] = {
174+ return mutableObjects. map { $0. toReadOnly ( ) }
175+ } ( )
176+
177+
178+ /// Array of Mutable Objects!
179+ ///
180+ private let mutableObjects : [ T ]
181+
182+
183+ /// Designated Initializer
184+ ///
185+ init ( mutableSection: NSFetchedResultsSectionInfo ) {
186+ name = mutableSection. name
187+ mutableObjects = mutableSection. objects as? [ T ] ?? [ ]
188+ }
183189 }
184190}
0 commit comments