RestKit RKObjectManager:getObjectsAtPath - custom processing



从服务器获取对象列表时,我在数据库中创建重复项时遇到 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]];

您是否尝试过将标识属性更改为派对 ID?我假设这是每个派对对象的唯一属性。然后,Restkit 将检查核心数据中是否已经存在具有相同 partyId 的对象并对其进行更新,而不是创建新对象。

阅读 http://restkit.org/api/latest/Classes/RKManagedObjectRequestOperation.html 文档中的"实体标识"部分,了解有关其工作原理的更多详细信息。

最新更新