领域查询对象属性字段按其属性 [英] Realm query Object property field by its property

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

问题描述

我正在使用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 and now I have another.

假设我们有一个这样的模式:

Suppose we have a schema like this:

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

class Car: Object {
    dynamic var name: String?

我有一个类(人),其中包含任意数量的另一类(车)中的对象.与该人链接"的汽车在该人的上下文中具有某些属性(对于不同的人,同一辆汽车或对于一个人的两辆相似的汽车,它们可能是不同的).使用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 can 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用于一个人,并且只有一次,我们可以创建另一个仅包含Cars附加属性的类,并将它们与Person ID加上Car 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.

所以,我如何看待解决方案.有一个表(类)存储Person的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?

推荐答案

FastList到底是什么?

如果希望Items具有Lists集合的属性.

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

您必须重新定义Realm模型.像这样的东西.

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)'")


编辑为更新的问题

您的解决方案是创建具有'Person','Car'和'Context Attribute'的表类.

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)")
    }

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

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