Core Data Editor Version 5

Core Data Editor lets you easily view, edit and analyze applications‘ data. Core Data Editor is compatible with Mac and iOS applications and supports XML, SQLite and binary stores, visualizes all relationships and is able to edit the data and generate Objective-C code for the data model.

Getting started

Core Data Editor will guide you through a short setup process when you initially launch the app. This setup process is entirely optional. You can skip over each and every step if you know what you are doing. It is recommended that you take your time and go though the setup process. It only takes a few minutes. There are two important settings that you can make during the setup process:

  • iPhone Simulator directory: By specifying your iPhone Simulator directory you enable the project browser feature of Core Data Editor which is very handy for iOS developers. It allows you to quickly create Core Data Editor projects.
  • Xcode derived data directory: Xcode keeps your build products in one place. This place is called the derived data directory. This is where all your compiled apps end up. By specifying the derived data directory you can drop store files on the Core Data Editor dock icon. If you drop a store file on Core Data Editor it will automatically try to find a matching model in your derived data directory and if a compatible model is found a new Core Data Editor project is created on the fly.

Creating a new Core Data Editor project

If you are working on iOS apps, the easiest way to create new Core Data Editor projects is by using the project browser. If you do not want to use the project browser continue reading. Otherwise jump ahead to the project browser paragraph. You create a new project by selecting File | New Project… from the main menu. Now Core Data Editor wants you to drop an application or model on the drop zone. If you have your Xcode project open you can find your app in the group called Products. You should see your app there. If it appears in red you have to build it for running/archiving first. Then drag that item on the drop zone and click next. Now Core Data Editor needs to know the store file you would like to edit. Please not that this store file must be compatible with the model you dragged on Core Data Editor previously. If you do not have a store file handy you can use the Create New Store button. This creates a new empty store file. Click Next and you are done.

The project browser

The project browser is a powerful new feature in Core Data Editor. It simplifies the creation of new Core Data Editor projects by scanning your iPhone Simulator directory for compatible store and model files. You have to specify your iPhone Simulator directory for this to work. You can specify your iPhone Simulator directory in the preferences window.

You access the project browser via the Window | Project Browser menu item. It is possible that your iOS app does not appear in the project browser. This can happen because of the following reasons:

  • You are not using SQLite: At the moment the project browser does only support SQLite stores.
  • You are using AFIncrementalStore, RestKit or a similar framework: Core Data Editor is not compatible with those frameworks because they modify your managed object model at runtime and thus the store files found by the project browser are not compatible with the model in your app bundle.
  • Your app is not in the iPhone Simulator directory: Run you apps at least once on the Simulator to make sure it is in place. Also make sure to create a store file.

CSV

Core Data Editor can import and export CSV data.

CSV Export

Getting your data out of Core Data Editor is easy as pie. Simply select the objects you want to export and then choose Entity | Copy Selection as CSV. Core Data Editor will create CSV data for the selected managed objects and put the data in the clipboard. The order of the columns in the table view will be reflected in the generated CSV data.

CSV Import

Importing CSV data with Core Data Editor requires a bit more work.

  • Select the entity you want the CSV data to be imported to.
  • Click on the CSV Import toolbar item.
  • Select a CSV file from the open panel. Make sure to set the correct delimiter in the open panel. The default delimiter used is , (comma). If the first line in the CSV file contains column names you have to check the checkbox in the open panel that says First line contains column names. You also should check the date format in case you are importing CSV data with date values.
  • Next you have to map the columns in the CSV file to the attributes of your selected entity.
  • If you are happy with your settings press Import….

Troubleshooting If your CSV import fails or produces incorrect results you should immediately close the Core Data Editor project without saving. This has the effect that the faulty import will not be persisted. Then there are several things that you can do: First double check the settings you made (date format, delimiter, …) in the CSV open panel. Next make sure that your CSV file is encoded in Unicode (UTF-8). Core Data Editor tries to guess the encoding of the CSV files but to make it easy for Core Data Editor change the encoding of your CSV file if needed. You can do this by using TextEdit. Duplicate the CSV file with TextEdit and then save the duplicate. In the TextEdit save panel you can select the text encoding. Then import the duplicated file. If this fails as well please contact me. Send me the CSV file (you can remove sensitive data from it) and your managed object model as well as description of which settings you used.

Relationships

Core Data Editor can modify attributes but also relationships. Imagine the following model:

  • Entity: Person
  • Entity: PersonDetail
  • Relationship: Person has a to-one relationship called detail.
  • Relationship: PersonDetail has a to-one relationship called person.

Now imagine that you have a bunch of Person and PersonDetail objects already created and now you want to associate a PersonDetail with a Person object. You do that by selecting a Person. The bottom area displays the relationships of the selected Person object.

As you can see the selected person does not yet have an associated PersonDetail object. You can change that by clicking on the +-button in the bottom area. This will bring up a menu with two options: You can either create a new PersonDetail object and associate it with the selected Person object or (2nd. option) you can select an existing (PersonDetail) object from a list.

The object picker allows you to select an object that you want to associate with the selected person object by checking the checkbox in the first column. In the case of a to-many relationship you will be able to select more than one object in the picker. You can also use the picker to change the associated objects at any time.

Automatically resolve validation errors

This is a new feature in Core Data Editor 5. You can enable it in the General tab in the preferences. Some developers like to define constraints in their model (where it makes sense). For example you could say that it does not make sense for a banking applications to have a bank object without an associated bank detail object. If you want to enforce this with Core Data you usually define this in your managed object model in the form of relationship constraints. This is nice but it can make things more complicated when you use Core Data Editor. Because once you create a bank object with Core Data Editor you will immediately see a validation error. To fix that you would also have to create a bank detail object and associate it with your bank object.

Luckily Core Data Editor can help you with that. If you enable the Automatically resolve validation errors feature then Core Data Editor will try to resolve validation errors for you automatically. Core Data Editor does that by using the constraints defined in your model and the current set of validation errors. Then those information are combined and Core Data Editor creates new objects and associations so that the constraints are no longer violated. This works for to-many and to-one relationships. For example: If you have to mandatory to-one relationship Core Data Editor will create a new object so that the relationship constraint if fulfilled. If a to-many relationship has a min. count of five then Core Data Editor creates five objects to fulfill this constraint.

Generate Code

Core Data Editor can generate Objective-C code for your model. Under the hood Core Data Editor is using mogenerator. If you want to use mogenerator without Core Data Editor you would have to download it on your own and learn how to use it. If you simply want to generate code quickly use the built in code generation feature of Core Data Editor. Before you make use of the code generation feature of Core Data Editor make sure that at least one of your entities has a custom managed object class. Core Data Editor can only generate code for entities which have a custom managed object class. If you think a bit about it this makes sense. To set a custom class open your model in Xcode and select a entity. In the data model inspector you should see a section called Entity. There should be two text fields. The first text field contains the name of the entity and the second one contains the class name of your managed object subclass. If you see NSManagedObject there replace it by something else.