联结并加入Realm in swift
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了联结并加入Realm in swift相关的知识,希望对你有一定的参考价值。
我有三个模型来保存联系人和类别。以及将联系人保存在不同类别中作为类别联系人的联结表。
分类:
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
}
和联结表是:
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
}
现在我希望得到一个特殊类别中的所有联系人。我不知道是否有可能在领域db?我如何在swift中的realm db中执行此操作
答案
我找到了解决方案。对于那些将来遇到的人
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)
并使用join我的数据来获取数据
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
}
以上是关于联结并加入Realm in swift的主要内容,如果未能解决你的问题,请参考以下文章