nspredicate

iPhone Development - Using NSPredicate to retrieve most recently added record from CoreData

I want to retrieve the most recently added record from CoreData. I was wondering if that's possible using NSPredicate? If so, how? e.g. i have a one-to-many relationship between Department and Staff, and i want to fetch the Staff record that was most recently employed. The Staff table has a date field which can be used. I don't want t...

NSPredicates, scopes and SearchDisplayController

Building a search with some custom objects and three scopes: All, Active, and Former. Got it working with the below code: - (void)filterContentForSearchText:(NSString*)searchText scope:(NSString *)scope { [[self filteredArtists] removeAllObjects]; for (HPArtist *artist in [self artistList]) { if ([scope isEqualToString:@"...

iPhone Development - Query related records using CoreData

I have a case where i have three entities with one-to-many and one-to-many relationships: Entity A (Entity B relationhip), Entity B (Entity A relationship, Entity C relationship), Entity C (Entity B relationhip) I have the reference of Entity A, and now i want to fetch all the related Entity C records. How can i do that? (with least ...

Core Data ANY BETWEEN predicate

I'm trying to create an NSPredicate to find 'projects' that contain 'sessions' within a certain date range. I tried this at first: [NSPredicate predicateWithFormat:@"ANY sessions.date BETWEEN {$STARTDATE, $ENDDATE}"]; But I get an exception: *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'to-ma...

Building a NSPredicate for a filter

Hi, Just wondering what the best way to build a NSPredicate is if some filters are optional? This is basically for a filter, so if some options aren't selected I don't to filter by them eg. If I have option1 and option2 set for the filter. NSPredicate* predicate = [NSPredicate predicateWithFormat:@"option1 = %@ AND option2 = %@] .......

Different results from array controller and fetch request for relationship with nil value

I'm using a simple NSComparisonPredicate similar to: ("employeeDepartment != %@", department) where employeeDepartment is an optional, one-to-many relationship in Employees, and department is an NSManagedObject of Departments. When applied against the Employees entity, I'm getting different results from an NSArrayController vs an NSFetch...

Setting a property value on each of the results in a FetchedResults set

Hi On my Core Data Entity "Book" i have a boolean property, 'wasViewed' (NSNumber numberWithBool) that tells me if the Book was "viewed". I would like to implement a sort of "reset" this property for all my NSManagedObjects "Book". So that I can set them all to NO between sessions. I use an NSPredicate to retrieve all the Books like th...

Core Data and NSDate

Hi ! I read this post but I don't really understand the code... I have a core data database with an Entity and some attributes. One of them is named "myDate" and has for type NSDate. Now I want to to display each date but eliminate dates with same day-month-year and display them ascendantly . Have you got an idea? Thanks a lot ! ...

How decompose NSPredicate into components?

Is there any common way to decompose an expression created by [NSPredicate predicateWithFormat] to objects NSComprasionPredicate, NSExpression and other? For below example need to disassemble into components. [NSPredicate predicateWithFormat:@"(0 != SUBQUERY(collection, $x, $x.name == "Name").@Count)"]; ...

NSPredicate that is the equivalent of SQL's LIKE

I'm looking for a way to use NSPredicate to set a LIKE condition to fetch objects. In addition to that, an OR would be useful as well. I'm trying to do something where if a user searches "James" I can write an NSPredicate that will do the equivalent of: select * from users where firstname LIKE '%James%' OR lastname LIKE '%James%'; ...

iPhone SDK Core Data: Fetch all entities with a nil relationship?

I have a core data project that has Books and Authors. In the data model Authors has a to-many relationship to Books and Books has a 1-1 relationship with Authors. I'm trying to pull all Books that do not have an Author. No matter how I try it, no results are returned. In my predicate I've also tried = NIL, == nil, == NIL. Any suggestion...

Core Data Predicates with Subclassed NSManagedObjects

I have an AUDIO class. This audio has a SOUND_A subclass and a SOUND_B subclass. This is all done correctly and is working fine. I have another model, lets call it PLAYLIST_JOIN, and this can contain (in the real world) SOUND_A's and SOUND_B's, so we give it a relationship of AUDIO and PLAYLIST. This all works in the app. The proble...

Cast an NSDictionary value while applying an NSPredicate?

Hi I have an Array of NSDictionary objects. These Dictionaries are parsed from a JSON file. All value objects in the NSDictionary are of type NSString, one key is called "distanceInMeters". I had planned on filtering these arrays using an NSPredicate, so I started out like this: NSPredicate *predicate = [NSPredicate predicateWithForm...

Core Data Many-to-Many Relationship NSPredicate

Hi all, I have a data model with a many-to-many relationship like EntityA <-->> EntityB <<--> EntityC. I used to query EntityA with different search criteria and I use NSCompoundPredicate with an array of NSPredicates. On one of the predicate I wanted to query EntityA using EntityC. I tried to use the following SUBQUERY but it did not w...

iPhone OS: Fetching a random entity instance using NSPredicate Nsfetchrequest and core data

Working on an app where I have a large collections of managed objects against which I want to fetch a few random instances. My question is, is there any way I can use NSPredicate and NSFetchRequest to return several objects at random. I saw that you could actually add a NSFetchRequest into the entity using the data modeler, any way to ...

Word boundaries (\b) in NSPredicate causing NSFetchRequest to return no managed objects.

Using Core Data w/a sqlite store on iPhone.... I've got a bunch of comic book image entities, each with a string that includes the comic's issue#, e.g.: image.imageTitle = @"Issue 12: Special Edition"; Part of the UI allows the user to type in an issue number to jump to the next issue. My initial code for this was sloooooooow because im...

comparying 2 array's with NSPredicate (cocoa)

i have an NSObject with 2 property's @interface Entity : NSObject { NSNumber *nid; NSString *title; } i have 2 array's with Entity's objects in it and I want to compare those two on the nid with a predicate array1: ({nid=1,title="test"},{nid=2,title="test2"}) array2: ({nid=2,title="test2"},{nid=3,title="test3"}) the 2...

AND NSPedicate on Core Data relationships

I'm having trouble compounding NSPredicate with AND, although using OR works fine. Imagine 2 entities, Doctor and Patient. Doctors can have many patients and patients many doctors. I want to find doctors that, say, have both person1 and person2 as patients. I expected this to work but it returns none. NSPredicate *predicate = [NSPre...

Uniquing with Existing Core Data Entities

I'm using Core Data to store a lot (1000s) of items. A pair of properties on each item are used to determine uniqueness, so when a new item comes in, I compare it against the existing items before inserting it. Since the incoming data is in the form of an RSS feed, there are often many duplicates, and the cost of the uniquing step is O(N...

Using NSPredicate to determine if a string equals another string.

I have a NSArray of CalEvents returned with the [CalCalendarStore eventPredicateWithStartDate] method. From the events returned, I am trying to keep only those in which the title of the event == @"on call" (case-insensitive). I am able to keep in the array those events whose title includes @"on call" with the following code (where event...