Fetch records in Core data using predicate


In my earlier post on core data I have showed

  1. Core Data introduction 
  2. How to add data and retrieve data in a core data app 


In my this post I will show you how to use predicate and fetch data using the core data framework.

Note: This tutorial is for only those readers who have some basic idea about core data and even if you don’t then I will suggest you to read my earlier two post on core data and then come back to this post.

Design Phase: I will be using console this time as well to display the fetched data.

Step 1: In the previous project file of yours select the appdelegate.h file and add a function called

-(void)reteriveDataviaPredicate;


The body of the above function will contain the logic to fetch data as per the user's supplied query

For this post I will  have two queries and that’s

  1. Use the parent name (mother name) and get the child name.
  2. Use the child name and get the parent name.


Step 2: Before moving any further we must have a look at a class called as NSPredicate.

NSPredicate class description: The NSPredicate class is used to define logical conditions used to constrain a search either for a fetch or for in-memory filtering. You use predicates to represent logical conditions, used for describing objects in persistent stores and in-memory filtering of objects.you often create predicates from a format string which is parsed by the class methods on NSPredicate. Here’s a link to the predicate formats .

Step 3: Select the appdelegate.m file and add this piece of code to get the child name using the parent name

-(void)reteriveDataviaPredicate
{
NSFetchRequest *fetchReq = [[NSFetchRequest alloc]init];

//setting the predicate format so that we can get the child name by supplying the mother name


NSPredicate *query = [NSPredicate predicateWithFormat:@"Mothername='Chichi'"];

//setting the predicate to the fetch request 
[fetchReq setPredicate:query];

[fetchReq setEntity:[NSEntityDescription entityForName:@"Parent" inManagedObjectContext:self.managedObjectContext]];
NSMutableArray *resultArray = [[NSMutableArray alloc]initWithArray:[self.managedObjectContext executeFetchRequest:fetchReq error:nil]];
if([resultArray count]>0)
{
for(Parent *p in resultArray)
{
for(Child *c in p.ParentToChild)
{
NSLog(@"Child name is %@",c.Childname);
}
}
}
else
{
NSLog(@"No records");
}
}


Note: Don't forget to make a call to this function in the application did finish loading method

The above code will generate the output given below






Now if you want to fetch the parent name using the child name then in this case you can use this piece of code

-(void)reteriveDataviaPredicate
{
NSFetchRequest *fetchReq = [[NSFetchRequest alloc]init];

//setting the predicate format so that we can get the parent name by supplying the child name
NSPredicate *query = [NSPredicate predicateWithFormat:@"Childname='Radix'"];

//setting the predicate to the fetch request 
[fetchReq setPredicate:query];

[fetchReq setEntity:[NSEntityDescription entityForName:@"Child" inManagedObjectContext:self.managedObjectContext]];
NSMutableArray *resultArray = [[NSMutableArray alloc]initWithArray:[self.managedObjectContext executeFetchRequest:fetchReq error:nil]];

if([resultArray count]>0)
{
for(Child *c in resultArray)
{
NSLog(@"The Father name is %@",((Parent*)(c.ChildToParent)).Fathername);
NSLog(@"The mother name is %@",((Parent*)(c.ChildToParent)).Mothername);
}
}
else
{
NSLog(@"No records");
}
}


The above code will produce the output given below




Step 4: Lets say you make a typing mistake or try to fetch data which is not present in the sqlite file that was created in doc directory then in that case you will see the output given below (This is because of the if condition added in the above code)





Step 5: This was all for core data and if time permits i will also try to update the blog on some more advance code that can be done using this cool framework.

iHope this post helped you, your comments are always welcome as they help me a lot in updating myself as well as my blog, also you can request me a tutorial , until then Happy iCoding and have a great Day

Comments

  1. Very good tutorial, Really help me a Lot.....
    Arpit Parekh, Vadodara, Gujarat,India

    ReplyDelete
  2. Good Tutorial ! Best For Beginner

    ReplyDelete

Post a Comment