RestKit在缓存中重复

时间:2014-11-23 13:24:12

标签: ios objective-c core-data restkit

我的cahce中有一些奇怪的副本

我的数据模型是

data model

我得到了这个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;
}

每次我向服务器发出请求时,都会向我的缓存添加一个&#34; fault&#34;对象

"<SSaladCards: 0x79d438a0> (entity: SSaladCards; id: 0x79d59900 <x-coredata://9277267C-0FAA-4E7B-BCE2-157DA4CF9D34/SSaladCards/p1> ; data: <fault>)"

为什么我有重复?请帮助:(

1 个答案:

答案 0 :(得分:0)

在FRC上使用缓存不太可能有所帮助,但可能不是导致此问题的原因。

我猜你还没有配置一个获取请求或内存managedObjectCache(由RKManagedObjectStore使用),因为这是允许RestKit在数据存储中查找现有项的机制并更新它们而不是创建新实例。