【问题标题】:Magical Record import (next step)魔法唱片导入(下一步)
【发布时间】:2013-05-02 12:17:16
【问题描述】:

我已经在标题中添加了下一步,因为这与我之前的问题不同,标题几乎完全相同。

我有一个Person 实体。

Person
--------
name      - mappedKeyName: FullName
email     - mappedKeyName: EmailAddress
personID  - mappedKeyName: Id
--------
photos

还有一个Photo 实体。

Photo
--------
image
createDate - mappedKeyName: Date
photoID    - mappedKeyName: Id
--------
owner (type Person) - mappedKeyName: UserId - relatedByAttribute: personID

还有其他与 Person 相关的对象,这些对象的 JSON 也是如此......

{
    ObjectId : blah,
    Owner : {
        Id : 12345asdfg,
        FullName : Oliver,
        EmailAddress : oliver@oliver.com
    }
}

有了这个 JSON,我的设置就可以处理导入。任何不存在的人员记录(带有 ID)都会被创建。并且任何确实存在的都会更新。

但是,照片 JSON 对象是这样来的……

{
    Id : thisIsThePhotoID,
    Date : today,
    UserId : 12345asdfg
}

当对象像这样掉下来时,Magical 记录导入会在人员导入时停止。

代码在...处崩溃

- (id) MR_relatedValueForRelationship:(NSRelationshipDescription *)relationshipInfo
{
    NSString *lookupKey = [self MR_lookupKeyForRelationship:relationshipInfo];
    return lookupKey ? [self valueForKeyPath:lookupKey] : nil;  // it stops here.
}

lookupKey 的值为@"personID"。

在断点处打印出 relationshipInfo 给出...

$6 = 0x1fd695e0 (<NSRelationshipDescription: 0x1fd695e0>),
    name owner,
    isOptional 0,
    isTransient 0,
    entity Photo,
    renamingIdentifier owner,
    validation predicates (),
    warnings (),
    versionHashModifier (null)
    userInfo {
        mappedKeyName = UserId;
        relatedByAttribute = personID;
    },
    destination entity Person,
    inverseRelationship photos,
    minCount 1,
    maxCount 1,
    isOrdered 0,
    deleteRule 1

我真的不知道为什么这不起作用。我没有报告任何明智的错误。

【问题讨论】:

    标签: ios objective-c core-data magicalrecord


    【解决方案1】:

    MagicalRecord 无法使用这种 JSON 格式自动映射关系:

    {
        Id : thisIsThePhotoID,
        Date : today,
        UserId : 12345asdfg
    }
    

    为了让 MagicalRecord 将关系映射到 Person 对象,它也必须是 JSON 中的对象,例如:

    {
        Id : thisIsThePhotoID,
        Date : today,
        User : {
            UserId : 12345asdfg
        }
    }
    

    这样,MagicalRecord 知道它是一个对象,它将在您现有的数据库中为具有上述 ID 的 Person 记录进行适当的查找并映射关系。

    不过,这有两个问题。如果您无法更改 JSON 输出,则必须在 Photo 上创建一个类别类,您可以在其中手动映射关系。我会在第二期之后再说。

    第二个问题是上述 JSON 格式假设您已经解析了用户并将记录存储在数据库中。如果您没有 MagicalRecord 将使用上述 ID 创建一个新的 Person 记录,但由于该对象上不存在其他属性(注意 UserId 键是字典中的唯一属性),它将相当空且不包括名称和电子邮件地址。您始终可以扩展您的 JSON(如果您有这种可能性)以将这些属性也包含在 Photo 字典中的 Person 字典中:

    {
        Id : thisIsThePhotoID,
        Date : today,
        User : {
            UserId : 12345asdfg,
            FullName : Oliver,
            EmailAddress : oliver@oliver.com
        }
    }
    

    JSON 负载非常小,所以如果可以的话,这样做并没有什么坏处。另外,如果数据库中不存在 Person 记录,它只会创建一个新记录。

    然后进行手动映射。如果您无法将 JSON 更改为上述格式,则必须手动覆盖关系映射,因为 JSON 没有像 MagicalRecord 进行映射那样准备。

    Photo 创建一个名为Photo+Mapping.h/.m 的类别类。对于这些,我喜欢坚持使用+Mapping。那么这个类在头文件和实现文件中应该是Photo (Mapping),你就可以开始了。

    MagicalRecord 有许多实例方法可以覆盖(参见 MagicalRecord 的作者写的this article on MagicalRecord importing 的后半部分),其中有import&lt;;attributeName&gt;;:import&lt;;relationshipName&gt;;:。类本身还有一个willImport:didImport:shouldImport: 方法,允许您覆盖任何映射。

    对于您的情况,您可以使用import&lt;;relationshipName&gt;;:shouldImport:。我选择了这两个,因为根据您是否已经映射了所有 Person 对象以及它们是否可用于 Photo 对象上的关系映射,一个有一点好处。

    以下是您可以执行的示例(如果您愿意,可以选择组合其中的几个,这样做并没有什么坏处)。在此注意:始终在覆盖映射时使用当前的 NSManagedObjectContext(通过 self.managedObjectContext 使用 MagicalRecord 可以轻松访问),否则您最终会遇到上下文问题。

    一定要导入 Person:

    #import "Photo+Mapping.h"
    #import "Person.h"
    
    // Assuming you only want to import the Photo object if you already have a Person stored this is a great method to tell MagicalRecord whether to continue with importing or not
    -(BOOL)shouldImport:(id)data {
        Person *person = [Person findFirstByAttribute:data[@"UserId"] value:@"personID" inContext:self.managedObjectContext];
        if (!person) {
            // no Person object exists so don't import the Photo object - again this is up to you since you might want to create the record if not
            return NO;
        }
        // you can set the relationship here (you might as well) or use the importPerson: method below (doing a second lookup, which is unnecessary at this point)
        [self setPerson:person];
        return YES;
    }
    
    // If you use this method you're doing the lookup to check whether a record exist when MagicalRecord is trying to map the Person relationship
    -(void)importPerson:(id)data {
        Person *person = [Person findFirstByAttribute:data[@"UserId"] value:@"personID" inContext:self.managedObjectContext];
        if (!person) {
            // if no Person record exists for the associated UserId, you should create one (or not - if you choose not to, it's wise to throw away this Photo object)
            person = [Person createInContext:self.managedObjectContext];
            [person setPersonID:data[@"UserId"]];
        }
        // set the relationship
        [self setPerson:person];
    }
    
    // finally you can also use the following method when MagicalRecord is done mapping and get rid of the Photo object if the Person relationship is nil:
    -(void)didImport:(id)data {
        if (!self.person) {
             [self deleteInContext:self.managedObjectContext];
        }
    }
    

    希望这会有所帮助!如果您有任何问题,请告诉我。

    【讨论】:

    • 优秀的答案!非常感谢。您是第一个说导入不适用于这种格式的人。 (我之前的三个问题的答案都说“它首先起作用”)。我明天要回去工作,所以我将着手实施您的解决方案。再次感谢。
    • @Fogmeister 告诉我进展如何,如果您有任何问题我可以提供帮助。
    • @Fogmeister 我很好奇它是否帮助您解决了问题?
    • 谢谢,最后我要求后端开发人员更改 JSON 以匹配支持的格式。我还没有尝试过其他方法。非常感谢。
    • 我知道这个答案有点陈旧,但它也根本不是真的。 MagicalRecord可以以这种格式导入关系,如本文所指出:cimgf.com/2012/05/29/importing-data-made-easy
    猜你喜欢
    • 2012-08-11
    • 1970-01-01
    • 2015-01-11
    • 1970-01-01
    • 2019-06-04
    • 2020-09-09
    • 1970-01-01
    • 2018-03-10
    • 1970-01-01
    相关资源
    最近更新 更多