Ios core data filter by date

In particular we can get Core Data to show us the underlying SQL queries it is using by setting the argument -com.

More Fetching

SQLDebug to 1 on application launch. With Xcode 4 the launch arguments are set in the project scheme window:. With the debugging enabled we can see the query that Core Data is using and the fetch execution time which will be useful when comparing the performance of different approaches:. The debugging output shows us that Core Data is performing a select to retrieve all of the attributes of the task object, ordered by creation data with a query limit of 1.

The total fetch execution time in this case was 0. Note that any time you are looking to optimise performance it is a good advice to run the code on an actual device. Running on the iOS Simulator will give you much faster performance due to the obviously greater performance of the host computer.

Examples of using NSPredicate to filter NSFetchRequest

Before looking at the use of expressions there is one minor optimisation that we could consider applying to the previous fetch request. Since we are interested only in the creation date we can modify the fetch request to make it only retrieve that one property:. By default the result type of a fetch request is NSManagedObjectResultType which as the name implies means we will get managed objects back from the fetch. To specify that we want one or more properties of an object you first need to make the fetch request return a dictionary by setting the result type to NSDictionaryResultType and then you set the properties to fetch by passing it an array containing the names of the properties you want back.

Now when we execute the fetch request we get back an array containing a single dictionary since we set a fetch limit of 1 which contains the single property which in this case is an NSDate:. Looking at the SQL debug you can see that the select statement now only retrieves the single attribute.

There is a performance improvement in that this fetch executes in around 0. However this may often turn out to be a false optimisation if after determining the earliest creation date we then shortly afterwards find we want to retrieve the actual task with this creation date. In that case it is generally better to just retrieve the full task as in the original query so that Core Data already has it cached ready for when we need it.

A better way to solve this type of query is actually to create an expression with the function that we want to perform.

Core Data Queries Using Expressions

Unfortunately there is a little bit more code required though we start as with the previous example by constructing a fetch request that will return a dictionary result:. We then create an expression which specifies the function we want to use and the key-path of the property we want to apply it to.

So for our example where we want the minimum of the createdAt property:. There are a wide range of functions that we could apply including average: Unfortunately that is not all we need to do as we must also create an expression description to specify the result type we are expecting from the fetch request:. The key point is that we need to set a name for the expression which we will use when retrieving the result - remember that we have already specified that the fetch request should give us back a dictionary containing the result.

Core Data Queries Using Expressions

The name of the expression will be our key into that dictionary. It either returns an NSArray object with zero or more objects, or it returns nil.

If nil is returned, you have received an error from Core Data and need to respond to it. If the array exists, you receive possible results for the request even though the NSArray may be empty.

iOS Tutorial SearchBar in TableView - Filter Your Array of Model Objects in Swift 3

An empty NSArray indicates that there were no records found. The real flexibility in fetching objects comes in the complexity of the fetch request. To begin with, you can add an NSPredicate object to the fetch request to narrow the number of objects being returned.

Contact us

Let's say that we have attribute called 'Colors' with values as:. Colors have a relationship with other entities so user can add many values with red color or green color.

  • Cleaning up Core Data Fetch Requests.
  • poker online para blackberry 8520.
  • night light pro symbian belle;
  • ios 9 features for iphone 4s.
  • e sword download for android;
  • How to Delete a Record From a Persistent Store?
  • More Fetching and Deleting Managed Objects With Core Data.

For example:. How to group Colors and show only those values that has been added? Well, we will need NSFetchRequest. Let's start with the easy one. Our code will look like this:. This part lets us to sort Fruits - by date, but of course we can use another value. We can also add more sort descriptors. Then, we move to Colors: