2017-12-27 54 views
0

Contcacts 및 Categories를 저장하는 세 가지 모델이 있습니다. 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 

} 

는 지금은 특별한 범주에 존재하는 모든 연락처를 얻으려면 :

카테고리 :

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

연락처로 . 나는 그것이 영역 DB에서 가능한지 아닌지 모른다. 어떻게 신속하게 realm db에서 할 수 있습니까?

답변

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 


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 

} 
를셨습니까로 데이터를 가져