gpt4 book ai didi

ios - RestKit 0.20.2 中创建的重复 Core Data 对象

转载 作者:行者123 更新时间:2023-12-02 03:10:11 26 4
gpt4 key购买 nike

我遇到了一个问题,每次在 RestKit 0.20.2 中执行映射操作时,我都会对 SQLite 数据库进行重复写入。我在网上进行了广泛的查找,似乎找不到与我遇到的问题重复的问题 - 我见过的所有类似问题都集中在使用 RestKit 进行所有操作,而我仅使用 RestKit 进行映射和数据库操作,因为我正在另一个库中进行网络方面的工作。

我有一个唯一的 identificationAttribute 属性集(推文 ID),并且正在使用通过 RKInMemoryManagedObjectCache 初始化的 managedObjectCache;然而,每次执行映射操作时,我都会在所有内容上得到全面的重复。我用于网络端的库 (STTwitter) 将 JSON 作为数组返回,因此我迭代数组中的每个对象。我还应该执行其他操作吗?我的印象是,在进行任何插入之前,RestKit 会将映射对象中指定的 identificationAttribute 属性与 SQLite 数据库中已有的属性进行比较。当我使用 RKManagedObjectRequestOperation 将其用于另一个项目中的所有内容时,我没有遇到此问题。

这是我设置模型的方法:

-(void)setup
{
self.objectStore = [[RKManagedObjectStore alloc] initWithManagedObjectModel:[self managedObjectModel]];

[self.objectStore createPersistentStoreCoordinator];

NSString *path = [RKApplicationDataDirectory() stringByAppendingPathComponent:@"FoodTruckxxx.sqlite"];
NSLog(@"Setting up store at %@", path);
[self.objectStore addSQLitePersistentStoreAtPath:path
fromSeedDatabaseAtPath:nil
withConfiguration:nil
options:self.optionsForSQLiteStore
error:nil];

[self.objectStore createManagedObjectContexts];

self.objectStore.managedObjectCache = [[RKInMemoryManagedObjectCache alloc] initWithManagedObjectContext:self.objectStore.persistentStoreManagedObjectContext];
}

这是我执行映射操作的方法:

-(void)performMapping
{
int i = 0;
while (i < self.localCopyOfAllStatuses.count)
{
RKManagedObjectStore *store = [[FoodTruckDataModel sharedDataModel] objectStore];
RKEntityMapping *mapping = [ObjectMappings FoodTruckArticleMapping];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"FoodTruck" inManagedObjectContext:store.persistentStoreManagedObjectContext];
NSManagedObject *newManagedObject = [[NSManagedObject alloc] initWithEntity:entity insertIntoManagedObjectContext:store.persistentStoreManagedObjectContext];
RKManagedObjectMappingOperationDataSource *mappingDS = [[RKManagedObjectMappingOperationDataSource alloc] initWithManagedObjectContext:store.persistentStoreManagedObjectContext cache:store.managedObjectCache];

//assign new array to contain to just one object at a time and iterate through it
NSArray *justOneStatus = [self.localCopyOfAllStatuses objectAtIndex:i];
RKMappingOperation *operation = [[RKMappingOperation alloc] initWithSourceObject:justOneStatus destinationObject:newManagedObject mapping:mapping];
operation.dataSource = mappingDS;
NSError *error = nil;
[operation performMapping:&error];
[store.persistentStoreManagedObjectContext save:&error];
[store.persistentStoreManagedObjectContext saveToPersistentStore:&error];
i++;
}
}

这是映射:

+(RKEntityMapping *)FoodTruckArticleMapping
{
RKEntityMapping *jsonMapping = [RKEntityMapping mappingForEntityForName:@"FoodTruck" inManagedObjectStore:[[FoodTruckDataModel sharedDataModel] objectStore]];
jsonMapping.identificationAttributes = @[@"tweetID"];

[jsonMapping addAttributeMappingsFromDictionary:@{
@"text": @"tweet", @"user.screen_name": @"foodTruckName", @"created_at": @"timeStamp", @"id": @"tweetID"}];

return jsonMapping;
}

here是我日志中的完整操作。任何帮助将不胜感激!

最佳答案

根据此问题的答案on the RestKit Google Group board (我验证有效),解决方案是不创建 NSManagedObject并分配 nildestinationObject在我的RKMappingOperation声明。以下是此方法的正确代码,以及我最初问题的答案:

-(void)performMapping
{
int i = 0;
while (i < self.localCopyOfAllStatuses.count)
{
RKManagedObjectStore *store = [[FoodTruckDataModel sharedDataModel] objectStore];
RKEntityMapping *mapping = [ObjectMappings FoodTruckArticleMapping];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"FoodTruck" inManagedObjectContext:store.persistentStoreManagedObjectContext];
RKManagedObjectMappingOperationDataSource *mappingDS = [[RKManagedObjectMappingOperationDataSource alloc] initWithManagedObjectContext:store.persistentStoreManagedObjectContext cache:store.managedObjectCache];

//assign new array to contain to just one object at a time and iterate through it
NSArray *justOneStatus = [self.localCopyOfAllStatuses objectAtIndex:i];
RKMappingOperation *operation = [[RKMappingOperation alloc] initWithSourceObject:justOneStatus destinationObject:nil mapping:mapping];
operation.dataSource = mappingDS;
NSError *error = nil;
[operation performMapping:&error];
[store.persistentStoreManagedObjectContext save:&error];
[store.persistentStoreManagedObjectContext saveToPersistentStore:&error];
i++;
}
}

但是,RestKit Google Group 链接中也建议我实现 RKMapperOperation而不是RKMappingOperation,因为这样我就省去了遍历 JSON 响应数组的麻烦。这就是我最终选择的方案,为了提高效率和更好地使用 RestKit,我提出了这个方案:

-(void)performMapping
{
RKManagedObjectStore *store = [[FoodTruckDataModel sharedDataModel] objectStore];
RKManagedObjectMappingOperationDataSource *mappingDS = [[RKManagedObjectMappingOperationDataSource alloc] initWithManagedObjectContext:store.persistentStoreManagedObjectContext cache:store.managedObjectCache];
RKMapperOperation *mapperOperation = [[RKMapperOperation alloc] initWithRepresentation:self.localCopyOfAllStatuses mappingsDictionary:@{ [NSNull null]: [ObjectMappings FoodTruckArticleMapping] }];
mapperOperation.mappingOperationDataSource = mappingDS;
NSError *error = nil;
[mapperOperation execute:&error];
[store.persistentStoreManagedObjectContext save:&error];
[store.persistentStoreManagedObjectContext saveToPersistentStore:&error];
}

关于ios - RestKit 0.20.2 中创建的重复 Core Data 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17006652/

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