Ios 神奇记录导入(下一步)

Ios 神奇记录导入(下一步),ios,objective-c,core-data,magicalrecord,Ios,Objective C,Core Data,Magicalrecord,我在标题中加入了下一步,因为这与我上一个问题的标题几乎完全相同 我有一个人实体 Person -------- name - mappedKeyName: FullName email - mappedKeyName: EmailAddress personID - mappedKeyName: Id -------- photos Photo -------- image createDate - mappedKeyName: Date photoID - mapp

我在标题中加入了下一步,因为这与我上一个问题的标题几乎完全相同

我有一个
实体

Person
--------
name      - mappedKeyName: FullName
email     - mappedKeyName: EmailAddress
personID  - mappedKeyName: Id
--------
photos
Photo
--------
image
createDate - mappedKeyName: Date
photoID    - mappedKeyName: Id
--------
owner (type Person) - mappedKeyName: UserId - relatedByAttribute: personID
和一个
照片
实体

Person
--------
name      - mappedKeyName: FullName
email     - mappedKeyName: EmailAddress
personID  - mappedKeyName: Id
--------
photos
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为)的人员记录。任何确实存在的都会被更新

但是,photos JSON对象是这样的

{
    Id : thisIsThePhotoID,
    Date : today,
    UserId : 12345asdfg
}
当对象像这样下降时,当它到达人物导入时,神奇记录导入停止

代码在

- (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
$6=0x1fd695e0(),
名称所有者,
等参比0,
isTransient 0,
实体照片,
重命名标识符所有者,
验证谓词(),
警告(),
versionHashModifier(空)
用户信息{
mappedKeyName=用户ID;
relatedByAttribute=personID;
},
目的地实体人,
反向关联照片,
minCount 1,
最大计数1,
有序0,
删除规则1

我真的不知道为什么这不起作用。我没有收到任何可报告的合理错误。

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

{
    Id : thisIsThePhotoID,
    Date : today,
    UserId : 12345asdfg
}
为了让MagicalRecord将关系映射到
Person
对象,它也必须是JSON中的对象,例如:

{
    Id : thisIsThePhotoID,
    Date : today,
    User : {
        UserId : 12345asdfg
    }
}
这样,MagicalRecord就知道它是一个对象,它将在现有数据库中对具有上述ID的
Person
记录进行适当的查找,并映射关系

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

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

{
    Id : thisIsThePhotoID,
    Date : today,
    User : {
        UserId : 12345asdfg,
        FullName : Oliver,
        EmailAddress : oliver@oliver.com
    }
}
JSON负载非常小,因此如果可以的话,这样做不会有什么坏处。另外,如果数据库中还没有新的
Person
记录,那么它只会创建一个新的
Person
记录

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

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

MagicalRecord有许多可重写的实例方法(请参阅MagicalRecord作者编写的后半部分),其中包括
import;:
导入;:。类本身上还有一个
willImport:
didImport:
shoulimport:
方法,允许您重写任何映射

对于您的情况,您可以使用
import;:
应导入:
。我之所以选择这两个对象,是因为其中一个有一点好处,这取决于您是否已经映射了所有
Person
对象,并且它们可以用于
Photo
对象上的关系映射

下面是一些您可以做的示例(如果您愿意,可以选择将其中的一些组合在一起,这样做不会有什么坏处)。此处需要注意的一点是:重写映射时始终使用当前的
NSManagedObjectContext
(通过
self.managedObjectContext
使用MagicalRecord可以轻松访问),否则最终会出现上下文问题

请确保导入人员:

#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可以导入此格式的关系,如本文所述: