RESTKit:对象失去了关系

时间:2014-08-07 08:07:44

标签: ios core-data restkit entity-relationship restkit-0.20

收到的JSON数据:

{
    "meeting": [{
        "meetingId": 506,
        "ownerId": "John.Doe",
        "startDate": "2014-07-27T13:15:07.000Z",
        "activities": [{
            "activityType": "Active Activity",
            "activityId": 729,
            "locationAddress": "1188 El Camino Real, San Bruno, CA, United States",
            "startTime": "2014-07-28T04:45:00.000Z",
            "customData": {
                "title": "Active Activity"
            },
            "modified": "2014-07-23T13:26:41.000Z"
        }],
        "senderId": "Johnny.Appleseed",
        "status": 8
}

SQLite文件: enter image description here

我有以下实体:

meeting
activity
customData

关系:

meeting与名为to-many的{​​{1}}有activity个关系

activitiesactivity

to-many的关系

当然,每种关系都存在反比关系。

customData customData只有一个名为entity

的属性

这是我的customData.h

title

RESKit Mapping:

@class Activity;

@interface customData : NSManagedObject

@property (nonatomic, retain) NSString * title;
@property (nonatomic, retain) Activity *activity;

@end

的ObjectManager:

+(RKEntityMapping *)customDataMapping:(RKEntityMapping *)customDataMapping;
{
    [customDataMapping addAttributeMappingsFromDictionary:@{@"title":@"title"}];
    customDataMapping.identificationAttributes = @[@"title"];
    return customDataMapping;
}

问题: 出于某种原因,我失去了 meetingMapping = [RESTMappingProvider meetingPutMapping:meetingMapping]; activityMapping = [RESTMappingProvider activityPutMapping:activityMapping]; customDataMapping = [RESTMappingProvider customDataMapping:customDataMapping]; [activityMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:kCustomDataRelationship toKeyPath:kCustomDataRelationship withMapping:customDataMapping]]; [meetingMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:kActivitiesRelationship toKeyPath:kActivitiesRelationship withMapping:activityMapping]]; RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:[meetingMapping inverseMapping] objectClass:[Meeting class] rootKeyPath:nil method:RKRequestMethodAny]; NSIndexSet *statusCodeSet = RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful); RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:meetingMapping method:RKRequestMethodAny pathPattern:kMeetupKeyPath keyPath:nil statusCodes:statusCodeSet]; objectManager.requestSerializationMIMEType = RKMIMETypeJSON; [objectManager addRequestDescriptor:requestDescriptor]; [objectManager addResponseDescriptor:responseDescriptor]; [objectManager.HTTPClient registerHTTPOperationClass:[AFHTTPRequestOperation class]]; [objectManager putObject:anInvite path:kMeetupKeyPath parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) { NSLog (@"******* OUTBOX OBJECT PUT **********"); } failure:^(RKObjectRequestOperation *operation, NSError *error) { }]; } } } }); activity对象之间的关系。如您所见,CoreData的内部ID(customData)并不总是分配ID,因此,我丢失了对象,因为关系不再存在。我认为它在PUT调用期间没有为对象分配id。

我在RESKit论坛上看到有关使用ZACTIVITY@parent的信息,但我不确定如何使用它,或者这是否是正确的方法。请指教。

1 个答案:

答案 0 :(得分:1)

我猜测自定义数据的title并不总是唯一的,因此,因为您使用customDataMapping.identificationAttributes = @[@"title"];和1:多关系,自定义数据的某些实例将被断开连接从他们过去的旧关系开始,再加上新的关系。

可能你应该删除identificationAttributes并添加一个获取请求块,它会在收集数据后清除所有孤立的自定义数据对象(关系为零的对象)。