gpt4 book ai didi

ios - RESKit : Duplicate objects are created

转载 作者:行者123 更新时间:2023-11-29 02:47:06 26 4
gpt4 key购买 nike

提取后数据库中出现重复项。

下面是映射和获取代码:

+(RKEntityMapping *)userMapping:(RKEntityMapping *)userMapping
{
[userMapping addAttributeMappingsFromDictionary:@{
@"userid" : @"userid",
@"firstName" : @"firstName",
@"lastName" : @"lastName",
}];
userMapping.identificationAttributes = @[@"userid"];

return userMapping;
}

获取代码:

AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
self.objectManager = [self getObjectManager];

self.objectManager.managedObjectStore = appDelegate.managedObjectStore;
self.managedObjectContext = self.objectManager.managedObjectStore.mainQueueManagedObjectContext;

[RKObjectManager sharedManager].requestSerializationMIMEType = RKMIMETypeJSON;

RKEntityMapping *userMapping = [RKEntityMapping mappingForEntityForName:@"User" inManagedObjectStore:self.objectManager.managedObjectStore];
userMapping = [RESTMappingProvider userMapping:userMapping];

NSIndexSet *statusCodeSet = RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful);
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:userMapping
method:RKRequestMethodGET
pathPattern:nil
keyPath:@"findUser" statusCodes:statusCodeSet];

NSMutableURLRequest *request = [self.objectManager.HTTPClient requestWithMethod:@"GET"
path:@"/findUser"
parameters:@{@"userid": userSearch
}];

//request.cachePolicy = NSURLRequestReloadIgnoringCacheData;

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

RKManagedObjectRequestOperation *operation = [[RKManagedObjectRequestOperation alloc]initWithRequest:request responseDescriptors:@[responseDescriptor]];
operation.managedObjectContext = self.objectManager.managedObjectStore.mainQueueManagedObjectContext;
operation.savesToPersistentStore = NO;

[operation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
NSError *error;
for (User *foundUser in mappingResult.array)
{
foundUser.lastUpdated = [NSDate date];
}
if ([self.managedObjectContext saveToPersistentStore:&error])
{
NSLog (@"******* User OBJECTS SAVED **********");
}

} failure:^(RKObjectRequestOperation *operation, NSError *error) {
});

}];

[operation start];
[operation waitUntilFinished];
}
}

日志:

2014-07-27 14:25:48.756 App[36854:5b3b] W restkit.core_data:RKManagedObjectMappingOperationDataSource.m:243 Performing managed object mapping with a nil managed object cache:
Unable to update existing object instances by identification attributes. Duplicate objects may be created.
2014-07-27 14:25:48.756 App[36854:3803] D restkit.object_mapping:RKPropertyInspector.m:130 Cached property inspection for Class 'User': {
email = {
isPrimitive = 0;
keyValueCodingClass = NSString;
name = email;
};
firstName = {
isPrimitive = 0;
keyValueCodingClass = NSString;
name = firstName;
};
lastName = {
isPrimitive = 0;
keyValueCodingClass = NSString;
name = lastName;
};
lastUpdated = {
isPrimitive = 0;
keyValueCodingClass = NSDate;
name = lastUpdated;
};
userid = {
isPrimitive = 0;
keyValueCodingClass = NSString;
name = userid;
};
}
2014-07-27 14:25:48.757 App[36854:5b3b] D restkit.object_mapping:RKMapperOperation.m:231 Asked to map source object {
firstName = Lois;
lastName = Lane;
userid = "lois.lane";
} with mapping <RKEntityMapping:0x12d78e80 objectClass=User propertyMappings=(
"<RKAttributeMapping: 0x12d819d0 userid => userid>",
"<RKAttributeMapping: 0x12d85f70 lastName => lastName>",
"<RKAttributeMapping: 0x12d72a10 firstName => firstName>",
)>

2014-07-27 14:25:49.476 App[36854:3803] W restkit.core_data:RKManagedObjectMappingOperationDataSource.m:243 Performing managed object mapping with a nil managed object cache:
Unable to update existing object instances by identification attributes. Duplicate objects may be created.
2014-07-27 14:25:49.477 App[36854:3803] D restkit.object_mapping:RKMapperOperation.m:231 Asked to map source object {
firstName = Lois;
lastName = Lane;
userid = "lois.lane";
} with mapping <RKEntityMapping:0xd6c17a0 objectClass=User propertyMappings=(
"<RKAttributeMapping: 0xd6b6ed0 userid => userid>",
"<RKAttributeMapping: 0xd6c86b0 lastName => lastName>",
"<RKAttributeMapping: 0xd6c1560 firstName => firstName>",
)>]

数据库:

enter image description here

如何避免重复?

编辑 1

- (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] fromSeedDatabaseAtPath:nil withConfiguration:nil options:[self optionsForSqliteStore] error:&error];
NSAssert(persistentStore, @"Failed to add persistent store with error: %@", error);

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

[self.managedObjectStore createManagedObjectContexts];

return self.managedObjectStore;
}

最佳答案

您需要创建缓存。这就是 RestKit 搜索和查找现有对象的方式,这是避免重复的机制。有 2 种类型或缓存,但您通常希望使用内存缓存(而不是获取缓存),因为它速度更快。

创建和设置缓存:

self.managedObjectStore.managedObjectCache = [[RKInMemoryManagedObjectCache alloc] initWithManagedObjectContext:managedObjectSyore.persistentStoreManagedObjectContext];

关于ios - RESKit : Duplicate objects are created,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24985937/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com