简体   繁体   中英

Realm with Swift 5 Inverse relationship crashes the app on launch

I am learning how to use Realm db, as per examples I'm using the following tasks list with 2 classes: Category and Item, and each category may have many items but each item belong to one category as follows:

Category class

import Foundation
import RealmSwift

final class Category: Object {
    @objc dynamic var name : String = ""
    let items = List<Item>()
}

Item class

import Foundation
import RealmSwift

final class Item: Object {
    @objc dynamic var title: String = ""
    @objc dynamic var done : Bool = false
    let parentCategory = LinkingObjects(fromType: Category.self, property: "Items")
}

when I run the project, it crashes with the following error:

Fatal error: 'try!' expression unexpectedly raised an error: Error Domain=io.realm Code=1 "Schema validation failed due to the following errors:

  • Property 'Category.Items' declared as origin of linking objects property 'Item.parentCategory' does not exist" UserInfo={NSLocalizedDescription=Schema validation failed due to the following errors:
  • Property 'Category.Items' declared as origin of linking objects property 'Item.parentCategory' does not exist, Error Code=1}

General info:

Xcode version: 11.6

Realm version: 5.4.0

OS version: Catalina 10.15.3

iOS version : 13

I tried cleaning the project, restarting Xcode and Mac itself deleting realm file itself with no luck.

I searched stackoverflow questions, many tutorials and above all Realm's documentation , this code should run fine.

You've just got a typo in the inverse relationship let parentCategory property. "Items" should be "items" to match the property in the Category class

final class Item: Object {
    @objc dynamic var title: String = ""
    @objc dynamic var done : Bool = false
    let parentCategory = LinkingObjects(fromType: Category.self, property: "items")
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM