开发者

Using CoreData with an object?

this is a beginner's question: Every tutorial I've seen on CoreData focusses on database-backed storage. They all start out with drawing an object model with relationships and whatnot.

But my app will be like a simple drawing app, think Illustrator light or OmniGraffle. I have an object in 开发者_StackOverflowmemory for the page and then several child object for the drawn elements. How would you use CoreData for this?

I don't need finished code just pointers or ideas.

Thank you!


Every program design starts (or should start) with the data model. The data model stores not only the data for the program but the logical relationships between pieces of data. The data model is the guts of the program and everything else is one or more interfaces that displays the data model in some manner.

Take the example of a simple drawing program that draws polygons. Logically, the program would start with a document. In core data this would be represented by a document entity in the data model and probably a NSManagedObject subclass. The document in turn would contain (relationship) one or more pages. The pages would also have an entity and a subclass. The pages would hold data about a physical page such as margins, footers, headers etc. A page would also hold the polygons. Each poly entity-subclass would hold have the information necessary to draw the polygon e.g. the points of the vertices.

As the user drew, the controller layer would convert the commands and screen positions into data in the data model and then back out again as needed.

There are many advantages to using core data to build the data model.

  • Core data automatically manages very complex logically relationships that are hard to track by hand.
  • It creates an automatic undo system
  • It makes it easier to transfer data (drawing in this case) between different parts of the program or to export subpart of a drawing.
  • It decouples the data from any particular interface. For example, you could output a drawing to LaTex or flash simply by writing another controller.
  • It makes it easy to script the application. Simply have the script interface programmatically with the data model instead of the user interface.

It is my understanding the Omnigraffle is based on Core Data so if you want an app like that Core Data is definitely the way to go.


Core Data requires defining a model of your object graph whether you're using a persistent store or an in-memory store. You should definitely read the Core Data Programming Guide.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜