Data-swift / ManagedModels

A SwiftData like `@Model` infrastructure for CoreData.
https://www.alwaysrightinstitute.com/managedmodels/
Apache License 2.0
101 stars 3 forks source link

Usage Tagged (safe) types in CoreData #29

Open inekipelov opened 7 months ago

inekipelov commented 7 months ago

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 commented 7 months ago

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 commented 7 months ago

@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 commented 7 months ago

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 commented 7 months ago

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.

inekipelov commented 7 months ago

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 commented 7 months ago

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

helje5 commented 7 months ago

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

inekipelov commented 7 months ago

@helje5 Have new error

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

in PersistentModel+KVC.swift

helje5 commented 7 months ago

With the 'develop' branch?

inekipelov commented 7 months ago

With the 'develop' branch?

yes

helje5 commented 7 months ago

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.