RESTKit:POST后关系中的重复对象

时间:2014-05-06 04:15:25

标签: core-data ios7 restkit-0.20

以下是我的设置:

- (RKManagedObjectStore *)setupCoreDataWithRESTKit
{
    NSError * error;
    NSURL * modelURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"App" ofType:@"momd"]];
    NSManagedObjectModel * managedObjectModel = [[[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL] mutableCopy];
    self.managedObjectStore = [[RKManagedObjectStore alloc] initWithManagedObjectModel:managedObjectModel];

    [self.managedObjectStore createPersistentStoreCoordinator];

    NSArray * searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    NSString * documentPath = [searchPaths objectAtIndex:0];
    NSPersistentStore * persistentStore = [self.managedObjectStore addSQLitePersistentStoreAtPath:[NSString stringWithFormat:@"%@/App%@.sqlite", documentPath, [TBSPersistence username]] fromSeedDatabaseAtPath:nil withConfiguration:nil options:[self optionsForSqliteStore] error:&error];
    NSAssert(persistentStore, @"Failed to add persistent store with error: %@", error);

    NSLog(@"Path: %@", [NSString stringWithFormat:@"%@/App%@.sqlite", documentPath, [TBSPersistence username]]);

    if(!persistentStore){
        NSLog(@"Failed to add persistent store: %@", error);
    }

    [self.managedObjectStore createManagedObjectContexts];

    return self.managedObjectStore;
}

POST邀请 - 邀请与Activity ::

有多对多的关系
-(void)sendInvite:(NSInteger)methodType
{
    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    self.objectManager = [self getObjectManager];
    self.objectManager.managedObjectStore = appDelegate.managedObjectStore;

    RKEntityMapping *invitationMapping = [RKEntityMapping mappingForEntityForName:kInvite
                                                             inManagedObjectStore:self.objectManager.managedObjectStore];



    RKEntityMapping *activityMapping = [RKEntityMapping mappingForEntityForName:kActivity
                                                           inManagedObjectStore:self.objectManager.managedObjectStore];


    Invite *invitation;


            invitationMapping = [RESTMappingProvider invitionPostMapping:invitationMapping];
            invitationMapping.identificationAttributes = @[@"inviteId"];

            activityMapping =  [RESTMappingProvider activityPostMapping:activityMapping];
           activityMapping.identificationAttributes = @[@"activityId"];

            [invitationMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:kActivitiesRelationship
                                                                                              toKeyPath:kActivitiesRelationship
                                                                                            withMapping:activityMapping]];
            invitation = [self setupInviteProperties:STPOST];
            [self setupDescriptors:invitationMapping andKeyPath:kKeyPath descriptorClass:0];

            [self.objectManager.HTTPClient  registerHTTPOperationClass:[AFHTTPRequestOperation class]];

            [self.objectManager postObject:invitation path:kKeyPath parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
            } 
            failure:^(RKObjectRequestOperation *operation, NSError *error) {
            }];

        }

     }

描述符:

-(void)setupDescriptors:(RKEntityMapping *)invitationMapping andKeyPath:(NSString *)keyPath descriptorClass:(BOOL)isTemplate
{

RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:[invitationMapping inverseMapping] objectClass:[Invite class] rootKeyPath:nil method:RKRequestMethodAny];

NSIndexSet *statusCodeSet = RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful);

RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:invitationMapping
                                                                                        method:RKRequestMethodGET
                                                                                   pathPattern:keyPath
                                                                                       keyPath:nil
                                                                                   statusCodes:statusCodeSet];

self.objectManager.requestSerializationMIMEType = RKMIMETypeJSON;
[self.objectManager addRequestDescriptor:requestDescriptor];
[self.objectManager addResponseDescriptor:responseDescriptor];

}

POST就好了。当我得到Response时,我从服务器返回为数据库中创建的对象分配的ID。

POSTING: @"inviteId" : @"0" @"activityId" : @"0"

作为回应,我将所有内容都恢复为POSTED,但服务器会发出Real ID。

RESPONSE: @"inviteId" : @"123456" @"activityId" : @"234567"

问题:

invite自动神奇地使用分配的ID更新对象;但是,活动对象在CoreData(.sqlite)中变为重复,其中一个副本将@"0"作为activityID,而其副本将具有指定的标识@"234567"

稍后当我GET, only the assigned Id object ( @&#34; 234567&#34; ) get updated. Objects with @&#34; 0&#34;`Id&#39; s永远不会被触及并留在核心数据中。< / p>

问题:如何避免重复记录?为什么这不会发生在invite对象

*更新*

返回JSON:

{
    "inviteId": 226,
    "meetingType": 2,
    "activities": [{
        "locationAddress": "4th Floor",
        "startTime": "2014-05-07T01:15:23Z",
        "activityId": 263,
        "latitude": "0",
        "longitude": "0",
        "meetupId": 226,
        "locationName": "West Conference Room",
        "oldId": -7360
    }, {
        "locationAddress": "123 Main St. ",
        "startTime": "2014-05-06T20:15:45Z",
        "activityId": 264,
        "latitude": "0",
        "longitude": "0",
        "meetupId": 226,
        "locationName": "Starwood Building",
        "oldId": -9325
    }],
    "comment": "Comments",
    "status": 0,
    "senderId": "Userid",
    "startDate": "2014-05-07T13:15:00Z"
}

activitiesInvite&amp;之间关系的名称。 Activity个实体

1 个答案:

答案 0 :(得分:1)

这是预期的(如果可能不是理想的)结果 - RestKit只能将响应映射到发送的对象(而不是它的关系)。

一个好的选择可能是使用获取请求块在以后查找和清除孤立活动对象(它不会在POST后运行,因此如果需要,您需要手动查找和删除立即删除死亡物品。)