mirror of
https://github.com/JohnEstropia/CoreStore.git
synced 2026-03-21 00:49:49 +01:00
DiffableDataSource.CollectionView implementation
This commit is contained in:
@@ -183,8 +183,8 @@ public /*abstract*/ class BaseDataTransaction {
|
||||
)
|
||||
let context = self.context
|
||||
objects.forEach {
|
||||
|
||||
$0.cs_rawObject(in: context).map({ context.delete($0) })
|
||||
|
||||
$0.asEditable(in: self).map({ context.delete($0.cs_toRaw()) })
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -39,7 +39,7 @@ extension CoreStore {
|
||||
- parameter object: the `ObjectRepresentation` to observe changes from
|
||||
- returns: a `ObjectMonitor` that monitors changes to `object`
|
||||
*/
|
||||
public static func monitorObject<O: ObjectRepresentation>(_ object: O) -> ObjectMonitor<O.ObjectType> {
|
||||
public static func monitorObject<O: DynamicObject>(_ object: O) -> ObjectMonitor<O> {
|
||||
|
||||
return Shared.defaultStack.monitorObject(object)
|
||||
}
|
||||
|
||||
@@ -38,13 +38,13 @@ extension DataStack {
|
||||
- parameter object: the `ObjectRepresentation` to observe changes from
|
||||
- returns: a `ObjectMonitor` that monitors changes to `object`
|
||||
*/
|
||||
public func monitorObject<O: ObjectRepresentation>(_ object: O) -> ObjectMonitor<O.ObjectType> {
|
||||
public func monitorObject<O: DynamicObject>(_ object: O) -> ObjectMonitor<O> {
|
||||
|
||||
Internals.assert(
|
||||
Thread.isMainThread,
|
||||
"Attempted to observe objects from \(Internals.typeName(self)) outside the main thread."
|
||||
)
|
||||
return ObjectMonitor(dataStack: self, object: object)
|
||||
return .init(objectID: object.cs_id(), context: self.unsafeContext())
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
291
Sources/DiffableDataSource.CollectionView.swift
Normal file
291
Sources/DiffableDataSource.CollectionView.swift
Normal file
@@ -0,0 +1,291 @@
|
||||
//
|
||||
// DiffableDataSource.CollectionView.swift
|
||||
// CoreStore
|
||||
//
|
||||
// Copyright © 2018 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.
|
||||
//
|
||||
|
||||
#if canImport(UIKit)
|
||||
|
||||
import UIKit
|
||||
import CoreData
|
||||
|
||||
|
||||
// MARK: - DiffableDataSource
|
||||
|
||||
extension DiffableDataSource {
|
||||
|
||||
// MARK: - CollectionView
|
||||
|
||||
open class CollectionView<O: DynamicObject>: NSObject, UICollectionViewDataSource {
|
||||
|
||||
// MARK: Public
|
||||
|
||||
public typealias ObjectType = O
|
||||
|
||||
@nonobjc
|
||||
public init(collectionView: UICollectionView, dataStack: DataStack, cellProvider: @escaping (UICollectionView, IndexPath, ObjectType) -> UICollectionViewCell?, supplementaryViewProvider: @escaping (UICollectionView, String, IndexPath) -> UICollectionReusableView? = { _, _, _ in nil }) {
|
||||
|
||||
self.collectionView = collectionView
|
||||
self.cellProvider = cellProvider
|
||||
self.supplementaryViewProvider = supplementaryViewProvider
|
||||
self.dataStack = dataStack
|
||||
|
||||
super.init()
|
||||
|
||||
// if #available(iOS 13.0, tvOS 13.0, watchOS 6.0, macOS 10.15, *) {
|
||||
//
|
||||
// self.rawDataSource = UITableViewDiffableDataSource<String, O.ObjectID>(
|
||||
// tableView: tableView,
|
||||
// cellProvider: { [weak self] (tableView, indexPath, objectID) -> UITableViewCell? in
|
||||
//
|
||||
// guard let self = self else {
|
||||
//
|
||||
// return nil
|
||||
// }
|
||||
// guard let object = self.dataStack.fetchExisting(objectID) as O? else {
|
||||
//
|
||||
// return nil
|
||||
// }
|
||||
// return self.cellProvider(tableView, indexPath, object)
|
||||
// }
|
||||
// )
|
||||
// }
|
||||
// else {
|
||||
|
||||
self.rawDataSource = Internals.DiffableDataUIDispatcher<O>(dataStack: dataStack)
|
||||
// }
|
||||
|
||||
collectionView.dataSource = self
|
||||
}
|
||||
|
||||
public func apply(_ snapshot: ListSnapshot<ObjectType>, animatingDifferences: Bool = true) {
|
||||
|
||||
let diffableSnapshot = snapshot.diffableSnapshot
|
||||
// if #available(iOS 13.0, tvOS 13.0, watchOS 6.0, macOS 10.15, *) {
|
||||
//
|
||||
// self.modernDataSource.apply(
|
||||
// diffableSnapshot as! NSDiffableDataSourceSnapshot<String, NSManagedObjectID>,
|
||||
// animatingDifferences: animatingDifferences,
|
||||
// completion: nil
|
||||
// )
|
||||
// }
|
||||
// else {
|
||||
|
||||
self.legacyDataSource.apply(
|
||||
diffableSnapshot as! Internals.DiffableDataSourceSnapshot,
|
||||
view: self.collectionView,
|
||||
animatingDifferences: animatingDifferences,
|
||||
performUpdates: { collectionView, changeset, setSections in
|
||||
|
||||
collectionView.reload(
|
||||
using: changeset,
|
||||
setData: setSections
|
||||
)
|
||||
}
|
||||
)
|
||||
// }
|
||||
}
|
||||
|
||||
public func itemIdentifier(for indexPath: IndexPath) -> O.ObjectID? {
|
||||
|
||||
// if #available(iOS 13.0, tvOS 13.0, watchOS 6.0, macOS 10.15, *) {
|
||||
//
|
||||
// return self.modernDataSource.itemIdentifier(for: indexPath)
|
||||
// }
|
||||
// else {
|
||||
|
||||
return self.legacyDataSource.itemIdentifier(for: indexPath)
|
||||
// }
|
||||
}
|
||||
|
||||
public func indexPath(for itemIdentifier: O.ObjectID) -> IndexPath? {
|
||||
|
||||
// if #available(iOS 13.0, tvOS 13.0, watchOS 6.0, macOS 10.15, *) {
|
||||
//
|
||||
// return self.modernDataSource.indexPath(for: itemIdentifier)
|
||||
// }
|
||||
// else {
|
||||
|
||||
return self.legacyDataSource.indexPath(for: itemIdentifier)
|
||||
// }
|
||||
}
|
||||
|
||||
|
||||
// MARK: - UICollectionViewDataSource
|
||||
|
||||
@objc
|
||||
public dynamic func numberOfSections(in collectionView: UICollectionView) -> Int {
|
||||
|
||||
// if #available(iOS 13.0, tvOS 13.0, watchOS 6.0, macOS 10.15, *) {
|
||||
//
|
||||
// return self.modernDataSource.numberOfSections(in: tableView)
|
||||
// }
|
||||
// else {
|
||||
|
||||
return self.legacyDataSource.numberOfSections()
|
||||
// }
|
||||
}
|
||||
|
||||
@objc
|
||||
public dynamic func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
|
||||
|
||||
// if #available(iOS 13.0, tvOS 13.0, watchOS 6.0, macOS 10.15, *) {
|
||||
//
|
||||
// return self.modernDataSource.tableView(tableView, numberOfRowsInSection: section)
|
||||
// }
|
||||
// else {
|
||||
|
||||
return self.legacyDataSource.numberOfItems(inSection: section)
|
||||
// }
|
||||
}
|
||||
|
||||
@objc
|
||||
open dynamic func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
|
||||
|
||||
// if #available(iOS 13.0, tvOS 13.0, watchOS 6.0, macOS 10.15, *) {
|
||||
//
|
||||
// return self.modernDataSource.tableView(tableView, cellForRowAt: indexPath)
|
||||
// }
|
||||
// else {
|
||||
|
||||
guard let objectID = self.legacyDataSource.itemIdentifier(for: indexPath) else {
|
||||
|
||||
Internals.abort("Object at \(Internals.typeName(IndexPath.self)) \(indexPath) already removed from list")
|
||||
}
|
||||
guard let object = self.dataStack.fetchExisting(objectID) as O? else {
|
||||
|
||||
Internals.abort("Object at \(Internals.typeName(IndexPath.self)) \(indexPath) has been deleted")
|
||||
}
|
||||
guard let cell = self.cellProvider(collectionView, indexPath, object) else {
|
||||
|
||||
Internals.abort("\(Internals.typeName(UICollectionViewDataSource.self)) returned a `nil` cell for \(Internals.typeName(IndexPath.self)) \(indexPath)")
|
||||
}
|
||||
return cell
|
||||
// }
|
||||
}
|
||||
|
||||
@objc
|
||||
open dynamic func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
|
||||
|
||||
guard let view = self.supplementaryViewProvider(collectionView, kind, indexPath) else {
|
||||
|
||||
return UICollectionReusableView()
|
||||
}
|
||||
return view
|
||||
}
|
||||
|
||||
|
||||
// MARK: Private
|
||||
|
||||
private weak var collectionView: UICollectionView?
|
||||
|
||||
private let dataStack: DataStack
|
||||
private let cellProvider: (UICollectionView, IndexPath, ObjectType) -> UICollectionViewCell?
|
||||
private let supplementaryViewProvider: (UICollectionView, String, IndexPath) -> UICollectionReusableView?
|
||||
private var rawDataSource: Any!
|
||||
|
||||
// @available(iOS 13.0, tvOS 13.0, watchOS 6.0, macOS 10.15, *)
|
||||
// private var modernDataSource: UITableViewDiffableDataSource<String, O.ObjectID> {
|
||||
//
|
||||
// return self.rawDataSource as! UITableViewDiffableDataSource<String, O.ObjectID>
|
||||
// }
|
||||
|
||||
private var legacyDataSource: Internals.DiffableDataUIDispatcher<O> {
|
||||
|
||||
return self.rawDataSource as! Internals.DiffableDataUIDispatcher<O>
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: - UICollectionView
|
||||
|
||||
extension UICollectionView {
|
||||
|
||||
// MARK: FilePrivate
|
||||
|
||||
@nonobjc
|
||||
fileprivate func reload<C, O>(
|
||||
using stagedChangeset: Internals.DiffableDataUIDispatcher<O>.StagedChangeset<C>,
|
||||
interrupt: ((Internals.DiffableDataUIDispatcher<O>.Changeset<C>) -> Bool)? = nil,
|
||||
setData: (C) -> Void
|
||||
) {
|
||||
|
||||
if case .none = window, let data = stagedChangeset.last?.data {
|
||||
|
||||
setData(data)
|
||||
self.reloadData()
|
||||
return
|
||||
}
|
||||
for changeset in stagedChangeset {
|
||||
|
||||
if let interrupt = interrupt, interrupt(changeset), let data = stagedChangeset.last?.data {
|
||||
|
||||
setData(data)
|
||||
self.reloadData()
|
||||
return
|
||||
}
|
||||
self.performBatchUpdates(
|
||||
{
|
||||
setData(changeset.data)
|
||||
|
||||
if !changeset.sectionDeleted.isEmpty {
|
||||
|
||||
self.deleteSections(IndexSet(changeset.sectionDeleted))
|
||||
}
|
||||
if !changeset.sectionInserted.isEmpty {
|
||||
|
||||
self.insertSections(IndexSet(changeset.sectionInserted))
|
||||
}
|
||||
if !changeset.sectionUpdated.isEmpty {
|
||||
|
||||
self.reloadSections(IndexSet(changeset.sectionUpdated))
|
||||
}
|
||||
for (source, target) in changeset.sectionMoved {
|
||||
|
||||
self.moveSection(source, toSection: target)
|
||||
}
|
||||
if !changeset.elementDeleted.isEmpty {
|
||||
|
||||
self.deleteItems(at: changeset.elementDeleted.map { IndexPath(row: $0.element, section: $0.section) })
|
||||
}
|
||||
if !changeset.elementInserted.isEmpty {
|
||||
|
||||
self.insertItems(at: changeset.elementInserted.map { IndexPath(row: $0.element, section: $0.section) })
|
||||
}
|
||||
if !changeset.elementUpdated.isEmpty {
|
||||
|
||||
self.reloadItems(at: changeset.elementUpdated.map { IndexPath(row: $0.element, section: $0.section) })
|
||||
}
|
||||
for (source, target) in changeset.elementMoved {
|
||||
|
||||
self.moveItem(at: IndexPath(row: source.element, section: source.section), to: IndexPath(row: target.element, section: target.section))
|
||||
}
|
||||
},
|
||||
completion: nil
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#endif
|
||||
@@ -152,12 +152,12 @@ public final class LiveList<O: DynamicObject>: Hashable {
|
||||
|
||||
public func section(containingItem item: LiveObject<O>) -> SectionID? {
|
||||
|
||||
return self.snapshot.sectionIdentifier(containingItem: item.cs_id())
|
||||
return self.snapshot.sectionIdentifier(containingItem: item.objectID())
|
||||
}
|
||||
|
||||
public func indexOfItem(_ item: LiveObject<O>) -> Int? {
|
||||
|
||||
return self.snapshot.indexOfItem(item.cs_id())
|
||||
return self.snapshot.indexOfItem(item.objectID())
|
||||
}
|
||||
|
||||
public func indexOfSection(_ identifier: SectionID) -> Int? {
|
||||
|
||||
@@ -101,7 +101,7 @@ public final class LiveObject<O: DynamicObject>: ObjectRepresentation, Hashable
|
||||
|
||||
return self.lazySnapshot
|
||||
}
|
||||
return .init(id: self.id, context: context)
|
||||
return .init(objectID: self.id, context: context)
|
||||
}
|
||||
|
||||
public func asSnapshot(in transaction: BaseDataTransaction) -> ObjectSnapshot<O>? {
|
||||
@@ -111,7 +111,7 @@ public final class LiveObject<O: DynamicObject>: ObjectRepresentation, Hashable
|
||||
|
||||
return self.lazySnapshot
|
||||
}
|
||||
return .init(id: self.id, context: context)
|
||||
return .init(objectID: self.id, context: context)
|
||||
}
|
||||
|
||||
public func asObjectMonitor(in dataStack: DataStack) -> ObjectMonitor<O>? {
|
||||
@@ -150,7 +150,7 @@ public final class LiveObject<O: DynamicObject>: ObjectRepresentation, Hashable
|
||||
self.init(
|
||||
objectID: objectID,
|
||||
context: context,
|
||||
initializer: ObjectSnapshot<O>.init(id:context:)
|
||||
initializer: ObjectSnapshot<O>.init(objectID:context:)
|
||||
)
|
||||
}
|
||||
|
||||
@@ -192,6 +192,11 @@ public final class LiveObject<O: DynamicObject>: ObjectRepresentation, Hashable
|
||||
}
|
||||
}
|
||||
|
||||
fileprivate var object: O {
|
||||
|
||||
return self.context.fetchExisting(self.id)!
|
||||
}
|
||||
|
||||
|
||||
// MARK: Private
|
||||
|
||||
|
||||
@@ -40,7 +40,7 @@ import CoreData
|
||||
Observers registered via `addObserver(_:)` are not retained. `ObjectMonitor` only keeps a `weak` reference to all observers, thus keeping itself free from retain-cycles.
|
||||
*/
|
||||
@available(macOS 10.12, *)
|
||||
public final class ObjectMonitor<D: DynamicObject>: ObjectRepresentation, Equatable {
|
||||
public final class ObjectMonitor<O: DynamicObject>: ObjectRepresentation, Equatable {
|
||||
|
||||
/**
|
||||
Returns the `DynamicObject` instance being observed, or `nil` if the object was already deleted.
|
||||
@@ -122,35 +122,35 @@ public final class ObjectMonitor<D: DynamicObject>: ObjectRepresentation, Equata
|
||||
|
||||
// MARK: ObjectRepresentation
|
||||
|
||||
public func objectID() -> D.ObjectID {
|
||||
public func objectID() -> O.ObjectID {
|
||||
|
||||
return self.id
|
||||
}
|
||||
|
||||
public func asLiveObject(in dataStack: DataStack) -> LiveObject<D>? {
|
||||
public func asLiveObject(in dataStack: DataStack) -> LiveObject<O>? {
|
||||
|
||||
let context = dataStack.unsafeContext()
|
||||
return .init(objectID: self.id, context: context)
|
||||
}
|
||||
|
||||
public func asEditable(in transaction: BaseDataTransaction) -> D? {
|
||||
public func asEditable(in transaction: BaseDataTransaction) -> O? {
|
||||
|
||||
return self.context.fetchExisting(self.id)
|
||||
}
|
||||
|
||||
public func asSnapshot(in dataStack: DataStack) -> ObjectSnapshot<D>? {
|
||||
public func asSnapshot(in dataStack: DataStack) -> ObjectSnapshot<O>? {
|
||||
|
||||
let context = dataStack.unsafeContext()
|
||||
return .init(id: self.id, context: context)
|
||||
return .init(objectID: self.id, context: context)
|
||||
}
|
||||
|
||||
public func asSnapshot(in transaction: BaseDataTransaction) -> ObjectSnapshot<D>? {
|
||||
public func asSnapshot(in transaction: BaseDataTransaction) -> ObjectSnapshot<O>? {
|
||||
|
||||
let context = transaction.unsafeContext()
|
||||
return .init(id: self.id, context: context)
|
||||
return .init(objectID: self.id, context: context)
|
||||
}
|
||||
|
||||
public func asObjectMonitor(in dataStack: DataStack) -> ObjectMonitor<D>? {
|
||||
public func asObjectMonitor(in dataStack: DataStack) -> ObjectMonitor<O>? {
|
||||
|
||||
let context = dataStack.unsafeContext()
|
||||
if self.context == context {
|
||||
@@ -160,31 +160,6 @@ public final class ObjectMonitor<D: DynamicObject>: ObjectRepresentation, Equata
|
||||
return .init(objectID: self.id, context: dataStack.unsafeContext())
|
||||
}
|
||||
|
||||
public typealias ObjectType = D
|
||||
|
||||
public static func cs_fromRaw(object: NSManagedObject) -> Self {
|
||||
|
||||
return self.init(
|
||||
context: object.managedObjectContext!,
|
||||
objectID: object.objectID
|
||||
)
|
||||
}
|
||||
|
||||
public func cs_id() -> ObjectType.ObjectID {
|
||||
|
||||
return self.objectID
|
||||
}
|
||||
|
||||
public func cs_object() -> D? {
|
||||
|
||||
return self.object
|
||||
}
|
||||
|
||||
public func cs_rawObject(in context: NSManagedObjectContext) -> NSManagedObject? {
|
||||
|
||||
return context.fetchExisting(self.objectID)
|
||||
}
|
||||
|
||||
|
||||
// MARK: Equatable
|
||||
|
||||
@@ -238,7 +213,7 @@ public final class ObjectMonitor<D: DynamicObject>: ObjectRepresentation, Equata
|
||||
|
||||
let fetchedResultsControllerDelegate = Internals.FetchedResultsControllerDelegate()
|
||||
|
||||
self.objectID = objectID
|
||||
self.id = objectID
|
||||
self.fetchedResultsController = fetchedResultsController
|
||||
self.fetchedResultsControllerDelegate = fetchedResultsControllerDelegate
|
||||
|
||||
@@ -331,7 +306,7 @@ public final class ObjectMonitor<D: DynamicObject>: ObjectRepresentation, Equata
|
||||
|
||||
// MARK: Private
|
||||
|
||||
private let objectID: ObjectType.ObjectID
|
||||
private let id: O.ObjectID
|
||||
private let fetchedResultsController: Internals.CoreStoreFetchedResultsController
|
||||
private let fetchedResultsControllerDelegate: Internals.FetchedResultsControllerDelegate
|
||||
private var lastCommittedAttributes = [String: NSObject]()
|
||||
@@ -339,6 +314,11 @@ public final class ObjectMonitor<D: DynamicObject>: ObjectRepresentation, Equata
|
||||
private var willChangeObjectKey: Void?
|
||||
private var didDeleteObjectKey: Void?
|
||||
private var didUpdateObjectKey: Void?
|
||||
|
||||
private var context: NSManagedObjectContext {
|
||||
|
||||
return self.fetchedResultsController.managedObjectContext
|
||||
}
|
||||
|
||||
private func registerChangeNotification(_ notificationKey: UnsafeRawPointer, name: Notification.Name, toObserver observer: AnyObject, callback: @escaping (_ monitor: ObjectMonitor<ObjectType>) -> Void) {
|
||||
|
||||
|
||||
@@ -69,40 +69,50 @@ public protocol ObjectRepresentation {
|
||||
func asObjectMonitor(in dataStack: DataStack) -> ObjectMonitor<ObjectType>?
|
||||
}
|
||||
|
||||
extension LiveObject {
|
||||
|
||||
public static func cs_object(in context: NSManagedObjectContext) -> LiveObject<O>? {
|
||||
|
||||
return nil
|
||||
}
|
||||
}
|
||||
extension NSManagedObject: ObjectRepresentation {}
|
||||
|
||||
extension ObjectMonitor {
|
||||
|
||||
public static func cs_object(in context: NSManagedObjectContext) -> LiveObject<D>? {
|
||||
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
extension NSManagedObject: ObjectRepresentation {
|
||||
|
||||
|
||||
}
|
||||
|
||||
extension CoreStoreObject: ObjectRepresentation {
|
||||
|
||||
}
|
||||
extension CoreStoreObject: ObjectRepresentation {}
|
||||
|
||||
extension DynamicObject where Self: ObjectRepresentation {
|
||||
|
||||
public static func cs_object(in context: NSManagedObjectContext) -> LiveObject<Self>? {
|
||||
|
||||
fatalError()
|
||||
|
||||
// MARK: ObjectRepresentation
|
||||
|
||||
public func objectID() -> Self.ObjectID {
|
||||
|
||||
return self.cs_id()
|
||||
}
|
||||
|
||||
public func cs_rawObject(in context: NSManagedObjectContext) -> NSManagedObject? {
|
||||
|
||||
|
||||
public func asLiveObject(in dataStack: DataStack) -> LiveObject<Self>? {
|
||||
|
||||
let context = dataStack.unsafeContext()
|
||||
return .init(objectID: self.cs_id(), context: context)
|
||||
}
|
||||
|
||||
public func asEditable(in transaction: BaseDataTransaction) -> Self? {
|
||||
|
||||
let context = transaction.unsafeContext()
|
||||
if self.cs_toRaw().managedObjectContext == context {
|
||||
|
||||
return self
|
||||
}
|
||||
return context.fetchExisting(self.cs_id())
|
||||
}
|
||||
|
||||
public func asSnapshot(in dataStack: DataStack) -> ObjectSnapshot<Self>? {
|
||||
|
||||
let context = dataStack.unsafeContext()
|
||||
return .init(objectID: self.cs_id(), context: context)
|
||||
}
|
||||
|
||||
public func asSnapshot(in transaction: BaseDataTransaction) -> ObjectSnapshot<Self>? {
|
||||
|
||||
let context = transaction.unsafeContext()
|
||||
return .init(objectID: self.cs_id(), context: context)
|
||||
}
|
||||
|
||||
public func asObjectMonitor(in dataStack: DataStack) -> ObjectMonitor<Self>? {
|
||||
|
||||
let context = dataStack.unsafeContext()
|
||||
return .init(objectID: self.cs_id(), context: context)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -69,7 +69,7 @@ public struct ObjectSnapshot<O: DynamicObject>: SnapshotResult, ObjectRepresenta
|
||||
|
||||
return self
|
||||
}
|
||||
return .init(id: self.id, context: context)
|
||||
return .init(objectID: self.id, context: context)
|
||||
}
|
||||
|
||||
public func asSnapshot(in transaction: BaseDataTransaction) -> ObjectSnapshot<O>? {
|
||||
@@ -79,7 +79,7 @@ public struct ObjectSnapshot<O: DynamicObject>: SnapshotResult, ObjectRepresenta
|
||||
|
||||
return self
|
||||
}
|
||||
return .init(id: self.id, context: context)
|
||||
return .init(objectID: self.id, context: context)
|
||||
}
|
||||
|
||||
public func asObjectMonitor(in dataStack: DataStack) -> ObjectMonitor<O>? {
|
||||
@@ -108,9 +108,9 @@ public struct ObjectSnapshot<O: DynamicObject>: SnapshotResult, ObjectRepresenta
|
||||
|
||||
// MARK: Internal
|
||||
|
||||
internal init(id: ID, context: NSManagedObjectContext) {
|
||||
internal init(objectID: O.ObjectID, context: NSManagedObjectContext) {
|
||||
|
||||
self.id = id
|
||||
self.id = objectID
|
||||
self.context = context
|
||||
self.values = O.cs_snapshotDictionary(id: id, context: context) as NSDictionary
|
||||
}
|
||||
|
||||
@@ -38,12 +38,9 @@ extension UnsafeDataTransaction {
|
||||
- parameter object: the `ObjectRepresentation` to observe changes from
|
||||
- returns: a `ObjectMonitor` that monitors changes to `object`
|
||||
*/
|
||||
public func monitorObject<O: ObjectRepresentation>(_ object: O) -> ObjectMonitor<O.ObjectType> {
|
||||
|
||||
return ObjectMonitor(
|
||||
unsafeTransaction: self,
|
||||
object: object
|
||||
)
|
||||
public func monitorObject<O: DynamicObject>(_ object: O) -> ObjectMonitor<O> {
|
||||
|
||||
return .init(objectID: object.cs_id(), context: self.unsafeContext())
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user