.fetchExisting is not working with NSManagedObjectID #307

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

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

It seems to perhaps be an issue with overloading the method?

Doing:

guard let objectManagedID = self.dataSource.itemID(for: indexPath) else {
    return;
}
let timer = CoreStoreDefaults.dataStack.fetchExisting(objectManagedID)

causes an error: Argument type 'O.ObjectID' (aka 'NSManagedObjectID') does not conform to expected type 'DynamicObject'

I'm doing this to pull my object in my UITableView.DataSource methods after using a ListPublisher diffable data source.

I also tried casting O.ObjectID directly to NSManagedObjectID and the same error occurred.

Originally created by @joeljfischer on GitHub (Feb 3, 2020). It seems to perhaps be an issue with overloading the method? Doing: ```swift guard let objectManagedID = self.dataSource.itemID(for: indexPath) else { return; } let timer = CoreStoreDefaults.dataStack.fetchExisting(objectManagedID) ``` causes an error: `Argument type 'O.ObjectID' (aka 'NSManagedObjectID') does not conform to expected type 'DynamicObject'` I'm doing this to pull my object in my `UITableView.DataSource` methods after using a `ListPublisher` diffable data source. I also tried casting `O.ObjectID` directly to `NSManagedObjectID` and the same error occurred.
adam added the question label 2025-12-29 18:25:31 +01:00
adam closed this issue 2025-12-29 18:25:31 +01:00
Author
Owner

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

You need to tell it what object type you are querying because it cannot infer it from the ID.

let timer: TimerObject = CoreStoreDefaults.dataStack.fetchExisting(objectManagedID)
@JohnEstropia commented on GitHub (Feb 4, 2020): You need to tell it what object type you are querying because it cannot infer it from the ID. ```swift let timer: TimerObject = CoreStoreDefaults.dataStack.fetchExisting(objectManagedID) ```
Author
Owner

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

In the first place, why aren't you getting the object directly from the ListPublisher?
(not the DiffableDataSource)

The ListPublisher/ListSnapshot has methods/subscripts to get the object directly from the IndexPath

@JohnEstropia commented on GitHub (Feb 4, 2020): In the first place, why aren't you getting the object directly from the `ListPublisher`? (not the `DiffableDataSource`) The `ListPublisher`/`ListSnapshot` has methods/subscripts to get the object directly from the `IndexPath`
Author
Owner

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

That was the first place I looked, but I don't see a publicly available method for getting the object from the IndexPath. I checked the current snapshot of the ListPublisher, such as listPublisher.snapshot.items(atIndices:), but that's designed for the diffible data source and I'd have to figure out how to do a bunch of conversion.

Attempting to directly subscript it like so let item = listPublisher?[indexPath] leads to this error message Value of type 'ListPublisher<MyObject>' has no subscripts.

If you could point me in the right direction, that would be appreciated!

EDIT: Your first comment did work, however, so I can continue to use that. If there's a better / easier way though, I'd love to use that!

@joeljfischer commented on GitHub (Feb 4, 2020): That was the first place I looked, but I don't see a publicly available method for getting the object from the `IndexPath`. I checked the current snapshot of the `ListPublisher`, such as `listPublisher.snapshot.items(atIndices:)`, but that's designed for the diffible data source and I'd have to figure out how to do a bunch of conversion. Attempting to directly subscript it like so `let item = listPublisher?[indexPath]` leads to this error message `Value of type 'ListPublisher<MyObject>' has no subscripts`. If you could point me in the right direction, that would be appreciated! EDIT: Your first comment did work, however, so I can continue to use that. If there's a better / easier way though, I'd love to use that!
Author
Owner

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

listPublisher.snapshot[indexPath] is what you want.

... but that's designed for the diffible data source and I'd have to figure out how to do a bunch of conversion.

It's the other way around actually. The snapshot is your data, and you can actually have multiple DiffableDataSources with a shared snapshot.

@JohnEstropia commented on GitHub (Feb 4, 2020): `listPublisher.snapshot[indexPath]` is what you want. > ... but that's designed for the diffible data source and I'd have to figure out how to do a bunch of conversion. It's the other way around actually. The snapshot is your data, and you can actually have multiple `DiffableDataSource`s with a shared snapshot.
Author
Owner

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

I see what you're saying. The issue I'm trying to solve is passing the object into a table view cell, which can then observe the object. It looks like the snapshot has an asPublisher method, so then I could just use that instead. I'll give that a shot, thank you so much for the help!

@joeljfischer commented on GitHub (Feb 4, 2020): I see what you're saying. The issue I'm trying to solve is passing the object into a table view cell, which can then observe the object. It looks like the snapshot has an `asPublisher` method, so then I could just use that instead. I'll give that a shot, thank you so much for the help!
Author
Owner

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

Yes, you'll use asPublisher to convert the snapshot to a "live" object. In general, any form of "read" (from indexPaths, or from property values) is done through an intermittent "state" (ListSnapshot, ObjectSnapshot) because the publisher variants may change anytime.

Although, I think if you had trouble finding the current accessors then the documentation might be lacking somewhere, or the current methods are not intuitive enough. I'll see how we can improve the utilities 👍

@JohnEstropia commented on GitHub (Feb 5, 2020): Yes, you'll use `asPublisher` to convert the snapshot to a "live" object. In general, any form of "read" (from indexPaths, or from property values) is done through an intermittent "state" (ListSnapshot, ObjectSnapshot) because the publisher variants may change anytime. Although, I think if you had trouble finding the current accessors then the documentation might be lacking somewhere, or the current methods are not intuitive enough. I'll see how we can improve the utilities 👍
Author
Owner

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

Thanks for the support again. I agree that was not entirely clear in the documentation, but it makes more sense now. This issue can be closed if you wish.

@joeljfischer commented on GitHub (Feb 10, 2020): Thanks for the support again. I agree that was not entirely clear in the documentation, but it makes more sense now. 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#307