ios - Compound predicate not returning the results -



ios - Compound predicate not returning the results -

i'm stuck problem while can't seem think of way right. i'll seek explain clear can.

there 3 entities in core info model. workplace, patient , handover.

a workplace can have multiple patients. patient can belong multiple workplaces.

a patient can have 1 handover , vice versa.

in app, list of workplaces shown user. when user selects workplace, need set patients belong selected workplace , have handovers today. since patient can have multiple handovers, there can duplicate records patients , that's okay.

this i'm doing now. first retrieve workplace object user selected. iterate through it's patients , extract ids of patient objects , collect them in array. pass array of patient ids , date filter out patients have handovers given date.

let workplace = db.loadworkplace(155) // 155 id of workplace var patientids: [int] = [] p in workplace.patients { allow patient = p patient patientids.append(patient.id) } handovers = db.loadhandovers(patientids, date: nsdate.date())

this method des filtering.

public func loadhandovers(patients: [int], date: nsdate) -> [anyobject] { allow fetchrequest = nsfetchrequest() allow entitydescription = nsentitydescription.entityforname("handover", inmanagedobjectcontext: managedobjectcontext!) allow patientpredicate = nspredicate(format: "patient.id in %@", patients) allow datepredicate = nspredicate(format: "date > %@ , date < %@", getstartdate(date), getenddate(date)) allow compoundpredicate = nscompoundpredicate(type: .andpredicatetype, subpredicates: [patientpredicate, datepredicate]) fetchrequest.entity = entitydescription fetchrequest.predicate = compoundpredicate var error: nserror? allow result = managedobjectcontext?.executefetchrequest(fetchrequest, error: &error) homecoming result! }

the getstartdate() , getenddate() methods convert nsdate object , start time , end time date frame. have them used in other places , work. here's detailed explanation it.

anyway loadhandovers() method returns 0 results. cannot because when insert data, can see handovers today. below sql query executed core data.

select 0, t0.z_pk, t0.z_opt, t0.zdate, t0.zsignedby, t0.zstatus, t0.zpatient zhandover t0 bring together zpatient t1 on t0.zpatient = t1.z_pk ( t1.zid in (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) , ( t0.zdate > ? , t0.zdate < ?))

can please tell me if wrong predicates? or if there different ways approach altogether? i'd appreciate it.

thank you.

using object graph, seems simple. seek avoid verbose fetch requests.

workplace.patients.filteredsetusingpredicate( nspredicate(format: "handovers.date > %@ && handovers.date < %@", startofday, endofday))

you seem bit confused own setup. if patient has 1 handover, why phone call relationship handovers in plural?

another flaw setup illustrated contradictory statement

"since patient can have multiple handovers..."

in info model, patient can have one handover, not many. explanation maintaining multiple patient instances same patient, because patient has more 1 handover. illogical , partial reason confusion , error encounter.

a improve info structure, avoiding duplicate patients:

workplace <<---->> patient <---->> handover

this assumes handovers have nil hospital. if do, should utilize handover entity kind of bring together table suggested @dank:

workplace <---->> handover <<----> patient

if want, application of predicate shorter:

workplace.handovers.filteredsetusingpredicate( nspredicate(format: "date > %@ && date < %@", startofday, endofday))

ios sql core-data nsfetchrequest nscompoundpredicate

Comments

Popular posts from this blog

formatting - SAS SQL Datepart function returning odd values -

c++ - Apple Mach-O Linker Error(Duplicate Symbols For Architecture armv7) -

php - Yii 2: Unable to find a class into the extension 'yii2-admin' -