RestKit 在缓存中重复
Posted
技术标签:
【中文标题】RestKit 在缓存中重复【英文标题】:RestKit duplicates in cache 【发布时间】:2014-11-23 13:24:11 【问题描述】:我的 cahce 中有一些奇怪的重复项
我的数据模型是
我得到这个 json 输入:
"pages": 1,
"salads": [
"id": 374,
"img": "http://salatiki.com.ua/images/mini/20120114_457.jpg",
"ingredCount": 5,
"ingredients": "курятина, яблоко, сыр твёрдый, икра красная, майонез",
"my_favorite": 1,
"name": "Сердце",
"rating": 5
,
"id": 336,
"img": "http://salatiki.com.ua/images/mini/20111229_2110.jpg",
"ingredCount": 6,
"ingredients": "креветки, салат, помидор, чеснок, майонез, сметана",
"my_favorite": 1,
"name": "Итальянский с креветками",
"rating": 5
]
数组“沙拉”可以在更少的对象上比示例中包含更多
这是我的日志:
Fetch result: (
"<SSaladCards: 0x797437b0> (entity: SSaladCards; id: 0x79635760 <x-coredata://9277267C-0FAA-4E7B-BCE2-157DA4CF9D34/SSaladCards/p13> ; data: \n sPages = 1;\n salads = (\n \"0x796529e0 <x-coredata://9277267C-0FAA-4E7B-BCE2-157DA4CF9D34/SSaladsCardData/p2>\",\n \"0x79d613f0 <x-coredata://9277267C-0FAA-4E7B-BCE2-157DA4CF9D34/SSaladsCardData/p1>\"\n );\n)",
"<SSaladCards: 0x79d438a0> (entity: SSaladCards; id: 0x79d59900 <x-coredata://9277267C-0FAA-4E7B-BCE2-157DA4CF9D34/SSaladCards/p1> ; data: <fault>)",
"<SSaladCards: 0x79d67830> (entity: SSaladCards; id: 0x79d439a0 <x-coredata://9277267C-0FAA-4E7B-BCE2-157DA4CF9D34/SSaladCards/p3> ; data: <fault>)",
"<SSaladCards: 0x79d43930> (entity: SSaladCards; id: 0x79d439b0 <x-coredata://9277267C-0FAA-4E7B-BCE2-157DA4CF9D34/SSaladCards/p4> ; data: <fault>)",
....
我的代码: 我的商店和经理是在 appDelegate 中分配的,这些代码我在第一个 ViewController 中使用,所以我调用 sharedManager 和 defaultStore
RKEntityMapping *saladPageMapping = [RKEntityMapping mappingForEntityForName:NSStringFromClass([SSaladCards class]) inManagedObjectStore:[RKManagedObjectStore defaultStore]];
[saladPageMapping addAttributeMappingsFromDictionary:@ @"pages" : @"sPages", ];
saladPageMapping.identificationAttributes = @[@"sPages"];
RKEntityMapping *saladsBodyMapping = [RKEntityMapping mappingForEntityForName:NSStringFromClass([SSaladsCardData class]) inManagedObjectStore:[RKManagedObjectStore defaultStore]];
[saladsBodyMapping addAttributeMappingsFromDictionary:@ @"id" : @"sId",
@"img" : @"sImage",
@"name" : @"sName",
@"rating" : @"sRating",
@"ingredients" : @"sIngredients",
@"ingredCount" : @"sIngredientsCount",
@"my_favorite" : @"sFavorites" ];
saladsBodyMapping.identificationAttributes = @[@"sId"];
[saladPageMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"salads"
toKeyPath:@"salads"
withMapping:saladsBodyMapping]];
RKResponseDescriptor *saladPageDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:saladPageMapping
method:RKRequestMethodGET
pathPattern:nil
keyPath:nil
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[[RKObjectManager sharedManager] addResponseDescriptorsFromArray: @[saladPageDescriptor]];
NSURLRequest *salRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://salatiki.com.ua/api/get.php?getByCat=-1&page=1&ukey=%@", [keychainWrapper objectForKey:(__bridge id)(kSecValueData)]]]];
RKManagedObjectRequestOperation *saladOperation = [[RKManagedObjectRequestOperation alloc] initWithRequest:salRequest responseDescriptors:@[saladPageDescriptor]];
saladOperation.managedObjectContext = [RKManagedObjectStore defaultStore].mainQueueManagedObjectContext;
saladOperation.managedObjectCache = [RKManagedObjectStore defaultStore].managedObjectCache;
NSOperationQueue *operationQueue = [NSOperationQueue new];
[operationQueue addOperation:saladOperation];
我如何获取对象
-(NSFetchedResultsController *)saladCardFetcehdResController
if (_saladCardFetcehdResController != nil)
return _saladCardFetcehdResController;
NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:NSStringFromClass([SSaladCards class])];
NSSortDescriptor *pages = [[NSSortDescriptor alloc] initWithKey:@"sPages" ascending:YES];
[fetchRequest setSortDescriptors:@[pages]];
[fetchRequest setReturnsObjectsAsFaults:NO];
[fetchRequest setFetchBatchSize:20];
self.saladCardFetcehdResController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:[RKManagedObjectStore defaultStore].mainQueueManagedObjectContext sectionNameKeyPath:nil cacheName:@"Salad"];
self.saladCardFetcehdResController.delegate = self;
NSError *error = nil;
if (![self.saladCardFetcehdResController performFetch:&error])
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
else
NSLog(@"Salad Fetch count: %i",[[self.saladCardFetcehdResController fetchedObjects] count]);
NSLog(@"Salad Fetch result: %@",[self.saladCardFetcehdResController fetchedObjects]);
return _saladCardFetcehdResController;
每次我向服务器发出请求时,它都会向我的缓存中添加一个“故障”对象
"<SSaladCards: 0x79d438a0> (entity: SSaladCards; id: 0x79d59900 <x-coredata://9277267C-0FAA-4E7B-BCE2-157DA4CF9D34/SSaladCards/p1> ; data: <fault>)"
为什么我有重复?请帮忙:(
【问题讨论】:
【参考方案1】:在 FRC 上使用缓存可能没有帮助,尽管它可能不是导致此问题的原因。
我猜你没有配置获取请求或内存中managedObjectCache
(供RKManagedObjectStore
使用),因为这是允许RestKit在数据存储中查找现有项目并更新它们的机制的创建新实例。
【讨论】:
谢谢,如何配置获取请求? 您查看过managedObjectCache
上RKManagedObjectStore
的文档吗?
是的,我的 managedObjectCache 不为零,我不明白为什么我有重复,因为我的应用程序中的相同代码工作正常
我的意思是我项目中的类似代码工作正常,我会在这些天创建一个示例项目并写信给你,也许如果你显示所有代码你可以帮助我。谢谢。以上是关于RestKit 在缓存中重复的主要内容,如果未能解决你的问题,请参考以下文章
RestKit + CoreData:从CoreData缓存中排除某些对象
更新 RestKit:loadObjectsAtResourcePath:usingBlock:不要将对象保存在缓存中