我已经获得了返回以下数据的API
{
"id": "45832",
"name": "test name",
"parentId":"xyz",
//other fields
}
我可以使用restkit保存这些数据。我有另一个API来获取只返回更新字段的更改。例如
{
"id": "45832",
"name": "test name2",
//other updated fields
}
我希望这些数据更新db中的现有对象。但在我的情况下,它将parentId
和其他缺少的属性设置为null。我在实体映射中设置了标识属性。
编辑:
以下是具有实体映射的托管对象的类。 #import" ZEntity.h" #import" ZContent.h" #import" ZThumb.h"
@implementation ZEntity
@dynamic availableFrom;
@dynamic dueDate;
@dynamic entityId;
@dynamic name;
@dynamic perCompleted;
@dynamic state;
@dynamic type;
@dynamic thumb;
@dynamic contents;
@dynamic completedOn;
@dynamic totalScore;
@dynamic parent;
@dynamic settings;
+ (RKEntityMapping *)map{
RKEntityMapping *mapping = [RKEntityMapping mappingForEntityForName:NSStringFromClass([self class]) inManagedObjectStore:[RKManagedObjectStore defaultStore]];
mapping.persistentStore = [RKManagedObjectStore defaultStore].persistentStoreCoordinator.persistentStores.firstObject;
NSAssert(mapping.persistentStore != nil, @"Requires Persistent Store");
mapping.identificationAttributes = @[@"entityId"];
[mapping addAttributeMappingsFromDictionary:@{
@"id" : @"entityId",
@"type" : @"type",
@"name" : @"name",
@"dueDate" : @"dueDate",
@"state" : @"state",
@"availableFrom" :@"availableFrom",
@"percCompletion" : @"perCompleted",
@"completedOn" : @"completedOn",
@"totalScore" : @"totalScore"
}];
[mapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"thumb" toKeyPath:@"thumb" withMapping:[ZThumb map]]];
return mapping;
}
@end
响应描述符
RKResponseDescriptor* entitiesD = [RKResponseDescriptor responseDescriptorWithMapping:[ZEntity map]
method:RKRequestMethodAny pathPattern:nil keyPath:@"objects"
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
Json回应
{
"total": 9,
"start": 1,
"count": 10,
"objects": [{
"id": "636488166759829858",
"type": "UPDATE",
"name": "copy1",
"canReattempt": false,
"score": 0,
"totalScore": 300,
"thumb": {
"id": "THUMB_QUPDATE4",
"expiry": 1472545125,
"url_180_120": "//s3-ap-southeast-1.amazonaws.com/mtgame-cdn.mindtickle.com/allaboard4.0/thumb/Q-Update-Thumbnail_04.png?AWSAccessKeyId=AKIAJYDLQOOEIA6NZLPQ&Expires=1472545125&Signature=To8a5SN7CoCiWDmmtXLkyANKulw%3D"
},
"dueDate": 1440845008,
"dueDateObj": {
"dueOn": 1440845008,
"dueDateExpiryAction": "NONE"
},
"state": "ACTIVE",
"percCompletion": 0.0,
"availableFrom": 1440585808,
"completedOn": 0,
"startTime": null,
"expiryTime": null,
"certificateCutOffScore": -1,
"lastPublishTime": 1440585653,
"completionCertificateEnabled": null
}]
}
在之前的调用中,我为parent
类设置了Entity
字段,这是一种关系(我也通过sqlite浏览器对此进行了验证)。在这个json中没有父母返回。完成此调用后,parent将设置为null。我必须保留现有的父值。