在 swift 中连接并加入 Realm

junction and join in Realm in swift

我有三个模型来保存联系人和类别。和一个联结 table 以将不同类别中的联系人保存为类别联系人。

类别:

import UIKit
import Foundation
import RealmSwift

class Category: Object, IEntity
{


override class func primaryKey() -> String? {
    return "CategoryId"
}

static func KeyName() -> String
{
    return primaryKey()!
}

 dynamic var    CategoryId: Int64 = 0
 dynamic var    Name = ""
dynamic var    AvatarName = ""
 dynamic var    CreationDateTime = ""
dynamic var    LocalContactCount: Int32 = 0
dynamic var    ServerContactCount: Int32 = 0
dynamic var    UserId: Int64 = 0
}

联系人为:

import UIKit
import Foundation
import RealmSwift

class CustomerClubContact: Object, IEntity
{

override class func primaryKey() -> String? {
    return "CustomerClubContactId"
}

static func KeyName() -> String
{
    return primaryKey()!
}

 dynamic var    CustomerClubContactId: Int64 = 0
 dynamic var    Prefix = ""
 dynamic var    FirstName = ""
 dynamic var    LastName = ""
 dynamic var    Mobile = ""
 dynamic var    BirthDay = ""
 dynamic var    AvatarName = ""
 dynamic var    UserId: Int64 = 0
 dynamic var    ErrorMessage = ""
 dynamic var    IsMembershipCanceled = false
 dynamic var    IsDeleted = false

 }

并且交汇点 table 是:

import UIKit
import Foundation
import RealmSwift

class CategoryContacts: Object, IEntity
{


override class func primaryKey() -> String? {
    return "Id"
}

static func KeyName() -> String
{
    return primaryKey()!
}

dynamic var    Id: Int64 = 0
dynamic var    CategoryId: Int64 = 0
dynamic var    CustomerClubContactId: Int64 = 0

}

现在我想获取特殊类别中的所有联系人。我不知道在 realm db 中是否可行?我怎样才能在 swift

的领域数据库中做到这一点

我找到了解决方案。 给以后遇到的人

import UIKit
import Foundation
import RealmSwift

class CustomerClubContact: Object, IEntity
{

override class func primaryKey() -> String? {
    return "CustomerClubContactId"
}

static func KeyName() -> String
{
    return primaryKey()!
}

 dynamic var    CustomerClubContactId: Int64 = 0
 dynamic var    Prefix = ""
 dynamic var    FirstName = ""
 dynamic var    LastName = ""
 dynamic var    Mobile = ""
 dynamic var    BirthDay = ""
 dynamic var    AvatarName = ""
 dynamic var    UserId: Int64 = 0
 dynamic var    ErrorMessage = ""
 dynamic var    IsMembershipCanceled = false
 dynamic var    IsDeleted = false


 let CategoryContactList = List<CategoryContacts>()


}

import UIKit
import Foundation
import RealmSwift

class Category: Object, IEntity
{


override class func primaryKey() -> String? {
    return "CategoryId"
}

static func KeyName() -> String
{
    return primaryKey()!
}

dynamic var    CategoryId: Int64 = 0
dynamic var    Name = ""
dynamic var    AvatarName = ""
dynamic var    CreationDateTime = ""
dynamic var    LocalContactCount: Int32 = 0
dynamic var    ServerContactCount: Int32 = 0
dynamic var    UserId: Int64 = 0

let CategoryContactList = List<CategoryContacts>()

}




import UIKit
import Foundation
import RealmSwift

class CategoryContacts: Object, IEntity
{
override class func primaryKey() -> String? {
    return "Id"
}

static func KeyName() -> String
{
    return primaryKey()!
}

dynamic var    Id: Int64 = 0
dynamic var    CategoryId: Int64 = 0
dynamic var    CustomerClubContactId: Int64 = 0

}

然后插入

    let c1 = Category()
    c1.CategoryId = 1
    c1.Name = "category 1"
    categoryBiz.insert(item: c1)

    let c2 = Category()
    c2.CategoryId = 2
    c2.Name = "category 2"
    categoryBiz.insert(item: c2)



    let cat1 = CategoryContacts()
    cat1.Id = 100
    cat1.CategoryId = 1
    cat1.CustomerClubContactId = 10
    //categoryContactBiz.insert(item: cat1)

    let cat2 = CategoryContacts()
    cat2.Id = 101
    cat2.CategoryId = 2
    cat2.CustomerClubContactId = 11
    //categoryContactBiz.insert(item: cat1)



    let con1 = CustomerClubContact()
    con1.CustomerClubContactId = 10
    con1.FirstName = "f1"
    con1.LastName = "l2"
    con1.CategoryContactList.append(cat1)
    contactBiz.insert(item: con1)

    let con2 = CustomerClubContact()
    con2.CustomerClubContactId = 11
    con2.FirstName = "f2"
    con2.LastName = "l2"
    con2.CategoryContactList.append(cat2)
    contactBiz.insert(item: con2)

并通过连接获取数据

    func FetchAllEligibleWithCategoryId(categoryId: Int64)-> Results<RealmEntityType>?
{
    do
    {

        let object = realm.objects(CustomerClubContact.self).filter(" any CategoryContactList.CategoryId == \(categoryId) AND IsDeleted = \(false) And IsMembershipCanceled = \(false) ")
        return object

    }
    catch
    {
        print(error.localizedDescription)

    }
    return nil

}