【发布时间】:2014-02-12 21:57:46
【问题描述】:
在从服务器获取对象列表时,我遇到了 RestKit 0.20 在我的数据库中创建重复项的问题。我在本地创建并发送到服务器的对象在保存映射结果时不会更新,而是被复制。我认为它与这个错误有关:https://github.com/RestKit/RestKit/issues/1613,所以我正在寻找临时修复,因为我必须尽快发货。当结果返回时
[myRKObjectManager getObjectsAtPath:path parameters:params success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult)
我可以在执行之前从中剔除重复项
[myRKObjectManager.managedObjectStore.mainQueueManagedObjectContext saveToPersistentStore:&error])
?我已经浏览了文档,但不了解如何剔除不需要的插入。
更新:@Wain 的答案:
这样设置:
myRKobjectManager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:urlString]];
NSManagedObjectModel *managedObjectModel = [NSManagedObjectModel mergedModelFromBundles:nil];
RKManagedObjectStore *managedObjectStore = [[RKManagedObjectStore alloc] initWithManagedObjectModel:managedObjectModel];
NSError *error;
NSPersistentStore *persistentStore = [managedObjectStore addSQLitePersistentStoreAtPath:[RKApplicationDataDirectory() stringByAppendingPathComponent:[self databaseFilename]] fromSeedDatabaseAtPath:nil withConfiguration:nil options:@{NSMigratePersistentStoresAutomaticallyOption:@YES, NSInferMappingModelAutomaticallyOption:@YES} error:&error];
NSAssert(persistentStore, @"Failed to add persistent store with error: %@", error);
// Create the managed object contexts
[managedObjectStore createManagedObjectContexts];
// Configure a managed object cache to ensure we do not create duplicate objects
managedObjectStore.managedObjectCache = [[RKInMemoryManagedObjectCache alloc] initWithManagedObjectContext:managedObjectStore.persistentStoreManagedObjectContext];
// Set up response routing
RKEntityMapping *partyMapping = [Party objectMappingInManagedObjectStore:managedObjectStore withCustomerMapping:customerMapping notificationMapping:notificationMapping];
RKResponseDescriptor *partyResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:partyMapping method:RKRequestMethodGET pathPattern:@"api/allparties" keyPath:@"parties" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[[RKObjectManager sharedManager] addResponseDescriptor:partyResponseDescriptor];
实体类被声明:
@interface Party : NSManagedObject<PostRequestDelegate>
使用类别
NSManagedObject (ActiveRecord)
派对对象是使用
创建的Party *newParty = [Party object];
并通过以下方式保存在数据库中:
[myRKobjectManager.managedObjectStore.mainQueueManagedObjectContext saveToPersistentStore:&error]
使用的映射:
RKEntityMapping* partyMapping = [RKEntityMapping mappingForEntityForName:@"Party" inManagedObjectStore:managedObjectStore];
[partyMapping addAttributeMappingsFromDictionary:@{
@"added_date": @"addedDate",
@"id": @"partyId",
@"modified_date": @"modifiedDate",
@"name": @"name",
}];
partyMapping.identificationAttributes=@[@"addedDate"];
[partyMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"customer" toKeyPath:@"customer" withMapping:customerMapping]];
[partyMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"notification_history" toKeyPath:@"notifications" withMapping:notificationMapping]];
【问题讨论】:
-
您使用什么映射?您是否在使用多种环境?您如何保存本地创建的对象?
-
@Wain 我已经用更多设置信息更新了帖子。谢谢!
-
为什么要创建自己的上下文而不是让 RestKit 创建它们?
-
我从 RestKit 代码中复制了它,选择器:[managedObjectStore createManagedObjectContexts];所以我让 RestKit 创建它们,是的。
-
那么您是否设置添加到您创建的对象的日期并保存它们?还是您要发布对象?
标签: ios core-data restkit restkit-0.20