2

So I am trying to work with LinkingObjects in Realm and seem to be getting bad results on the first level (linking not working).

I am going to add here all the code. Obviously not expecting people to go over it all, just want to show that I went over all answers that I saw so far in Stack Overflow.

class Book: Object {
    @objc dynamic var bookId = 0
    @objc dynamic var bookName = ""
    var sections = List<Section>()

    override static func primaryKey() -> String? {
        return "bookId"
    }
}

class Section: Object {
    @objc dynamic var sectionId = 0
    @objc dynamic var sectionName = ""
    let chapters = List<RChapter>()
    let book = LinkingObjects(fromType: Book.self, property: "sections")

    override static func primaryKey() -> String? {
        return "sectionId"
    }
}

class RChapter: Object {
    @objc dynamic var chapterId = 0
    @objc dynamic var chapterName = ""
    var section = LinkingObjects(fromType: Section.self, property: "chapters")

    override static func primaryKey() -> String? {
        return "chapterId"
    }
}

class DataBase {
    static func getChapterById(id: String) -> RChapter? {
        let realm = try! Realm()
        let chapter = realm.objects(RChapter.self).filter("chapterId = \(id)").first
        return chapter
    }

    static func writeToRealm(json: JSON) {
        let book = Book()
        book.bookId = json["bookId"].int!
        book.bookName = json["bookName"].string!
        let section = Section()
        section.sectionId = json["sectionId"].int!
        section.sectionName = json["sectionName"].string!
        let chapter = RChapter()
        chapter.chapterId = json["chapterId"].int!

        section.chapters.append(chapter)
        book.sections.append(section)

        let realm = try! Realm()
        try! realm.write {
            realm.add(book, update: true)
        }
    }
}

but after filling up the database I cannot get section from the chapter.

enter image description here

anyone know why? Thanks,

Community
  • 1
  • 1
Shlomo Koppel
  • 885
  • 7
  • 14
  • 1
    So where and how exactly are you trying to get the chapter from. The manager? What is that object? Where is that initialized? Etc? – EpicPandaForce Nov 29 '17 at 17:24
  • I am trying to get the chapter from a view controller model where I present it's content to the user. it contains some text (I removed all the content that is irrelevant so that the code should not be to long) technically there is another param named chapter.summary – Shlomo Koppel Dec 03 '17 at 12:47
  • That code is relevant to answer the question, and also what is `cannot get section`? Does it crash? Does it just not return it? Do you have a notification token? – EpicPandaForce Dec 03 '17 at 13:21
  • chapter?.section.first returns nil. thanks – Shlomo Koppel Dec 03 '17 at 13:47
  • Please file a ticket at our GitHub issue tracker (https://github.com/realm/realm-cocoa/issues), and enclose a reproduction case that clearly demonstrates the problem you're seeing. It's very difficult to debug a problem without having access to all the relevant code. Thanks. – AustinZ Dec 05 '17 at 21:50

0 Answers0