Sharlon M. Balbalosa
Sharlon M. Balbalosa

Reputation: 1309

Searching for identical NSManagedObject in CoreData

I am newbie in coredata and I want to know how tell if an object is identical and already exist lets say for example I save a News model(NSManagedObject) with a title and content and save it, later I have instantiated another News model with the same title and content is there a way to tell that this object already exist? Thanks in advance!

Upvotes: 1

Views: 177

Answers (1)

Jeremy
Jeremy

Reputation: 9030

To find another News model with the same title and content, you need to first query using a compare on both content and title before instantiating another News model.

Here's what your NSPredicate might look like:

NSString *newContent = @"SomeNewContentValue";
NSString *newTitle = @"SomeNewTitleValue";

NSPredicate *newsFilter = [NSPredicate predicateWithFormat:@"title==[cd]%@ AND content==[cd]%@", newTitle, newContent];

Observe the [cd] string options which make your comparison both case and diacritic insensitive. This, of course, assumes you need it to be case-insensitive. Otherwise, leave out the [cd] in either spot.

Upvotes: 4

Related Questions