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

Error with Optional RealmableEnumString on launch #76

Open
phiefferj24 opened this issue Aug 15, 2023 · 1 comment
Open

Error with Optional RealmableEnumString on launch #76

phiefferj24 opened this issue Aug 15, 2023 · 1 comment

Comments

@phiefferj24
Copy link

I have a class VideoEntry that conforms to Realmable. One of the properties on said class is season of type EntrySeason?. In AppDelegate I put both Realmable and EntrySeason in the register function but I am still met with an error stating that "'Property 'season' is declared as 'EntrySeason', which is not a supported RLMObject property type". See code snippets below.

Note that the error is thrown the second that DataManager (and therefore Realm) is initialized.
I have tried this with and without the configuration.objectTypes being set.

VideoEntry

final class VideoEntry: Realmable, Entry, Codable {
    typealias HistoryType = VideoHistory

    init() {}

    internal init(
        id: String,
        sourceId: String,
        title: String,
        alternativeTitles: [String],
        cover: String?,
        alternativeCovers: [String],
        banner: String?,
        alternativeBanners: [String],
        tags: [String],
        synopsis: String?,
        contentRating: ContentRating,
        status: Status,
        score: Double?,
        season: EntrySeason?,
        year: Int?,
        episodes: Double?,
        links: [String]
    ) {
        self.id = id
        self.sourceId = sourceId
        self.title = title
        self.alternativeTitles = alternativeTitles
        self.cover = cover
        self.alternativeCovers = alternativeCovers
        self.banner = banner
        self.alternativeBanners = alternativeBanners
        self.tags = tags
        self.synopsis = synopsis
        self.contentRating = contentRating
        self.status = status
        self.score = score
        self.season = season
        self.year = year
        self.episodes = episodes
        self.links = links
    }

    var id: String = ""
    var sourceId: String = ""
    var title: String = ""
    var alternativeTitles: [String] = []
    var cover: String?
    var alternativeCovers: [String] = []
    var banner: String?
    var alternativeBanners: [String] = []
    var tags: [String] = []
    var synopsis: String?
    var contentRating: ContentRating = .unknown
    var status: Status = .unknown
    var score: Double?
    var season: EntrySeason?
    var year: Int?
    var episodes: Double?
    var links: [String] = []

    static func == (lhs: VideoEntry, rhs: VideoEntry) -> Bool {
        lhs.id == rhs.id && lhs.sourceId == rhs.sourceId
    }

    func hash(into hasher: inout Hasher) {
        hasher.combine(self.id)
        hasher.combine(self.sourceId)
    }
}

EntrySeason

enum EntrySeason: String, RealmableEnumString, Codable {
    case winter = "WINTER"
    case spring = "SPRING"
    case summer = "SUMMER"
    case fall = "FALL"
}

AppDelegate Statements

Realm.registerRealmables([
      TextEntry.self,
      ImageEntry.self,
      VideoEntry.self,
      TextHistory.self,
      ImageHistory.self,
      VideoHistory.self,
      LibraryItem.self,
      LibraryCategory.self
], enums: [
      MediaType.self,
      EntrySeason.self,
      Status.self,
      ContentRating.self,
      HistoryStatus.self
])

DataManager.shared = DataManager()

DataManager

class DataManager {
    static var shared: DataManager!

    let realm: Realm

    init() {
        let realmables: [RealmableBase.Type] = [
            TextEntry.self,
            ImageEntry.self,
            VideoEntry.self,
            TextHistory.self,
            ImageHistory.self,
            VideoHistory.self,
            LibraryItem.self,
            LibraryCategory.self
        ]

        var configuration = Realm.Configuration()
        // TODO: REMOVE (and add schema versioning)
        configuration.deleteRealmIfMigrationNeeded = true
        configuration.objectTypes = realmables.compactMap({ $0.objectType() })
        guard let realm = try? Realm(configuration: configuration) else { fatalError("Realm could not be initialized.") }
        self.realm = realm
    }
}

Full error:

2023-08-14 23:05:15.100320-0400 Soshiki[71100:14452637] *** Terminating app due to uncaught exception 'RLMException', reason: 'Property 'season' is declared as 'EntrySeason', which is not a supported RLMObject property type. All properties must be primitives, NSString, NSDate, NSData, NSNumber, RLMArray, RLMSet, RLMDictionary, RLMLinkingObjects, RLMDecimal128, RLMObjectId, or subclasses of RLMObject. See https://www.mongodb.com/docs/realm-legacy/docs/objc/latest/api/Classes/RLMObject.html for more information.'
*** First throw call stack:
(
	0   CoreFoundation                      0x000000018040e7c8 __exceptionPreprocess + 172
	1   libobjc.A.dylib                     0x0000000180051144 objc_exception_throw + 56
	2   Soshiki                             0x0000000104adb464 -[RLMProperty setTypeFromRawType:] + 3260
	3   Soshiki                             0x0000000104adc694 -[RLMProperty initSwiftPropertyWithName:indexed:linkPropertyDescriptor:property:instance:] + 2792
	4   Soshiki                             0x0000000104ab8d44 +[RLMObjectSchema propertiesForClass:isSwift:] + 1084
	5   Soshiki                             0x0000000104ab7248 +[RLMObjectSchema schemaForObjectClass:] + 948
	6   Soshiki                             0x0000000104c5d370 _ZL13registerClassP10objc_class + 264
	7   Soshiki                             0x0000000104c5cb2c +[RLMSchema schemaWithObjectClasses:] + 704
	8   Soshiki                             0x0000000104da6260 $sSo9RLMSchemaC13objectClassesABSayyXlXpG_tcfCTO + 76
	9   Soshiki                             0x0000000104da61e4 $s10RealmSwift0A0V13ConfigurationV11objectTypesSaySo13RLMObjectBaseCmGSgvsSo9RLMSchemaCAIXEfU_ + 108
	10  libswiftCore.dylib                  0x000000018bd496fc $sSq3mapyqd__Sgqd__xKXEKlF + 240
	11  Soshiki                             0x0000000104da5620 $s10RealmSwift0A0V13ConfigurationV11objectTypesSaySo13RLMObjectBaseCmGSgvs + 112
	12  Soshiki                             0x00000001048162d4 $s7Soshiki11DataManagerCACycfc + 868
	13  Soshiki                             0x0000000104815f64 $s7Soshiki11DataManagerCACycfC + 44
	14  Soshiki                             0x00000001047d4be4 $s7Soshiki11AppDelegateC11application_29didFinishLaunchingWithOptionsSbSo13UIApplicationC_SDySo0j6LaunchI3KeyaypGSgtF + 2696
	15  Soshiki                             0x00000001047d599c $s7Soshiki11AppDelegateC11application_29didFinishLaunchingWithOptionsSbSo13UIApplicationC_SDySo0j6LaunchI3KeyaypGSgtFTo + 188
	16  UIKitCore                           0x000000011ecfee38 -[UIApplication _handleDelegateCallbacksWithOptions:isSuspended:restoreState:] + 292
	17  UIKitCore                           0x000000011ed00380 -[UIApplication _callInitializationDelegatesWithActions:forCanvas:payload:fromOriginatingProcess:] + 2776
	18  UIKitCore                           0x000000011ed05040 -[UIApplication _runWithMainScene:transitionContext:completion:] + 856
	19  UIKitCore                           0x000000011e4694d0 -[_UISceneLifecycleMultiplexer completeApplicationLaunchWithFBSScene:transitionContext:] + 148
	20  UIKitCore                           0x000000011ed02008 -[UIApplication _compellApplicationLaunchToCompleteUnconditionally] + 44
	21  UIKitCore                           0x000000011ed02358 -[UIApplication _run] + 832
	22  UIKitCore                           0x000000011ed06374 UIApplicationMain + 124
	23  libswiftUIKit.dylib                 0x000000010ac00fd0 $s5UIKit17UIApplicationMainys5Int32VAD_SpySpys4Int8VGGSgSSSgAJtF + 100
	24  Soshiki                             0x00000001047d6580 $sSo21UIApplicationDelegateP5UIKitE4mainyyFZ + 104
	25  Soshiki                             0x00000001047d6508 $s7Soshiki11AppDelegateC5$mainyyFZ + 44
	26  Soshiki                             0x00000001047d790c main + 28
	27  dyld                                0x00000001096d9fa0 start_sim + 20
	28  ???                                 0x000000010940108c 0x0 + 4450160780
	29  ???                                 0x6f50000000000000 0x0 + 8020910936346853376
)
libc++abi: terminating with uncaught exception of type NSException
*** Terminating app due to uncaught exception 'RLMException', reason: 'Property 'season' is declared as 'EntrySeason', which is not a supported RLMObject property type. All properties must be primitives, NSString, NSDate, NSData, NSNumber, RLMArray, RLMSet, RLMDictionary, RLMLinkingObjects, RLMDecimal128, RLMObjectId, or subclasses of RLMObject. See https://www.mongodb.com/docs/realm-legacy/docs/objc/latest/api/Classes/RLMObject.html for more information.'
terminating with uncaught exception of type NSException
CoreSimulator 857.14 - Device: iPhone 14 Pro Max (DA7B039D-54AA-40CD-A098-15AAFF8A73AC) - Runtime: iOS 16.2 (20C52) - DeviceType: iPhone 14 Pro Max

Thanks in advance for the help.

@arturdev
Copy link
Collaborator

Currently, optional enums are not supported. What you could do is to create a fileprivate enum with an additional case none, use it as a replacement for nil case:

fileprivate enum _EntrySeason: String, RealmableEnumString, Codable {
    case winter = "WINTER"
    case spring = "SPRING"
    case summer = "SUMMER"
    case fall = "FALL"
    case none
}

final class VideoEntry: Realmable, Entry, Codable {
     //...
     fileprivate var _season: _EntrySeason = .none
     //...     
}

extension VideoEntry {
    var season: EntrySeason? {
         return  EntrySeason(rawValue: _sesion.rawValue)
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants