Realm查询对象属性字段由它的属性 [英] Realm query Object property field by it's property

查看:1463
本文介绍了Realm查询对象属性字段由它的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用swift开发iOS应用程序,并选择Realm作为数据库解决方案。我问了一个关于Realm的问题(领域问题)现在我有另一个。

I'm developing an application for iOS using swift and chose Realm as a database solution for it. I asked one question about Realm (Realm Question) and now I have another.

假设我们有这样的模式:

Suppose we have a schema like that:

class Person: Object {
    dynamic var id: String = NSUUID().UUIDString
    dynamic var name: String?
    dynamic var cars: Car?

class Car: Object {
    dynamic var name: String?

我有一个类(Person)包含任何数量的另一个类(Car)的对象。与人链接的汽车在该人的上下文中具有一些属性(并且对于不同的人,对于相同的汽车它们可以是不同的,或者对于一个人,两个类似的汽车是不同的)。使用List< ...>()我们不能为每个项目存储这样的属性,对吗?

I have one class (Person) that contains any number of objects of another class (Car). Car that are "linked" with the Person has some properties in context of that Person (and they ca be different for same Car for different Persons or for two similar Cars for one Person). Using List<...>() we can not store such properties for each Item, am I right?

如果我们只使用Car一个人,可以创建另一个类,只包括汽车的附加属性,并链接他们与ID的人加ID的车。但是,如果我们有两个类似的汽车有不同的附加属性,它不工作。

If we use Car only for one Person and only once we can create another class that includes only additional properties for Cars and links them with ID of Person plus ID of Car. But it does't work if we have two similar Cars with different additional properties.

所以,我怎么看待解决方案。有一个表(类)商店ID的人,ID一个汽车和此汽车的附加属性。对于同一人的另一辆汽车,它具有相同的人员ID,汽车ID(相同或不相同)和此汽车实例的另一个附加属性。

So, how I see the solution. Have one table (class) stores ID of Person, ID of one Car and additional properties for this Car. For another Car for the same Person it has the same Person ID, Car ID (same or not) and another additional properties for this instance of a Car.

问题(和我的意思)。具有该结构,我想查询该表中的所有汽车及其附加属性的人员ID等于some_id 。我应该怎么做?或者也许什么另一个结构(也许使用List< ...>)我应该实现这样的行为?

There is a problem (and a question that I mean). Having that structure I want to query all Cars from that table with their additional properties that have Person ID equals to some_id. How should I do this? Or maybe what another structure (maybe using List<...>) I should have to achieve such kind of behavior?

我希望我的问题更清楚。 p>

I hope I made the question clearer.

推荐答案

FastList 究竟是什么?

如果您要项目拥有列表集合的属性。

If you want Items to have a property of Lists collection.

您必须重新定义您的领域模型。类似这样的东西。

You have to redefine your Realm model. something like this.

class Car:Object{
    dynamic var createDate: NSDate = NSDate()
}
class Person:Object{
    let cars = List<Car>()
}

并按此类的谓词查询

let realm = Realm()
var ownedCarsFilterByDate = realm.objects(Person).filter("ANY cars.createDate = '\(date)'")






已编辑为更新问题


Edited to updated question

您的解决方案是创建表类

Your solution is to create table class, which has 'Person' , 'Car' and 'Context Attribute'.

您的模型将是这样

class PersonAndCarRelation:Object{

    dynamic var person: Person?
    dynamic var car: Car?
    dynamic var contextAttribute = ""

}

可以查询与人相关的所有汽车

and you can query all cars associated with person

    let personID = "123456789"
    let personAndCarArray = realm.objects(PersonAndCarRelation).filter("person.id == \(personID)")
    for personAndCar in personAndCarArray{
        let personName = personAndCar.person.name
        let carName = personAndCar.car.name
        let context = personAndCar.contextAttribute
        println("I am \(personName). I have a \(carName) with \(context)")
    }

这篇关于Realm查询对象属性字段由它的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆