我有两个实体“人”和“汽车”来自网络服务。 我需要通过核心数据将其存储在sqlite中。我想知道将save方法与NSmanagedobject绑定或在实用程序或管理器中以不同方式编写时最好的方法是什么?
**Approach 1**
@interface Person : NSManagedObject
@property (nonatomic, retain) NSString * title;
- (void) saveManagedObject:(NSDictionary*)response //Responsible for init the person object and save in context
@end
**Approach 2**
@interface CoreDataUtility : NSObject
- (void) saveManagedObject:(NSDictionary*)response //Responsible for create the person object and save in context
@end
答案 0 :(得分:0)
Short answer: both methods are good, especially if you do not want to deal with 3rd party frameworks. Those might be useful, but the also bear some risks; this would be another question.
It is perfectly fine to have a creation method in the `NSManagedObject´ subclass (in a category or elsewhere*).
However, you should make it a class method, not an instance method.
To create an entity in a DataManager
class is also a feasible and a very common pattern. The good thing about the utility class is that you can more easily maintain multiple contexts for background operations.
*) I do not use categories any more because it is rare to change the model by more than a few fields- I prefer to edit them manually.