CoreStoreObject snapshot doesn't work with ObjectPublisher #308

Closed
opened 2025-12-29 18:25:32 +01:00 by adam · 6 comments
Owner

Originally created by @joeljfischer on GitHub (Feb 3, 2020).

Referencing a property on an ObjectSnapshot<MyObject> where MyObject inherits from CoreStoreObject results in an error:

Referencing subscript 'subscript(dynamicMember:)' on 'ObjectSnapshot' requires that 'MyObject' inherit from 'NSManagedObject'

Do I have to change my object to be an NSManagedObject to get this to work?

Originally created by @joeljfischer on GitHub (Feb 3, 2020). Referencing a property on an `ObjectSnapshot<MyObject>` where `MyObject` inherits from `CoreStoreObject` results in an error: > Referencing subscript 'subscript(dynamicMember:)' on 'ObjectSnapshot' requires that 'MyObject' inherit from 'NSManagedObject' Do I have to change my object to be an `NSManagedObject` to get this to work?
adam added the questionpending labels 2025-12-29 18:25:32 +01:00
adam closed this issue 2025-12-29 18:25:32 +01:00
Author
Owner

@JohnEstropia commented on GitHub (Feb 4, 2020):

It simply means the signature doesn't match. Can you show the code you are getting this error in?

@JohnEstropia commented on GitHub (Feb 4, 2020): It simply means the signature doesn't match. Can you show the code you are getting this error in?
Author
Owner

@joeljfischer commented on GitHub (Feb 4, 2020):

Sure, I'll try to provide the relevant code:

private func prepareObject(_ object: MyObject) {
    observer = object.asPublisher(in: CoreStoreDefaults.dataStack)
    observer?.addObserver(self, { [weak self] (publisher) in
        self?.currentSnapshot = publisher.snapshot
        if let snapshot = self?.currentSnapshot {
            self?.updateUI(snapshot: snapshot)
        }
    })
}

private func updateUI(snapshot: ObjectSnapshot<MyObject>) {
    if snapshot.isPaused { // Accessing a property of MyObject fails with the error noted in the issue description
        playPauseButton.setImage(UIImage(systemName: "play.circle.fill"), for: .normal)
    } else {
        playPauseButton.setImage(UIImage(systemName: "pause.circle.fill"), for: .normal)
    }
}
@joeljfischer commented on GitHub (Feb 4, 2020): Sure, I'll try to provide the relevant code: ```swift private func prepareObject(_ object: MyObject) { observer = object.asPublisher(in: CoreStoreDefaults.dataStack) observer?.addObserver(self, { [weak self] (publisher) in self?.currentSnapshot = publisher.snapshot if let snapshot = self?.currentSnapshot { self?.updateUI(snapshot: snapshot) } }) } private func updateUI(snapshot: ObjectSnapshot<MyObject>) { if snapshot.isPaused { // Accessing a property of MyObject fails with the error noted in the issue description playPauseButton.setImage(UIImage(systemName: "play.circle.fill"), for: .normal) } else { playPauseButton.setImage(UIImage(systemName: "pause.circle.fill"), for: .normal) } } ```
Author
Owner

@JohnEstropia commented on GitHub (Feb 4, 2020):

What is isPaused? Is it a computed property? If so you will need to write that property in an extension:

extension ObjectSnapshot where O == MyObject {
    var isPaused: Bool {
        // ...
    }
}
@JohnEstropia commented on GitHub (Feb 4, 2020): What is `isPaused`? Is it a computed property? If so you will need to write that property in an extension: ```swift extension ObjectSnapshot where O == MyObject { var isPaused: Bool { // ... } } ```
Author
Owner

@joeljfischer commented on GitHub (Feb 4, 2020):

That's a good catch. It is a computed property, I didn't think about that. So I will need to write the computed property twice, once on an ObjectSnapshot extensions and once on the actual object itself (if I need to access it in both of those contexts)?

@joeljfischer commented on GitHub (Feb 4, 2020): That's a good catch. It is a computed property, I didn't think about that. So I will need to write the computed property twice, once on an `ObjectSnapshot` extensions and once on the actual object itself (if I need to access it in both of those contexts)?
Author
Owner

@JohnEstropia commented on GitHub (Feb 8, 2020):

Unfortunately that's a language limitation for now.

There will be a new way to do this using @Field.Virtual in an upcoming version (Swift 5.2): https://github.com/JohnEstropia/CoreStore/issues/359
It will be released soon after Xcode 11.4 goes out of beta.

@JohnEstropia commented on GitHub (Feb 8, 2020): Unfortunately that's a language limitation for now. There will be a new way to do this using `@Field.Virtual` in an upcoming version (Swift 5.2): https://github.com/JohnEstropia/CoreStore/issues/359 It will be released soon after Xcode 11.4 goes out of beta.
Author
Owner

@joeljfischer commented on GitHub (Feb 10, 2020):

Thanks for the help, this issue can be closed if you wish.

@joeljfischer commented on GitHub (Feb 10, 2020): Thanks for the help, this issue can be closed if you wish.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/CoreStore-JohnEstropia#308