Steve M.
Steve M.

Reputation: 60

fatal error at runtime with Realm using LInkingOjbects

Getting the following fatal error.

fatal error: 'try!' expression unexpectedly raised an error: Error Domain=io.realm Code=1 "Schema validation failed due to the following errors: - Property 'GearModel.category' declared as origin of linking objects property 'CategoryRepository.gearList' links to type 'CategoryModel'" UserInfo={NSLocalizedDescription=Schema validation failed due to the following errors: - Property 'GearModel.category' declared as origin of linking objects property 'CategoryRepository.gearList' links to type 'CategoryModel', Error Code=1}:

Object models here: Gear Model is assigned to ONE only Category

import Foundation
import RealmSwift

class GearModel: Object{

    dynamic var gearID = NSUUID().uuidString
    dynamic var name = ""
    dynamic var manufacturer = ""
    dynamic var sortOrder = 0
    dynamic var category: CategoryModel?

    override class func primaryKey() -> String? {return "gearID"}
    }

Category Model: Trying to get list of all GearModel Items with the Category assigned.

    import Foundation
    import RealmSwift

    class CategoryModel: Object{

        dynamic var categoryID = NSUUID().uuidString
        dynamic var sortOrder = 0
        dynamic var name = ""
        let gearList = LinkingObjects(fromType: GearModel.self, property: "category")

override class func primaryKey() -> String? {return "categoryID"}
    }

Category Repository class: fatal error occurs on line "let realm =...." in the allCategories() func

import Foundation
import RealmSwift

class CategoryRepository: CategoryModel{

    class func GetGearItemsForCategory(CategoryID: String) -> Results<GearModel>
    {
        let realm = try! Realm()

        var gearItems = realm.objects(GearModel.self)

        let predicate = NSPredicate(format: "categoryID = %@", CategoryID)
        gearItems = gearItems.filter(predicate)
        return gearItems
    }

    class func GetCountOfGearItems(CategoryID: String) -> Int
    {
        let realm = try! Realm()

        var gearItems = realm.objects(GearModel.self)

        let predicate = NSPredicate(format: "categoryID = %@", CategoryID)
        gearItems = gearItems.filter(predicate)
        return gearItems.count
    }

    class func AddCategory(CategoryID: String, Name: String, SortOrder: Int){

        let realm = try! Realm()

        let category = CategoryModel(name: CategoryID, categoryID: Name, sortOrder: SortOrder)

        try! realm.write {
            realm.add(category)
        }
    }

    class func allCategories() -> Results<CategoryModel> {

        let realm = try! Realm()


        return realm.objects(CategoryModel.self).sorted(byProperty: "SortOrder")
    }

Upvotes: 1

Views: 671

Answers (1)

Thomas Goyne
Thomas Goyne

Reputation: 8138

Inheriting from a class which has LinkingObjects properties is not supported.

Object and List properties in Realm are not covariant, meaning that they can only point to exactly the specified class, and not subclasses of that class. This means that GearModel.category cannot link to CategoryRepository objects, but CategoryRepository has the inherited gearList property which is trying to list the GearModel objects which link to it.

In your specific case, it's not clear why CategoryRepository is inheriting from CategoryModel.

Upvotes: 1

Related Questions