【发布时间】: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>)"
为什么我有重复?请帮忙:(
【问题讨论】:
标签: ios objective-c core-data restkit