Getting Started with Core Data - Perficient Blogs
Blog
  • Topics
  • Industries
  • Partners

Explore

Topics

Industries

Partners

Getting Started with Core Data

Enabling persistent object mapping is a relatively straight-forward process. It differs from transient object mapping in only a few ways:

  1. libRKCoreData.a must be linked into your target
  2. Apple’s CoreData.framework must be linked to your target
  3. A Data Model Resource must be added to your target and configured within Xcode
  4. The RestKit Core Data headers must be imported via #import <RestKit/CoreData/CoreData.h>
  5. An instance of RKManagedObjectStore must be configured and assigned to the object manager
  6. Persistent models inherit from RKManagedObject rather than RKObject
  7. Creates the object mapping targeting the Core Data entity with the specified name by RKManagedobjectMapping
  8. A Primary Key property must be defined by primaryKeyAttribute

Example for configuring the Core Data entities:

1. defining –  product and product description

 01

02

 

2. Configuring the Core Data object mapping.

 03

The primaryKeyAttribute was defined the primary key for the table ‘MKG_PROD_DESC’.

 04

 

The primaryKeyAttribute was defined the primary key for the table ‘MKG_PROD’. The hasOne relationship defined the relationship between product and product description.

 

Once these configuration changes have been completed, RestKit will load & map payloads into Core Data backed classes.

There are a couple of common gotchas and things to keep in mind when working with Core Data:

  1. You can utilize a mix of persistent and transient models within the application — even within the same JSON payload. RestKit will determine if the target object is backed by Core Data at runtime and will
    return managed and unmanaged objects as appropriate.
  2. RestKit expects that each instance of an object be uniquely identifiable via a single primary key that is present in the payload. This allows the mapper to differentiate between new, updated and removed objects.
  3. Apple recommends utilizing one managed object context instance per thread. When you retrieve a managed object context from RKManagedObjectStore, a new instance is created and stored onto thread local storage if the calling thread is not the main thread. You don’t need to worry about managing the life-cycle of the managed object contexts or merging changes — the object store observes these thread-local contexts and handles merging changes back into the main object context.
  4. RestKit assumes that you use an entity with the same name as your model class in the data model.
  5. There is not currently any framework level help for working with store migrations.

 

 

TAGS  

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Subscribe to the Weekly Blog Digest:

Sign Up

Categories