Pages

NSPredicates && NSSortDescriptors in Core Data

Fetching Data with advance Queries from Core Data:


   As you know that when you fetch data from sqlite database with NSFetchRequest() function you get the data in the order as you store it in the Core Data. But what if you want to fetch data in different order according to the different circumstances. 
    There are so many ways you can do it some of them are by using NSPredicate, NSSortDescriptor classes && the functions provided by them. First let us have example of NSPredicate Class && its functions.

Consider for example you want to extract words from Core Data for key 'location' whose 1st letter is 'B' && 'I' you fetch by using following predicate queries && attaching them to the fetch request that you gonna make....here is the figure below, the example I am considering:



Here is the fetch Request instance with which we are going to make queries:

--->>>let fetchRequest = NSFetchRequest(entityName: "NewAddedAnnotation")

   We are only creating a fetchRequest variable of type "NSFetchRequest" for entity/table "NewAddedAnnotation" for example. Now create predicate queries:

--->>>let predicate1 = NSPredicate(format: "location BEGINSWITH 'B'", argumentArray: [])
        let predicate2 = NSPredicate(format: "location BEGINSWITH 'I'", argumentArray: [])   <<<<-------

 ** Now combining these two predicates into one: **

--->>>  let predicate = NSCompoundPredicate.andPredicateWithSubpredicates([predicate1, predicate2])

Lets us now combine the predicate with our fetch request:


----->>>>>  fetchRequest.predicate = predicate

Here left hand side "predicate" is predefined predicate that our fetchRequest variable of type "NSFetchRequest" is using && right hand side "predicate" is our created composite variable. As usual now execute the fetchRequest && you will get the result but in my case the result is as shown in figure:



   And since I don't have any any location entry with letter 'C' in the begining so it is not shown in the figure.

****---->>>> Let us now have example of "NSSortDescriptor" Class:

    Assume for example you want to sort the location entires in ascending or in descending order how do you do it....Here is the query...:

                                           //In ascending order
--->>>let sort = NSSortDescriptor(key: "location", ascending: true)
                                or
--->>>let sort = NSSortDescriptor(key: "location", ascending: false)
              //In standard order as you stored data in Core Data

Now pass this sort descriptor you created to the fetchRequest you gonna make:

---->>>>  fetchRequest.sortDescriptors = [sort]   <<<<----

In my example the result of this combined sortDescriptor query by "executingFetchRequest" is:


As you can see in the Figure that all the entries are sorted order compare to the very first figure in this tutorial. You can play with these queries in a lot more ways, as usual I will let you handle you all those ways....

I hope you enjoyed this post guys.....


No comments:

Post a Comment