魔法logging导入(下一步)

我已经把标题的下一步,因为这是不一样的问题,因为我几乎完全相同的标题上一个问题。

我有一个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我的设置与导入工作。 任何人不存在的logging(与Id)都被创build。 任何存在的都会被更新。

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

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

当这些对象像这样下来时,魔法logging导入在到达人员导入时停止。

代码崩溃在…

 - (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 

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

MagicalRecord无法自动将关系映射到这个JSON格式:

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

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

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

这样MagicalRecord就知道这是一个对象,它会在你现有的数据库中为具有上述ID的Personlogging做适当的查找并映射关系。

所以这有两个问题。 如果您无法更改JSON输出,则必须在Photo上创build一个类别类别,然后亲自手动映射关系。 我会在第二个问题之后去解决这个问题。

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

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

JSON的有效载荷非常小,所以如果可以的话,也不会伤害到这个。 另外,只有在数据库中不存在的情况下才会创build新的Personlogging。

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

Photo创build一个名为Photo+Mapping.h/.m的类别类。 我喜欢坚持+Mapping这些。 然后这个类应该是头文件和实现文件中的Photo (Mapping) ,你很好。

MagicalRecord有许多实例方法可用来覆盖(见MagicalRecord的作者写的MagicalRecord导入本文的后半部分),其中包括import<;attributeName>;:import<;relationshipName>;: 。 还有一个willImport:didImport:shouldImport:类本身的方法,它允许你重写任何映射。

对于你的情况,你可以使用import<;relationshipName>;:或者shouldImport: 我把这两个,因为一个有一点好处取决于你是否已经映射所有的Person对象,他们可用于Photo对象的关系映射。

以下是你可以做的事情的例子(如果你愿意的话,你可以select合并其中的一些),这样做不会有什么伤害的。 这里有一个注释:在覆盖映射时, 总是使用当前的NSManagedObjectContext (通过self.managedObjectContext方便地通过self.managedObjectContext访问),否则最终会出现上下文问题。

一定要inputPerson:

 #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]; } } 

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