I have a managed object reference *event.
An Event has many Occurrences. An Occurrence has an attribute call date
I want to fetch all the occurrences of the event *event which has today’s date. How should I write a NSPredicate on a FetchRequest.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You have to be aware, that there are two ways to get your Occurrence objects: You can traverse the relationship from the Event object (i.e.
myEvent.occurences) or you can create a fetch request for all occurrences that have the event of interest in their inverse relationship, i.e. a predicate like[NSPredicate predicateWithFormat:@"event == %@", myEvent"].There are fundamental differences to these two. A fetch request will always cause SQL to be executed and I/O to happen which is expensive. If you simply traverse the relationship, and you’re in a situation where the Occurrence objects are already in memory, it will be very cheap to do (no SQL, no I/O). The fetch request on the other hand, will allow you to fault in all objects in one go, while traversing will only fault objects one at a time, so you’ll end up doing multiple SQL + I/O events. Also, a fetch request allows you to limit the predicate even further. But in many cases, simply traversing might be the best case.
CoreData does a lot of heavy lifting for you, but at the end of the day, you still need to know about when to do SQL and I/O and how to prevent it.
I hope this helps.