Pages

Showing posts with label Core Data. Show all posts
Showing posts with label Core Data. Show all posts

Core Data advanced queries with NSExpression Class

Fetching data from Core Data is very necessary && interesting to do because any application only storing the data  is only an overhead to the memory because all it can do is just to store the data in the memory but can't delete from it, can't fetch according to you expectations && eventually your application will crash due to shortage of memory. By using NSExpression class you can get data from database in any format you want. For example if you want to fetch the entries in such a manner which are entered in particular interval. You can do this by using NSExpression class to manually build predicates. A predicate contains two or more expressions and one or more comparator. Let us see through an example, you want to extract entries from database corresponding to attribute age in which age in between 20 to 30.

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.

Some Important commands of Core Data

Viewing the Core Data Database on the Terminal:

    Well guys as we create our Core Data Model in our projects that supports Core Data && then storing the data into Core Data, there should be a question in your mind, where is our Data in Core Data && How to view it.
    As you know that an Entity that you create in your Data Model of your Application, behaves like a Class which is a subclass of "NSObject" && this entity can be seen like a table in the actual Database. As you know that Core Data at back end uses Sqlite database. So how to view the data that your application create && store in the database.....Here is an answer.....some commands that will be helpful.

1. finding the database on the terminal with an extension "(.sqlite)" that is created && stored in your app's document directory.

------->>>>   find ~ -name '<database name>.sqlite' ;   <<<<-------

Transformable && Binary Data Properties in Core Data

Using Transformable && Binary Data properties in Core Data:

    As you know Core Data attributes can have properties like Undefined, Integer16, Integer32, Integer64, Float, Decimal, Date, Boolean, String, Double, Binary Data in Objective C has to offer. Out of all of these properties Binary Data is must to be considered.    
    With the support of all of these properties Swift language is also providing you Transformable property which is very Interestingly Important. Because of this property you can put any type of data into the Core Data without extra overhead. In this tutorial I am going to show you how to use both of these properties...(Binary Data && Transformable property).

1. As usual create a singleView template application && name it "ImportantDataTypeInCoreData" && choose language as swift Leaving Core Data checkbox Unchecked we will Insert our Core Data model later. as shown in Image below:

Core Data

Overview of Core Data: 
    Core Data feels like a database but is not technically a database. It is a Object Graph that usually stores its data in SQLite database. It behaves like a database & can create, store & retrieve objects that have attributes & relationships to other objects. Basic advantage of Core Data is that it insulates the User from the underlying SQL, thats what SQLite basically called.

         Core Data uses many different classes to make its job done. These classes are divided into two groups, first group of classes is that set up once, when the App launches, these are:

NSPersistentStore

NSPersistentStoreCoordinator
NSManagedObjectModel