问题描述
以下是我的设置:
- (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 ::
POST Invite -- Invite has a to-many relationship with 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。
I POST
just fine. When I get a Response
, I get the assigned ID's, for the created objects in the database, back from the server.
POSTING:
@inviteId:@0@activityId: @0
作为回应,我将所有内容都恢复为POSTED,但服务器会发出真实ID。
In response, I get everything back as POSTED, however, the server issues Real IDs.
回应:
@inviteId:@123456@activityId: @234567
问题:
邀请
使用自动神奇的指定ID更新对象;但是,活动对象在CoreData(.sqlite)中变为重复,其中一个副本将具有 @0
作为 activityID
,而其副本将具有指定的Id @234567
invite
Object gets updated with the assigned ID auto-magically; however, the activity objects become duplicates in CoreData (.sqlite) with one of the duplicate will have @"0"
as the activityID
, while its duplicate will have the assigned Id @"234567"
稍后当我 GET,只有指定的Id对象(
@234567)得到更新。具有
@0的对象`Id'永远不会被触及并保留在Core Data中。
Later when I GET, only the assigned Id object (
@"234567") get updated. Objects with
@"0"` Id's are never touched and remain in Core Data.
问题:如何避免重复记录以及为什么这不会发生在邀请
对象
Question: How can I avoid the duplicate record and why is this not happening to the invite
Object
*更新*
返回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"
}
活动
是邀请
&的关系名称。 活动
实体
activities
is the name of the relationship between Invite
& Activity
entities
推荐答案
这是预期的(如果不是很理想)结果 - RestKit只能将响应映射到发送的对象(而不是它的关系)。
This is the expected (if perhaps not ideal) outcome - RestKit is only able to map the response onto the sent object (not its relationships).
一个好的选择可能是使用获取请求块来查找和清除以后的孤儿活动对象(它不会在POST后运行,所以如果你需要立即删除死对象,你需要手动查找和删除。)
A good option could be to use fetch request blocks to find and purge the orphan activity objects at a later date (it wouldn't run after the POST, so you would need to manually find and delete if you needed the dead objects removed immediately).
这篇关于RESTKit:POST后关系中的重复对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!