MagicalRecord 无法使用此 JSON 格式自动映射关系:
{
Id : thisIsThePhotoID,
Date : today,
UserId : 12345asdfg
}
为了让 MagicalRecord 将关系映射到Person
对象,它也必须是 JSON 中的对象,例如:
{
Id : thisIsThePhotoID,
Date : today,
User : {
UserId : 12345asdfg
}
}
这样 MagicalRecord 就知道它是一个对象,并且它将在现有数据库中进行适当的查找Person
记录上面的ID并映射关系。
不过,这有两个问题。如果您无法更改 JSON 输出,则必须在其上创建一个类别类Photo
您自己手动映射关系。我将在第二期之后讨论这个问题。
第二个问题是,上述 JSON 格式假设您已经解析了用户并将记录存储在数据库中。如果还没有,MagicalRecord 将创建一个新的Person
使用上述 ID 进行记录,但由于该对象上不存在其他属性(请注意 UserId 键是字典中的唯一属性),它将相当空,并且不包含姓名和电子邮件地址。您始终可以扩展 JSON(如果有可能)以将这些属性也包含在照片字典内的 Person 字典中:
{
Id : thisIsThePhotoID,
Date : today,
User : {
UserId : 12345asdfg,
FullName : Oliver,
EmailAddress : [email protected]
}
}
JSON 有效负载非常小,因此如果可以的话这样做也没什么坏处。另外它只会创建一个新的Person
记录数据库中是否尚不存在。
然后进行手动映射。如果您无法将 JSON 更改为上述格式,则必须手动覆盖关系映射,因为 JSON 未按照 MagicalRecord 进行映射的方式准备。
创建一个类别类Photo
called Photo+Mapping.h/.m
。我喜欢坚持+Mapping
对于这些。那么班级应该是Photo (Mapping)
在头文件和实现文件中,你就可以开始了。
MagicalRecord 有许多可供重写的实例方法(请参阅这篇关于 MagicalRecord 导入的文章由MagicalRecord的作者编写),其中有import<;attributeName>;:
and import<;relationshipName>;:
。还有一个willImport:
, didImport:
and shouldImport:
类本身的方法允许您覆盖任何映射。
对于您的情况,您可以使用import<;relationshipName>;:
or shouldImport:
。我选择了这两个,因为其中一个有一点好处,具体取决于您是否已经映射了所有内容Person
对象,并且它们可用于关系映射Photo
object.
以下是您可以执行的操作示例(如果您愿意,可以选择将其中的一些操作组合起来,这样做也没什么坏处)。这里有一个注释:ALWAYS使用当前的NSManagedObjectContext
当覆盖映射时(可以通过 MagicalRecord 轻松访问)self.managedObjectContext
)否则你最终会遇到上下文问题。
请务必导入人员:
#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];
}
}
希望这可以帮助!如果您有任何疑问,请告诉我。