2013年2月11日月曜日

CoconutKitの解説ページ調べ。Core data情報を探して。

coconutkitという便利そうなライブラリを使ってみるため、
調査開始。

まず、以下のwikiが初歩の解説。
https://github.com/defagos/CoconutKit/wiki/Introduction

containerに関するページ。
https://github.com/defagos/CoconutKit/wiki/Containers

残念!Core Dataに関する解説は、まだwikiにはありませんでした。
https://github.com/defagos/CoconutKit/wiki/Core-Data-extensions

Core Data関連のコードは、ここに掲載されていました。コードを読んで解読するしかないですね。(作者の方は、「コードに解説を書いているのでそれを参考にして欲しい。」といったことをどこかに書いておられました。コードのコメントで最新情報を書いておくから、ということで)
https://github.com/defagos/CoconutKit-CocoaPods/tree/master/Sources/CoreData

CoconutKitをcocoa podを使ってインストールする解説はこちら。
https://github.com/defagos/CoconutKit/wiki/Getting-started

私は特にCore Dataに関する情報が欲しいので、
wikiのintroductionページから、以下にコピっておきます。自分用に日本語メモもつけますが、意味の正確性は、怪しいところがあり、訳は断片的で不十分です。ご了承ください。

Core Data management

Core Data is a wonderful framework. In too many projects I worked on, I started with a custom lightweight object model fulfilling exactly my needs. As usual, I then discovered I needed a way of requesting objects, persisting them or checking their consistency. And in all cases, I ended up rewriting part of Core Data, regretting not to have used it right from the start.
このライブラリの作者defagoさんは、Core Dataを非常に素晴らしい、といっていますね。最初から使っていれば良かった、と。私と同じ間違いはしないで、とも。Core Dataの全ての機能が必要なくても、かかる時間と頭痛を減らしてくれる、とも。
You should never make the same mistakes as I made. Even if you do not need all of Core Data features at the time you start a project, you can be sure that Core Data will save you a lot of time and headaches later. You just need a few model objects, nothing more? Well, that's not a reason not to use Core Data and an in-memory persistent store, which lets your conveniently design your model using the Xcode Core Data editor. When you later discover that your objects must finally be persisted on disk, all that is basically required is to change a single setting.
There are usually a few issues when working with Core Data, though:
Core Dataを使う場合、幾つかの課題がある、と。
  • There is a lot of boilerplate code to write, and the Xcode Core Data template is rather poor(boilerplate、すなわち決まり文句をたくさん書かなくてはならず、XcodeのCore Dataのテンプレートはやや貧弱)
  • The code becomes cluttered with managed object contexts, and issues can arise in multi-threaded applications if you use the wrong context by mistake
  • Validation is poorly implemented and leads to a lot of boilerplate code (e.g. for proper error-chaining), especially if you want to perform validation both interactively (when a model field is edited using a text field) or when an object is saved to the persistence store (ヴァリデーションの実装が貧弱で、お決まりのコードをたくさん書かなくてはならない。特に、インタラクティブなテキストフィールドを扱う場合や、persistence storeにセーブする場合。)
To solve all those issues, CoconutKit provides with:
で、CoconutKitが提供する解決策は、
  • HLSModelManager, a class which manages context and persistence store creation for you. (HLSModelManagerというクラスは、コンテクストを制御し、persistence storeをあなたのために作成してくれます)No more Xcode templates to use, everything has been done once for all.(Xcodeテンプレートを使う必要が無く、全てのことを一度に行います) To eliminate issues with explicit managed contexts thrown all over the place, you now never interact with managed contexts directly.  Instead, stacks of HLSModelManager objects are managed on a thread basis.(managed contextsに直接触れることなく、HLSModelManagerオブジェクトは、スレッドベースでマネージします) Context-free methods then allow you to perform usual CRUD operations with the managed object context associated with the top model manager only.
  • A validation runtime layer, which makes it possible to write validation methods once for all in a single place, and to call them both interactively and when saving objects to the store. Error-chaining has been implemented correctly, and text field bindings have been implemented so that form creation is made as easy as possible (it is just a matter of saying which model object field is edited by a text field, CoconutKit takes care of the rest)
また、作者さんのブログにCoconutKit1.1の時のCore Dataに関する記述もあったので、メモ。
(実装の具体例はないのが、残念。どこかにないかな。。)



Easy Core Data validation

Usually, when writing Core Data validation methods, you need to add a lot of boilerplate code to handle error chaining. Not only is error chaining easy to make wrong, it also clutters your precious business logic with Core Data code, making validation methods more difficult to maintain and reuse. Most notably, such validation methods cannot be reused for simple needs like validation of values entered interactively. Core Data validations are namely meant to be triggered by Core Data in a row once an object is saved to the store. It is often not what you want. 



CoconutKit 1.1 therefore introduces a new way to write validation methods, without the need to care about the usual Core Data boilerplate code. Your methods now can focus on business logic only, and can be easily called to validate fields individually when needed. 

But there is more. If you often write forms for editing Core Data objects (most notably made of text fields), you probably always end up writing the same kind of code: 

Calling the validation logic when the user exits edit mode for a text field
Synchronizing model fields with text fields in both model-to-view and view-to-model directions
Writing formatting code if values need to be formatted

These days are over. You now simply bind a text field to a model object field (providing an optional NSFormatter if needed), all the rest is handled for you. Your code will be faster and easier to write, understand, and maintain. 

I put a lot of effort to get this feature right, testing it extensively and ensuring it is really easy to use. I sincerely hope it will help you write better Core Data validation code.

:::::::::::::::::::::::::::::::

ソースを見ると、以下のファイル群あたりで実装されている様子。


HLSManagedTextFieldValidator.m
HLSModelManager.m
NSManagedObject+HLSExtensions.m
NSManagedObject+HLSValidation.m

まあ、まず自分で、Core Dataの基礎を学ばないと、手がつけられない感じです。
最初から楽をしよう、という心を反省して、基礎をまず学びます。

0 件のコメント:

コメントを投稿