0

I’m working on an dictionary app. this app using core data and I'm very new to core data. In the database, records stored with a single entity called dictionary and dictionary entity has just two attributes “English” & “Meaning”. So whenever user types some char say a, in UISearchBar, I want to fetch records from data base that starts from “a”, if user types data a than b that I want to fetch records starts from “ab” and so on.

I’m trying same thing by below code:

// Fetch the devices from persistent data store

NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:ENTITY_NAME inManagedObjectContext:managedObjectContext];
[request setEntity:entity];
// retrive the objects with a given value for a certain property
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K beginswith[c] %@",@"English",searchText];
[request setPredicate:predicate];

// Edit the sort key as appropriate.
/*NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"English" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[request setSortDescriptors:sortDescriptors];*/

// Edit the section name key path and cache name if appropriate.
// nil for section name key path means "no sections".
/*NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:managedObjectContext sectionNameKeyPath:nil cacheName:@"Root"];
aFetchedResultsController.delegate = self;*/

NSError *error = nil;
_searchResult = [managedObjectContext executeFetchRequest:request error:&error];

when this code executes my app crashes with the following error msg:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'keypath English not found in entity <NSSQLEntity TamilEnglishDict id=1>'

EDIT:

this is how my Data Model is...

Data Model

Any who can guide me to the right path of fetching records properly?

18
  • Can you say something about the model? In your entity there is no attribute English. Commented Jan 10, 2014 at 8:49
  • @flexaddicted attribute "English" is there. As far as my model is concern I try to explain it.. I had some xml files, I parse those files and created a data base. Directly in the managedObject I setValue with two key "English" and "Meaning". that's how I created database and fetching records the way I explained in my question. Commented Jan 10, 2014 at 8:56
  • Show me the model. What is ENTITY_NAME? Provide a screenshot if possible. Commented Jan 10, 2014 at 8:58
  • Is ENTITY_NAME the same as dictionary in your question? Commented Jan 10, 2014 at 9:01
  • I would like to see the model, if possible. Commented Jan 10, 2014 at 9:03

2 Answers 2

3

Yes it's case sensitive.

So the correct predicate is the following.

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K beginswith[c] %@",@"english", searchText];

or just

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"english beginswith[c] %@", searchText];

but while saving those values in managedObject I wrote '[managedObject setValue:[obj objectForKey:@"English"] forKey:@"English"]';

So now edit your question and explain me the comment above. What do you mean?

Maybe you could just do

[managedObject setValue:[obj objectForKey:@"English"] forKey:@"english"];
Sign up to request clarification or add additional context in comments.

3 Comments

thanks for bearing with me so patiently. While I was creating database from XML files that is how I stored data in managedObject and save the context.
@SuryakantSharma Do you have any other problems? If not, please mark the answer as correct. Cheers.
I'll do it for sure because it is indeed a correct ans. thanks
0

I saw your code & your code is correct, you just have to change Predicate you are using. just replace this code with your predicate & hope your problem is solve. because you want result that conatain "ab" or "a" or "b" so you have to use "contain" in place of "beginswith".

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K contains[cd] %@",@"English", searchText];

Let me know if you have any issue.

3 Comments

I used contains as well but it doest work at all. when I use contains, my app crashes with error *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'unimplemented SQL generation for predicate : (English CONTAINS[cd] "w")'
English will be your column name & search text will be your search string
Why changing from BEGINSWITH to CONTAINS will resolve the problem? That's not true. In addition CONTAINS is more expensive than the first.

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.