CoreData获取预取数据

时间:2018-05-06 20:59:24

标签: ios objective-c xcode core-data nsfetchrequest

我有一个包含SkuDetails的设备目录。我需要根据颜色,价格和存储来实现过滤。

获取我创建此功能的所有设备:

[[CoreDataManager sharedInstance] fetchWithEntity:@"DevicesCatalogue" Predicate:nil success:^(NSArray *fetchLists) {
        if(fetchLists) {
            NSMutableArray *filteredItems = [NSMutableArray new];
            NSSortDescriptor *valueDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
            filteredItems = (NSMutableArray *)[fetchLists sortedArrayUsingDescriptors:[NSArray arrayWithObject:valueDescriptor]];
            DeviceDetails *deviceDetails = [[DeviceDetails alloc]init];
            NSLog(@"first filter item ");
            deviceDetails.devices = filteredItems;
            successBlock(deviceDetails);
        }
        else {
            failureBlock(nil);
        }
    }failed:^(NSError *error) {
        failureBlock(error);
    }];

获取我创建的所有颜色:

+ (void)getAllColorsWithSuccess:(void (^)(NSArray *colorsList))successBlock
                         failureBlock:(void (^)(NSError * error))failureBlock  {


    [[CoreDataManager sharedInstance] fetchWithEntity:@"DevicesCatalogueSkuDetails" Predicate:nil success:^(NSArray *fetchLists) {
        if(fetchLists) {
            NSArray *colorsList  = [@[fetchLists,fetchLists] valueForKeyPath:@"@distinctUnionOfArrays.colorCode"];
            NSLog(@"%@",colorsList);

            successBlock(colorsList);
        }
        else {
            failureBlock(nil);
        }
    }failed:^(NSError *error) {
        failureBlock(error);
    }];
}

反之亦然我也增加了存储功能和价格。

对于过滤功能,我需要知道如何预取已经使用谓词获取的数据。

就像,为了获得价格范围之间的设备,我创建了这个函数,它只返回那些由价格组成的设备:

NSPredicate * pricePredicate =  [NSPredicate predicateWithFormat:@"ANY skuDetails.priceWithVat >= %f AND ANY skuDetails.priceWithVat <= %f",selectedSliderMinValue,selectedSliderMaxValue];
    [[CoreDataManager sharedInstance] fetchWithEntity:@"DevicesCatalogue" Predicate:pricePredicate success:^(NSArray *fetchLists) {
        if(fetchLists) {
            for (Devices *devices in fetchLists) {
                NSLog(@"name --- %@",devices.name);
            }
            [self resetAllArrays];
        }
    }failed:^(NSError *error) {

    }];

我的问题是,现在我获得了一系列设备,我如何预取这些设备以包含更多过滤器,如存储和颜色?是否可以重新获取已使用一个谓词获取的CoreData。

**

- 更新: **

对于过滤器我把这个功能但是这个基本上改变了params并再次调用,但我如何根据我的获取数据基于仅包含2个设备的价格来调用它。

-(void)getAllDevicesDetailBasedOnFilter{
    NSMutableArray *predicateArray = [NSMutableArray array];
    //DevicesCatalogueSkuDetails
    isColorSelected = YES;
    isBrandSelected = YES;
    isStorageSelected = YES;
    isPriceSelected = YES;
    if (isPriceSelected) {
        NSPredicate * pricePredicate =  [NSPredicate predicateWithFormat:@"ANY skuDetails.priceWithVat >= %.f AND ANY skuDetails.priceWithVat <= %.f",minimumPriceValue,maximumPriceValue];
        [predicateArray addObject:pricePredicate];
    }
    if (isBrandSelected) {
        NSPredicate * brandPredicate =  [NSPredicate predicateWithFormat:@"brand = %@", @"Samsung"];
        [predicateArray addObject:brandPredicate];
    }

    if (isColorSelected) {
        NSPredicate * colorPredicate = [NSPredicate predicateWithFormat:@"ANY skuDetails.color CONTAINS[cd] %@",@"Green"];
        [predicateArray addObject:colorPredicate];
    }

    if (isStorageSelected) {
        NSPredicate * colorPredicate = [NSPredicate predicateWithFormat:@"ANY skuDetails.storage CONTAINS[cd] %@",@"256"];
        [predicateArray addObject:colorPredicate];
    }

    NSPredicate *compoundPredicate =  [NSCompoundPredicate andPredicateWithSubpredicates:predicateArray];
    [[CoreDataManager sharedInstance] fetchWithEntity:@"DevicesCatalogue" Predicate:compoundPredicate success:^(NSArray *fetchLists) {
        if(fetchLists) {

        }

    }failed:^(NSError *error) {

    }];

}

enter image description here

0 个答案:

没有答案