Fog*_*ter 3 core-data objective-c ios magicalrecord
我已经在标题中添加了下一步,因为这与我之前的几乎完全相同标题的问题不同.
我有一个Person实体.
Person
--------
name - mappedKeyName: FullName
email - mappedKeyName: EmailAddress
personID - mappedKeyName: Id
--------
photos
Run Code Online (Sandbox Code Playgroud)
和一个Photo实体.
Photo
--------
image
createDate - mappedKeyName: Date
photoID - mappedKeyName: Id
--------
owner (type Person) - mappedKeyName: UserId - relatedByAttribute: personID
Run Code Online (Sandbox Code Playgroud)
还有其他与之相关的对象,Person而这些对象的JSON也是如此......
{
ObjectId : blah,
Owner : {
Id : 12345asdfg,
FullName : Oliver,
EmailAddress : oliver@oliver.com
}
}
Run Code Online (Sandbox Code Playgroud)
使用此JSON,我的设置适用于导入.创建任何不存在的人(带有Id).任何确实存在的都会更新.
但是,照片JSON对象是这样的......
{
Id : thisIsThePhotoID,
Date : today,
UserId : 12345asdfg
}
Run Code Online (Sandbox Code Playgroud)
当对象像这样下来时,魔法记录导入在进入人员导入时停止.
代码崩溃在......
- (id) MR_relatedValueForRelationship:(NSRelationshipDescription *)relationshipInfo
{
NSString *lookupKey = [self MR_lookupKeyForRelationship:relationshipInfo];
return lookupKey ? [self valueForKeyPath:lookupKey] : nil; // it stops here.
}
Run Code Online (Sandbox Code Playgroud)
值为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
Run Code Online (Sandbox Code Playgroud)
我真的不知道为什么这不起作用.报告中我没有任何明智的错误.
run*_*mad 17
MagicalRecord无法使用此JSON格式自动映射关系:
{
Id : thisIsThePhotoID,
Date : today,
UserId : 12345asdfg
}
Run Code Online (Sandbox Code Playgroud)
为了让MagicalRecord将关系映射到Person对象,它也必须是JSON中的对象,例如:
{
Id : thisIsThePhotoID,
Date : today,
User : {
UserId : 12345asdfg
}
}
Run Code Online (Sandbox Code Playgroud)
这样,MagicalRecord知道它是一个对象,它将在现有数据库中对Person具有上述ID 的记录进行适当的查找并映射关系.
因此,这有两个问题.如果您无法更改JSON输出,则必须Photo在自己手动映射关系的位置创建类别类.在第二期之后我会谈到这一点.
第二个问题是上述JSON格式假设您已经解析了用户并将记录存储在数据库中.如果你没有MagicalRecord将创建一个Person具有上述ID 的新记录,但由于该对象上没有其他属性(注意UserId键是字典中唯一的属性),它将是相当空的,不包括名称和电子邮件地址.您可以随时扩展您的JSON(如果您有这种可能性)以在Photo字典中的Person字典中包含这些属性:
{
Id : thisIsThePhotoID,
Date : today,
User : {
UserId : 12345asdfg,
FullName : Oliver,
EmailAddress : oliver@oliver.com
}
}
Run Code Online (Sandbox Code Playgroud)
JSON有效负载非常小,所以如果可以的话,这样做并没有什么坏处.另外,Person如果数据库中不存在新记录,它只会创建新记录.
然后进行手动映射.如果您无法将JSON更改为上述格式,则必须手动覆盖关系映射,因为JSON未按MagicalRecord映射的方式准备.
为Photo被调用创建一个类别类Photo+Mapping.h/.m.我喜欢坚持+Mapping这些.然后该类应该Photo (Mapping)在头文件和实现文件中,你很高兴.
MagicalRecord有许多实例方法可以覆盖(参见MagicalRecord作者编写的MagicalRecord导入文章的后半部分),其中包括import<;attributeName>;:和import<;relationshipName>;:.还有一个willImport:,didImport:和shouldImport:类本身,它允许您覆盖任何映射方法.
对于您的情况,您可以使用import<;relationshipName>;:或shouldImport:.我选择了这两个因为一个有一点好处,这取决于你是否已经映射了所有Person对象,并且它们可用于Photo对象上的关系映射.
以下是您可以做的事情的例子(如果您愿意,可以选择合并其中一些,这样做也没有坏处).这里有一个注释:在覆盖映射时总是使用当前的NSManagedObjectContext(通过MagicalRecord可以轻松访问self.managedObjectContext),否则你最终会遇到上下文问题.
一定要导入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];
}
}
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助!如果您有任何疑问,请告诉我.
| 归档时间: |
|
| 查看次数: |
3792 次 |
| 最近记录: |