2

I have two Core data entities named Patients and Recordings. A patient can have a single or multiple recordings. I want when a view load i can fetch information of both the entities attributes in a single fetch like we did in sql using join. Any help?

Core Data Model Image Search Patient

A patient can have a single recording dictation or multiple. Now on view load i show all the patients from core data patients entity. I want to show images in table cell that if patient has recording done it shows recording icon or if has transcription done it has transcription icon as well and none of the icon shows if patient has no recording.i want to get datails of both the table in a single fetch.

func FetchSearchData() {

        let uid = defaults.value(forKey: "UserID")
        searchTasks.removeAll()

        let fetchRequest:NSFetchRequest<Patients> = Patients.fetchRequest()
        let sortDescriptor = NSSortDescriptor(key: "dateSchedule", ascending: true)
        fetchRequest.sortDescriptors = [sortDescriptor]
        let predicate = NSPredicate(format: "(userID=%@)", uid as! CVarArg)
        fetchRequest.predicate = predicate

        do {
            let count = try getContext().count(for: fetchRequest as! NSFetchRequest<NSFetchRequestResult>)

            if count > 0 {

                let fetchResult = try getContext().fetch(fetchRequest)

                for item in fetchResult {
                    searchTasks.append(item)
                    searchTableView.reloadData()
                }
            } else {
                searchTableView.reloadData()
            }

        }catch {
            print(error.localizedDescription)
        }

    }

I have create relationship like we see in image. How can I check now which patient have recording exists?

3
  • I'm not a Core Data expert but what have you tried so far? What failed? What does your Core Data model look like? What fetch request are you trying? Commented Apr 6, 2017 at 15:20
  • 1
    can you at least pseudo code the fetch you want to do Commented Apr 6, 2017 at 15:43
  • stackoverflow.com/help/how-to-ask Commented Apr 6, 2017 at 15:45

1 Answer 1

2

With Core Data you fetch only one entity type at a time. Core Data isn't SQL.

The Core Data style approach to this would be something like: Fetch instances of Patients. Then use the sounds relationship on each Patients instance to get information about which recordings exist. For example, if you want to know if a specific patient has recordings, look at the sounds relationship-- which is a set-- and see if it's empty.

Sign up to request clarification or add additional context in comments.

1 Comment

@Robotic Cat thanks for the edited question. I will surely check the rules about asking questions next time i come here. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.