Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Usage Tagged (safe) types in CoreData #29

Open
inekipelov opened this issue Feb 14, 2024 · 11 comments
Open

Usage Tagged (safe) types in CoreData #29

inekipelov opened this issue Feb 14, 2024 · 11 comments
Assignees
Labels
bug Something isn't working question Further information is requested

Comments

@inekipelov
Copy link

Hi @helje5
Thank you for quick solve previous issue

I use Tagged
Tagged structure also have Codable protocol realisation like RawValue type
Example

struct AccessTokenTagged {}
typealias AccessToken = Tagged<AccessTokenTagged, String>

@Model
final class StoredAccess: NSManagedObject {
    var token: AccessToken
    ...
}

When compilation as result we can see

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unacceptable type of value for attribute: property = "token"; desired type = NSString; given type = _TtGC13ManagedModels10CodableBoxGV6Tagged6TaggedV5WE_UC17AccessTokenTaggedSS__; value = <_TtGC13ManagedModels10CodableBoxGV6Tagged6TaggedV5WE_UC17AccessTokenTaggedSS__: 0x600000336460>.'

It's strange, because ManagedModels use CodableBox for wrap Codable types. How we can solve it issue in current realisation?

@helje5
Copy link
Contributor

helje5 commented Feb 14, 2024

Can't look into that right away, but RawValue properties are not supposed to be treated as Codable's. Instead they should just store as their rawValue (e.g. String in this case).

@inekipelov
Copy link
Author

@helje5

Can't look into that right away, but RawValue properties are not supposed to be treated as Codable's. Instead they should just store as their rawValue (e.g. String in this case).

Tagged supports both protocols RawRepresentable and Codable
Anyway it must works fine? Or I must add additional ValueTransformer?

@inekipelov
Copy link
Author

inekipelov commented Feb 14, 2024

Same troubles when trying usage enum types

enum AccessLevel: String, Codable {
    case selfOnly = "selfonly"
    case master
    case technical
    case financial
}

@Model
final class StoredAccess: NSManagedObject {
    var accessLevel: AccessLevel
    ...
}

Crash

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unacceptable type of value for attribute: property = "accessLevel"; desired type = NSString; given type = _TtGC13ManagedModels10CodableBoxO5WE_UC11AccessLevel_; value = <_TtGC13ManagedModels10CodableBoxO5WE_UC11AccessLevel_: 0x600000030840>.'

@helje5
Copy link
Contributor

helje5 commented Feb 14, 2024

Hm, weird. That is pretty much the same I'm doing in the sample app. Though maybe not Codable, that might interfere, need to check.

@helje5 helje5 self-assigned this Feb 14, 2024
@helje5 helje5 added the bug Something isn't working label Feb 14, 2024
@inekipelov
Copy link
Author

inekipelov commented Feb 14, 2024

I'm some change init code and now have new console crash with fatalError

ManagedModels/PersistentModel+KVC.swift:249: Fatal error: No box found for non-optional Codable value for key token?

helje5 added a commit that referenced this issue Feb 15, 2024
Should fix issue #29

This was missing overloads for RawRepresentable & Codable,
not sure why this didn't produce an ambiguity warning.
@helje5
Copy link
Contributor

helje5 commented Feb 15, 2024

I pushed a fix for Codable+RawRepresentable properties to develop, but I didn't try the Tagged thing.

@helje5
Copy link
Contributor

helje5 commented Feb 16, 2024

Also did a quick test w/ Tagged, seems to work.

@helje5 helje5 closed this as completed Feb 16, 2024
@inekipelov
Copy link
Author

@helje5 Have new error

Could not cast value of type 'Swift.Optional<Any>' (0x1f624fc18) to 'Swift.String' (0x1f6248898).

in PersistentModel+KVC.swift

@helje5
Copy link
Contributor

helje5 commented Feb 16, 2024

With the 'develop' branch?

@helje5 helje5 reopened this Feb 16, 2024
@inekipelov
Copy link
Author

With the 'develop' branch?

yes

@helje5
Copy link
Contributor

helje5 commented Feb 16, 2024

Can you produce a mini-project that reproduces that? All tests run through and I tried w/ the demo app w/ success.
Please make sure the develop branch is up2date.

@helje5 helje5 added the question Further information is requested label Feb 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants