Mastering In CoreData (Part 9 NSFetchRequest)

Core Data
Figure 1

Explanation

Getting Started

Fetching NSManagedObject instances

Figure 2
  • Refer to persistentContainer from appdelegate singleton object
  • Create/Access the singleton managed object context from persistentContainer
  • Created a fetch request to filter only NSManagedObject having entity name User. This example does not add any requirements to that data other than the type of entity being returned
  • You handed the fetch request to the managed object context to do the heavy lifting. fetch(_:) returns an array of managed objects meeting the criteria specified by the fetch request. Method has two possible results. It either returns an NSArray object of type NSManagedObject with zero or more objects, or it throw an error , you have received an error from Core Data and need to respond to it
Figure 3

Filtering

  1. Firstly we added two users
  2. Save objects into the persistent store by calling save method
Figure 4
Figure 5

Sorting

Figure 6
  1. First, we created two sort Descriptors and the highest priority was given firstName property of User after that secondName will considered
  2. Printed in the console and the result now sorted as shown in the console of Figure 7
Figure 7

Optimization

Which Persistent Store to Search

[request setAffectedStores:[NSArray arrayWithObjects:firstStore,secondStore,thirdStore, nil]];
  1. When your application have multiple persistent stores and you don’t specify affectedStores property, it will look from all the persistent stores
  2. If you specify this property it will look only these stores
Figure 8

Result type of the Fetch Request

  1. managedObjectResultType (Default )
  2. dictionaryResultType. (Return array of dictionary)
  3. countResultType. (Return only count )
  4. managedObjectIDResultType (Return only objectIds )
  1. First we created a fetch request with array type User
  2. Second we defines the result type with “managedObjectResultType” and it is a default type, which we expect from fetchrequest (make sure step 1 and step 2 should be consistent otherwise app will crash).Majority of the case we needed this type that’s why it is the default one
  3. Printed User object as you can see User whole objects is printed on the console in Figure 9
Figure 9
  1. First we created a fetch request with array type NSDictionary
  2. Second we defines the result type with “dictionaryResultType” , which we expect from fetchrequest (make sure step 1 and step 2 should be consistent otherwise app will crash).
  3. Printed User details using their property which acts as a key in Dictionary as shown in Figure 10
Figure 10
  1. First we created a fetch request with array type NSNumber
  2. Second we defines the result type with “countResultType” , which we expect from fetchrequest (make sure step 1 and step 2 should be consistent otherwise app will crash).
  3. Printed User users count in the database as shown in Figure 10. As you can see sometimes we need total count only instead of loading whole objects in the memory we can used this which surely reduces memory consumption
Figure 11
  1. First we created a fetch request with array type NSManagedObjectID
  2. Second we defines the result type with “managedObjectIDResultType” , which we expect from fetchrequest (make sure step 1 and step 2 should be consistent otherwise app will crash).
  3. Printed User Object Id which is unique for every record in the context. The question that might be arised in your mind what is NSManagedObjectID we will look into this when we will be doing multiple managedObjectContext or in threading part as well . It’s not very helpful when we have only one NSManagedObjectContext. At this moment if you don’t understand anything about it it’s OK but one think you should take is that we can return NSMangedObjectId in fetch request as well by changing it’s resultType to NSManagedObjectID. It will surely optimize the performance when we need to transfer data between two contexts.
Figure 12

Summary

What Next?

Useful Links

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store