简体   繁体   中英

How do I fetch objects from Core Data that do not have any relatives that meet a set of criteria?

I'm new to Core Data and having a little trouble understanding the best way to fetch data efficiently, particularly with entities that are related.

Imagine that I have two entities: Patients and Appointments. Patients have many Appointments.

I want to fetch all the patients that haven't had an appointment this [Patient.appointment_frequency], where appointment_frequency is weekly, monthly, etc.

How would I do that, particularly in a way that's fast with hundreds or thousands of Patient objects and hundreds of appointments per patient?

First, you'd predicate your fetch request for appointments within the variable appointment threshold. Your returned fetched set will contain each of the matching appointment objects, you can then build a patient set by asking each of the returned appointments for it's patient, and then grouping the appointments by patient (ie, to present in a tableview).

If the result set contains many objects (hundreds, thousands), CoreData will manage populating and faulting objects in the result set, so don't concern yourself with the memory or performance unless you're using it and finding performance less than expected.

Apple has provided a core data programming guide with sample code. It explains how to make different kinds of fetch requests and is very clearly written.

In your situation, I'd take the current date and subtract a week (or month, or whatever the frequency says). Fetch a request for all patients using a predicate that says you want all users with frequency X whose last appointment < the calculated date. The returned users need to schedule appointments.

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