Core data RESTKit:POST后在关系中复制对象
以下是我的设置:Core data RESTKit:POST后在关系中复制对象,core-data,ios7,restkit-0.20,Core Data,Ios7,Restkit 0.20,以下是我的设置: - (RKManagedObjectStore *)setupCoreDataWithRESTKit { NSError * error; NSURL * modelURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"App" ofType:@"momd"]]; NSManagedObjectModel * managedObjectModel = [[[NSMana
- (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 Invite--Invite与活动之间存在多对多关系:
-(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
很好。当我得到响应时,我会从服务器返回数据库中已创建对象的分配ID
发布:
@“inviteId”:@“0”@“activityId”:@“0”
作为回应,我将所有内容都恢复到发布状态,但是,服务器会发出真实的ID
响应:
@“inviteId”:@“123456”@“activityId”:@“234567”
问题:
invite
对象自动神奇地使用分配的ID更新;但是,活动对象在CoreData(.sqlite)中成为重复项,其中一个重复项将具有@“0”
作为活动Id
,而其重复项将具有分配的Id@“234567”
稍后当我获取时,只有分配的Id对象(
@“234567”)得到更新。具有
@“0”`Id的对象永远不会被触摸,并保留在核心数据中
问题:如何避免重复记录?为什么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"
}
活动
是邀请
和活动
实体之间关系的名称这是预期(如果可能不理想)结果-RestKit只能将响应映射到发送的对象(而不是其关系)
一个好的选择可能是在以后使用fetch请求块来查找和清除孤立的活动对象(它不会在发布后运行,因此如果需要立即删除死对象,则需要手动查找和删除)。JSON在不起作用时是否返回数组?显示JSON和跟踪日志。是的……Invite是一个字典,“活动”是一个包含“活动”对象的数组。我用返回的JSON更新了这个问题。非常感谢。因此,您正在发布一个带有嵌套活动的邀请,并使用标识符返回所有这些对象(并且只有邀请得到更新,而不是活动)-正确吗?是的。活动对象不会更新,而是作为副本创建。谢谢。什么时候运行?在成功块内?获取分配了@“0”ID的所有项目并在帖子的成功块中删除它们不是更容易吗?是的,这是手动选项。没有比单独获取更多的代码。只需记住,之后需要显式保存。从技术上讲,您可以在发出请求后立即删除它们,但成功块很容易触发…最后一个问题:我正在使用self.object管理器。我知道使用RKManagedObjectRequestOperation可以执行操作。saveStoperistentStore=NO)。我希望我可以用ObjectManager做同样的事情,一旦我从上下文中删除@“0”id,我就可以将它保存到localStore?我认为你做不到(尽管我没有尝试/检查)。它在后台线程上运行,因此很难实现(安全/正确)。