JohnEstropia / CoreStore

Unleashing the real power of Core Data with the elegance and safety of Swift
MIT License
4k stars 254 forks source link

.fetchExisting is not working with NSManagedObjectID #365

Closed joeljfischer closed 4 years ago

joeljfischer commented 4 years ago

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.

JohnEstropia commented 4 years ago

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 4 years ago

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

joeljfischer commented 4 years ago

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!

JohnEstropia commented 4 years ago

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.

joeljfischer commented 4 years ago

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!

JohnEstropia commented 4 years ago

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 👍

joeljfischer commented 4 years ago

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.