简体   繁体   中英

How to access the realm model list in filter in swift 4?

This is my object/model

import RealmSwift
class IBPChapters: Object {

    @objc dynamic var ibp_chapter_id = 0
    @objc dynamic var chapter = ""
    @objc dynamic var chapter_president = ""
    @objc dynamic var office_no = ""
    @objc dynamic var email_address = ""
    @objc dynamic var services_offered = ""
    var service = List<Services>()
    @objc dynamic var locid = 0
    @objc dynamic var loc:Locations?
    @objc dynamic var status = 0

    override static func primaryKey() -> String {
        return "ibp_chapter_id"
    }
}

Services Model

import RealmSwift
class Services: Object{

    @objc dynamic var service_id = 0
    @objc dynamic var service = ""
    @objc dynamic var status = 0
    @objc dynamic var ibp_service_filter = true
    @objc dynamic var pao_service_filter = true
    @objc dynamic var lac_service_filter = true

    override static func primaryKey() -> String {
        return "service_id"
    }
}

Example Data

1. legal aid,outreach,counseling,case handling
2. legal aid,counseling
3. outreach,counseling

Appending Data

if chapter.service[0].ibp_service_filter == true , append all data having legal aid

Output will be 1 and 2 //Expected OUTPUT

if chapter.service[1].ibp_service_filter == true , append all data having counseling but if the id is already exist ignore that index and continue to the next data

Output will be 1 and 2 and 3 //Expected OUTPUT

func addData(){

    var ibpArray:[IBPChapters] = []

    let chapters = realm.objects(IBPChapters.self)//condition for filter here
    for chapter in chapters{
        print(chapter.service)
        ibpArray.append(chapter)

        //Legal Aid service[0]
        if chapter.service[0].ibp_service_filter == true{
            ibpArray.append(chapter)
        }
        //Counseling service[1]
        if chapter.service[1].ibp_service_filter == true{
            ibpArray.append(chapter)
        }
        //Outreach service[2]
        if chapter.service[2].ibp_service_filter == true{
            ibpArray.append(chapter)
        }
        //Case Handling service[3]
        if chapter.service[3].ibp_service_filter == true{
            ibpArray.append(chapter)
        }
    }
}

MY OUTPUT 1,2,3,1,2,3,1,2,3,1,2,3 //<- wrong output

only 1 and 2 has legal aid , and all of them 1,2 and 3 has counseling so I dont need to append 1 and 2 again since I already appended them on the array on the first loop

How can I access the list on the realm for my filter, so I can manipulate the data or Do you have any idea or alternative way to generate my expected output

Can I use array index to filter? ex. .filter("services[0].ibp.filtered == true)

Correct Output

func addData(){

    let chapters = realm.objects(IBPChapters.self)
    for chapter in chapters{
        let filteredServices = chapter.service.filter("ibp_service_filter == true")
        for service in filteredServices{
            let service_index = filteredServices.index(of: service)
            if chapter.service.contains(filteredServices[service_index!]){
                if !ibpArray.contains(where: {$0.ibp_chapter_id == chapter.ibp_chapter_id}){
                    ibpArray.append(chapter)
                }
            }
        }
    }
}

but I still want to know if there is a much better answer or shorter than my answer or lets say less loops and conditions

Realm support .filter() function based on NSPredicate . So, first you should get ibp_service_filter == true values:

let filteredServices = chapter.service.filter("ibp_service_filter == true")

and then save only uniq ids ( basic idea ):

for service in filteredServices where !history_ibp.contains(where: { $0. service_id == service.service_id }) {
    history_ibp.append(service)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM