iOS CoreData + MoGenerator:仅当我使用嵌套上下文时,如何初始化托管对象一次? [英] iOS CoreData+MoGenerator: How do I initialize a Managed Object once only when I am using nested contexts?

查看:274
本文介绍了iOS CoreData + MoGenerator:仅当我使用嵌套上下文时,如何初始化托管对象一次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用mogenerator从具有TestPerson管理对象的模型生成代码。 TestPerson从抽象对象TLSyncParent继承。在TLSyncParent我有代码:

I am using mogenerator to generate code from a model with a TestPerson managed object. TestPerson inherits from the abstract object TLSyncParent. In TLSyncParent I have the code:

- (void) awakeFromInsert
{
    [super awakeFromInsert];
    QNSLOG(@"%@\n%@", self.managedObjectContext, self.description);
    if (self.syncStatus == nil) {
        self.syncStatusValue = SYNCSTATUS_NEW;
        self.tempObjectPID = [self generateUUID];
        QNSLOG(@"After init values\n%@", self.description);
    }
}



我在childMOC中创建TestPerson对象,父对象是mainMOC ,其父为rootMOC。 awakeFromInsert按预期运行,并进行init更改。当我将childMOC保存到mainMOC时,会再次运行awakeFromInsert。从文档我不会指望,但有一些歧义。从文档中,您通常使用此方法来初始化特殊的默认属性值。此方法在对象的生命周期中仅调用一次。真正的问题是,当awakeFromInsert在mainMOC中运行时,在childMOC中进行的init更改不存在。 awakeFromInsert显然是在保存实际发生之前运行的。

I create the TestPerson object in childMOC whose parent is mainMOC, whose parent is rootMOC. awakeFromInsert runs as expected and makes the init changes. When I save childMOC to mainMOC, awakeFromInsert is run again. From the docs I would not expect that, but there is some ambiguity. From the Docs, "You typically use this method to initialize special default property values. This method is invoked only once in the object's lifetime." The real problem is that when awakeFromInsert runs in mainMOC, the init changes made in childMOC are NOT there. awakeFromInsert is apparently run before the save actually takes place.

2013-10-02 11:22:45.510_xctest[21631:303] TestPerson -awakeFromInsert <NSManagedObjectContext: 0xd684780>
<TestPerson: 0xd6863b0> (entity: TestPerson; id: 0xd684ed0 <x-coredata:///TestPerson/t02B71E0D-AE3F-4605-8AC7-638AE072F2302> ; data: {
    dept = nil;
    job = nil;
    objectPID = nil;
    personName = nil;
    syncStatus = 0;
    tempObjectPID = nil;
    updatedAt = nil;
})
2013-10-02 11:22:45.511_xctest[21631:303] TestPerson -awakeFromInsert After init values
<TestPerson: 0xd6863b0> (entity: TestPerson; id: 0xd684ed0 <x-coredata:///TestPerson/t02B71E0D-AE3F-4605-8AC7-638AE072F2302> ; data: {
    dept = nil;
    job = nil;
    objectPID = nil;
    personName = nil;
    syncStatus = 4;
    tempObjectPID = "7AB46623-C597-4167-B189-E3AAD24954DE";
    updatedAt = nil;
})
2013-10-02 11:22:45.511_xctest[21631:303] CoreDataController -saveChildContext: Saving Child MOC
2013-10-02 11:22:45.511_xctest[21631:303] TestPerson -awakeFromInsert <NSManagedObjectContext: 0xd682180>
<TestPerson: 0xd68fce0> (entity: TestPerson; id: 0xd684ed0 <x-coredata:///TestPerson/t02B71E0D-AE3F-4605-8AC7-638AE072F2302> ; data: {
    dept = nil;
    job = nil;
    objectPID = nil;
    personName = nil;
    syncStatus = 0;
    tempObjectPID = nil;
    updatedAt = nil;
})
2013-10-02 11:22:45.511_xctest[21631:303] TestPerson -awakeFromInsert After init values
<TestPerson: 0xd68fce0> (entity: TestPerson; id: 0xd684ed0 <x-coredata:///TestPerson/t02B71E0D-AE3F-4605-8AC7-638AE072F2302> ; data: {
    dept = nil;
    job = nil;
    objectPID = nil;
    personName = nil;
    syncStatus = 4;
    tempObjectPID = "B799AFDA-3514-445F-BB6F-E4FE836C4F9D";
    updatedAt = nil;
})

使用MoGenerator结构时,管理对象初始化的正确位置是什么?

What is the proper place to initialize a managed object when using the MoGenerator structure?

推荐答案

好的,感谢Tom Herrington,我发现了一个非常好的方法。它似乎做的正是我想要的最少的麻烦。它完全符合MoGenerator结构。我已经有一个类的NSManagedObject与方法initWithMO​​C。我添加了一个对awakeFromCreate方法的调用,并提供了一个默认实现。你只需重写awakeFromCreate,就像覆盖awakeFromInsert一样。唯一的要求是您始终使用initWithMO​​C方法创建MO。

OK, thanks to Tom Herrington, I found a very nice way to do this. It seems to do exactly what I want with a minimum of trouble. It fits perfectly with the MoGenerator structure. I already had a category on NSManagedObject with the method initWithMOC. I added a call to the method awakeFromCreate and provided a default implementation. You just override awakeFromCreate in the same way you would override awakeFromInsert. The only requirement is that you ALWAYS create the MO using the initWithMOC method.

@implementation NSManagedObject (CoreDataController)

+ (NSManagedObject*) initWithMOC: (NSManagedObjectContext*) context
{
    NSManagedObject* mo = (NSManagedObject*)
            [NSEntityDescription insertNewObjectForEntityForName: NSStringFromClass(self)
                                          inManagedObjectContext: context];

    [mo awakeFromCreate];
    return mo;
}

- (void) awakeFromCreate
{
    return;
}

这篇关于iOS CoreData + MoGenerator:仅当我使用嵌套上下文时,如何初始化托管对象一次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆