2

新しいノードごとに、本を含むxmlファイルを解析します。

Book *book = (Book*)[NSEntityDescription insertNewObjectForEntityForName:@"Book" inManagedObjectContext:managedObjectContext];

Core Data BookエンティティのNSManagedObjectを取得するには、次に、管理対象Bookオブジェクトにデータを入力し、それを配列に追加し、すすぎ、繰り返します。

終わったら、本のリストをユーザーに提示します。私はまだ保存を実行していません:

NSError *error;
if (![managedObjectContext save:&error]) {
    NSLog(@"%@", [error domain]);
}

ユーザーは今、私が保持したい本の1つを選択しますが、これだけで、他のすべての本はもう私には興味がありません。ブックエンティティには関係がないか、関係の一部ではありません。これは単なる「単一の」エンティティです。

「保存レバー」を引くと、すべてのBookオブジェクトが保持され、目的のオブジェクト以外のすべてを削除する必要があります。

この課題をどのように回避すればよいでしょうか。CoreDataProgrammingGuideでその特定のユースケースを見つけることができないようですが、これも少しバグがありますが、ここでのベストプラクティスに反対していますか?

助けてくれてありがとう。

4

1 に答える 1

1

Yes you are going against best practices. In that use case, if you really do not want to save the objects, keep them in an intermediate format and only create a Core Data object for the one you want to persist. However this seems quite wrong.

What is the harm in saving the other books? Are the only going to be used once ever?

Based on the small amount of information you have provided, it seems your approach to the problem needs to be reconsidered.

Update

Removing unsaved objects from the context has a very small overhead yes so that is the best solution given the parameters you have. I asked the other questions to see if there was a cleaner overall solution rather than building the objects just to throw them away. Sounds like you have already been down that path though.

于 2010-04-20T17:50:06.233 に答える