简体   繁体   English

如何正确使用Swift中的generics在Realm的列表中存储不同类型的对象?

[英]How to properly use generics in Swift to store different types of objects in a list in Realm?

I am having a problem of Realm throwing an error that states that my type 'Category' is not managed by Realm.我遇到了 Realm 引发的错误,指出我的“类别”类型不受 Realm 管理。

For context, a 'Category' can hold either a collection of type 'Mistakes' or 'Subjects'.对于上下文,“类别”可以包含“错误”或“主题”类型的集合。 A 'Subject' can also contain a collection of 'Category' containing a collection of 'Mistake'. “主题”还可以包含“类别”的集合,其中包含“错误”的集合。 Thus, I am using generics so that I do not have to create two separate data models to group these two data models.因此,我使用的是 generics,这样我就不必创建两个单独的数据模型来对这两个数据模型进行分组。

class Category<O:Object>:Object{

//properties
@objc dynamic var categoryName:String = ""

//relationships
let ChildObjects = List<O>() //type of child object is to be set by the generic type; two possible types: subjects & mistakes

//mother objects
var Subjects = LinkingObjects(fromType: Subject.self, property: "MistakeCategories") //For Mistake Categories

}

I load the results of a category of subjects, using the following code:我使用以下代码加载了一类主题的结果:

var CategoryDatabase:Results<Category<Subject>>!

CategoryDatabase = R.objects(Category<Subject>.self)

I have also tried using the following code:我也尝试过使用以下代码:

var CategoryDatabase:Results<Category<Subject>>!

CategoryDatabase = R.objects(Category.self)

Nonetheless, I am having the error below.尽管如此,我遇到了以下错误。

*** Terminating app due to uncaught exception 'RLMException', reason: 'Object type '_TtGC8Go_For_A8CategoryCSo16RealmSwiftObject_' not managed by the Realm'

I have tried to search the internet for possible solutions but they their problems aren't exactly the same as mine.我试图在互联网上搜索可能的解决方案,但他们的问题与我的不完全相同。 Nonetheless, I found this which could be a possible answer as to why I am getting the error.尽管如此,我发现可能是我为什么会收到错误的一个可能答案。 However, the error and its cause is different.但是,错误及其原因是不同的。

This is my generic function that worked properly when storing different types of objects into Realm database:这是我的通用 function 在将不同类型的对象存储到 Realm 数据库时正常工作:

let realm = try! Realm()

func createObject<T: Object>(_ object: T) {
     do {
         try realm.write {
             realm.add(object)
           }
        } catch {
             print(error)
           }
        }

This is not going to work这行不通

let ChildObjects = List<O>() //...two possible types: subjects & mistakes

Realm Collections (Result, List) are homogenous and can only store zero or more instances of one object type; Realm Collections (结果,列表)是同质的,只能存储一个 object 类型的零个或多个实例; eg you can't store different classes of Object in the same List object例如,您不能在同一个列表 object 中存储不同类别的 Object

Generics are great but when you're working with different (possibly unrelated) objects, subjects and mistakes, using a generic is probably the wrong approach because queries and the actually data will be quite different. Generics 很棒,但是当您处理不同的(可能不相关的)对象、主题和错误时,使用泛型可能是错误的方法,因为查询和实际数据会完全不同。

Keep your data separate将您的数据分开

let subjects = List<SubjectClass>()

and

let mistakes = List<MistakeClass>()

of if there is some connection make it one class的如果有一些连接使它成为一个 class

class SubjectClass: Object {
    @objc dynamic var subject_name = ""
    @objc dynamic var is_mistake = false
}

of if a subject can have lots of mistakes如果一个主题可以有很多错误

class SubjectClass: Object {
    @objc dynamic var subject_name = ""
    let allMyMistakes = List<MistakeClass<>
}

For some data modeling info, review this answer and then a fantasic answer from @DávidPásztor here有关一些数据建模信息,请查看此答案,然后在此处查看@DávidPásztor 的精彩答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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