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.
anyone know why? Thanks,