ManagedModels icon indicating copy to clipboard operation
ManagedModels copied to clipboard

Usage Tagged (safe) types in CoreData

Open inekipelov opened this issue 1 year ago • 11 comments

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?

inekipelov avatar Feb 14 '24 17:02 inekipelov

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).

helje5 avatar Feb 14 '24 17:02 helje5

@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 avatar Feb 14 '24 17:02 inekipelov

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>.'

inekipelov avatar Feb 14 '24 18:02 inekipelov

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 avatar Feb 14 '24 18:02 helje5

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?

inekipelov avatar Feb 14 '24 20:02 inekipelov

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

helje5 avatar Feb 15 '24 15:02 helje5

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

helje5 avatar Feb 16 '24 13:02 helje5

@helje5 Have new error

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

in PersistentModel+KVC.swift

inekipelov avatar Feb 16 '24 14:02 inekipelov

With the 'develop' branch?

helje5 avatar Feb 16 '24 14:02 helje5

With the 'develop' branch?

yes

inekipelov avatar Feb 16 '24 14:02 inekipelov

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 avatar Feb 16 '24 15:02 helje5