CoreData 多个 Fe​​tch 请求

Posted

技术标签:

【中文标题】CoreData 多个 Fe​​tch 请求【英文标题】:CoreData multiple Fetch Request 【发布时间】:2018-05-07 20:06:12 【问题描述】:

我有一个核心数据模型和设备数据存储。每个设备都包含颜色、存储和价格。无论如何我可以过滤已经获取的请求(数据库中所有设备的数组)。我需要为包含颜色、存储和价格的设备实现过滤器功能。过滤器可以多次应用,这意味着一旦您为颜色应用过滤器,它只会显示那些包含这些颜色的设备+相应的存储,反之亦然。我可以使用谓词对其进行一次过滤,但要应用另一个过滤器,我需要再次调用查询以获取所有设备并应用受尊重的过滤器。有没有其他方法,所以当我从获取请求中获取过滤数据时,我可以对相同的过滤数据应用其他过滤器。或者我正在做的方式是处理核心数据的唯一方式。

这就是我根据所选过滤器过滤数据的方式:

-(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) 

    ];


【问题讨论】:

fetchLists 存储为本地属性,并将其用于第二个过滤器。 我该怎么做?你能详细说明一下吗? 【参考方案1】:

您可以使用fetchLists 的本地属性尝试类似的操作

@property NSMutableArray fetchLists;

- (NSPredicate *) getFirstPredicate 
    // create your initial predicate here


- (NSPredicate *) getSecondPredicate 
    // create your second predicate here


- (void) fetchEntities 
    [[CoreDataManager sharedInstance] fetchWithEntity: @"DevicesCatalogue" predicate: nil 
        success: ^(NSArray *result) 
            if(result) 
                self.fetchLists = result;
            
         
        failed: ^(NSError *error) 
            // handle error
    ];

然后从您想要获取和过滤数据的任何地方执行以下操作:

[self fetchEntities];
NSArray *firstFilteredArray = [self.fetchLists filteredArrayUsingPredicate: getFirstPredicate];
NSArray *secondFilteredArray = [firstFilteredArray filteredArrayUsingPredicate: getSecondPredicate];

等等

或者为您的第一个和第二个谓词使用复合词来过滤原始fetchLists

【讨论】:

以上是关于CoreData 多个 Fe​​tch 请求的主要内容,如果未能解决你的问题,请参考以下文章

发出多个 Axios 请求导致 CORS 错误

是否可以手动预检多个 CORS 资源?

在 Angular+ 节点中实现 CORS

多个 CORS 适用于 GET 但不适用于 PUT/POST 与飞行前请求 Web api 2

来自多个来源的 CORS

使用具有多个来源的 PUT 和 DELETE 请求时如何解决 ASP.NET Web API CORS Preflight 问题?