Skip to content

Importing large datasets in CoreData

Denys Telezhkin edited this page May 31, 2014 · 1 revision

The first article you should read on this topic before we proceed, is Apple's guide on Core Data import.

Seriously. Go read it, now =)

Ok, once you've read that, lets discuss, what EasyMapping does to improve CoreData imports.

EKCoreDataImporter

EKCoreDataImporter is a brand new class, introduced in 0.6.0 release. Following Apple recommendation, it implements efficient Find-Or-Create pattern. It starts it's work, when you call EKManagedObjectMapper methods like objectFromExternalRepresentation:withMapping:inManagedObjectContext: or arrayOfObjectsFromExternalRepresentation:withMapping:inManagedObjectContext:. And basically it does three things:

  • Collect all entity names from provided mapping
  • Introspect external JSON representation, that was passed to us, collect all primary keys for entities involved
  • Fetch all managed objects for single entity with one fetch request.

This allows us to decrease number of fetch requests to number of entities, that take part in a mapping. For example, you are creating array of Users from JSON. Existing users will be fetched with a single fetch request. This approach drastically improves fetch times when importing large datasets. And since we do this recursively, the more JSON we get, the bigger optimization will be.

How to win big with EasyMapping

The best way to put it, always try to use arrayOfObjectsFromExternalRepresentation:withMapping:inManagedObjectContext, if you have array of objects. If you try to use objectFromExternalRepresentation:withMapping:inManagedObjectContext:, you will lose optimization, that EasyMapping provides for you, at least large part of it. You will still get some, if object contains array of another objects, but it will be less effective.

Where to read about this stuff?

There's a great article on objc.io about CoreData imports.

There's also great architectural article on concurrent CoreData stack by Florian Kugler, you should definitely check it out.

Clone this wiki locally