NSFetchedResultsController 在排序时检索特定数据

NSFetchedResultsController retrieval of specific data during sorting

我创建了一个 NSFetchedResultsController 对象并用核心数据实体(称为 "AssetsPartners")的数据加载了它。这个实体有许多不同的属性,例如 nametypephoto 等。目前,我在数据获取期间使用 name 属性,这会带回所有内容。现在我需要能够在按 'type' 属性按下 3 个 UISegmentedControl 按钮之一时对 NSFetchedResultsController 中的内容进行排序。 type 属性仅包含 3 个可能的值:all、asset & counterparty。索引 0 = "All",索引 1 = "Assets",索引 2 = "Counterparty"。因此,当用户单击索引 1 时,例如,它是“Assets”,我只想在 table 中显示该数据之前从我的 NSFetchedResultsController 中提取该类型的数据看法。我该怎么做?

以下是我创建 NSFetchedResultsController 的方法:

-(NSFetchedResultsController *)fetchedResultsController
{
    //Is there already anything in that instance variable? Not equalling to nil means there's something alreayd there. Just return it & we're done.
    if (_fetchedResultsController != nil)
    {
        return _fetchedResultsController;
    }


    //IF we make it past the previous if statement, we'll have to make the previous object.
    //We need a NSFetchRequest & a sort descriptor to create a NSFetchedResultsContoller.

    NSFetchRequest *fetchRequest    = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity     = [NSEntityDescription entityForName:@"AssetsPartners" inManagedObjectContext:[AppDelegate shared].managedObjectContext];
    [fetchRequest setEntity:entity];


    //Specify how the fetched objects should be sorted.
    NSSortDescriptor *assetName = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES]; //type //Sorts data by name. In this case by "name"'s first letter of the alphabet.
    [fetchRequest setSortDescriptors:@[assetName]]; //@[fetched, assetType, dateSD];


    _fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
                                                                    managedObjectContext:[AppDelegate shared].managedObjectContext
                                                                      sectionNameKeyPath:nil    //'nil' brings data back in one long list. But writing "author", for instance, would split data by work written by each different author.
                                                                               cacheName:nil];  //Not sure what this does...


    _fetchedResultsController.delegate = self; //"Look to me for any of your delegate methods".

    return _fetchedResultsController;
}

获得数据后,我将其放入 table 视图:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //Has to be here to be accessible by any object that requires it regarding of scope.
    AssetsPartners *items;


    //Segmented control stuff. Simply filter fetched array data rather than creating everything cell-related from scratch.
    if(_segmentedControl.selectedSegmentIndex == 0)
    {
        items = [_fetchedResultsController objectAtIndexPath:indexPath]; //Ask '_fetchedResultsController' what's at this particular indexPath right now. Also alphabetically sorts the array list.
    }
    else if(_segmentedControl.selectedSegmentIndex == 1)
    {
        //Sorts table view alphabetically.
        //[self.elements sortUsingDescriptors:@[[NSSortDescriptor sortDescriptorWithKey:@"lastName" ascending:YES]]];
    }
    else
    {

    }




    CustomTableCell *aCell = (CustomTableCell *)[tableView dequeueReusableCellWithIdentifier:@"CustomCell"]; //Cells are dequeued & reused.
    if (aCell == nil)
    {
        aCell = [[CustomTableCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"CustomCell"];


        UILabel *label  = [[UILabel alloc] initWithFrame:CGRectMake(65.0, 15.0, 150.0, 30.0)];
        label.textColor = [UIColor whiteColor];
        label.tag       = 999; //Neccessary to have this to by-pass scope & access this label outside this if statement (Chosen number is random).
        [aCell.contentView addSubview:label];




        //Entirely new UIImageView for every UITableViewCell (to tweak the frame size & position).
        UIImageView *newImgView = [[UIImageView alloc] initWithFrame:CGRectMake(10.0, 15.0, 35.0, 35.0)];
        newImgView.tag          = 1000;
        [aCell.contentView addSubview:newImgView];
    }


    //Makes sure the separator doesn't have that little gap in the beginning.
    //NOTE: This MUST be here and "_tableView.separatorInset = UIEdgeInsetsZero;" must exist inside [self extraTableViewParameter]!
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
    {
        aCell.layoutMargins                     = UIEdgeInsetsZero;
        aCell.preservesSuperviewLayoutMargins   = NO;
    }




    /*CELL DETAILS.*/
    aCell.backgroundColor           = [UIColor clearColor];
    aCell.textLabel.textColor       = [UIColor clearColor];
    aCell.detailTextLabel.textColor = [UIColor lightGrayColor];


    /*FILL CELL WITH DATA.*/
    aCell.textLabel.text        = @"";
    NSInteger anInteger         = [items.balance integerValue];
    aCell.detailTextLabel.text  = [NSString stringWithFormat:@"%@ P", [self addWhitespacesTo:anInteger]];


    UILabel *label  = (UILabel *)[aCell.contentView viewWithTag:999]; //Casting UILabel to access the previously created one.
    label.text      = items.name;


    //1 - Reduce 'photo' characters to bare essentials. Which is just an image name.
    NSString *iconName      = [self cutOutString:@"bizzer://" fromString:items.photo]; //Get rid of "bizzer://" from string value.

    //2 - Utilize previously created UIImageView for this cell with the image name.
    UIImageView *imgView    = (UIImageView *)[aCell.contentView viewWithTag:1000];
    imgView.image           = [UIImage imageNamed:iconName];
    [self turnIcon:imgView toColor:items.color];

    return aCell;
}

您可以更新与 NSFetchResultController 关联的 fetchRequest NSPredicate。这是示例代码。

-(void)updateFetchRequestForType:(NSString *)type
{
    [NSFetchedResultsController deleteCacheWithName:@"Root"];  

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"type == %@",type];
    [_fetchedResultsController.fetchRequest setPredicate:predicate];
    NSError *error = nil;
    if (![[self fetchedResultsController] performFetch:&error]) {   
    NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
    abort();
    }
    [table reloadData];
}

注意:在您的代码中初始化 NSFetchResultController 时,将您的缓存名称设置为 Root

(这不是一个好的解决方案,但我把它留在这里让其他人了解原因并免除他们的挫败感)。

我是这样解决的。我敏锐地意识到这不是最优雅的解决方案,但它对我有用。虽然可能不太理想,但我正在努力提供帮助,我非常愿意学习更好的技术。

为你的 NSPredicate 创建一个 属性 并给它一个默认值(比如 "All" 或任何适合你的值)。

  • 将谓词分配给您的 fetchedResultsController。

为您的分段控件创建一个操作,它在值更改时执行以下操作:

  • 将新的期望值分配给您的 NSPredicate
  • 将 fetchedResultsController 设置为 nil (self.fetchedResultsController = nil;)
  • 调用 fetchedResultsController ([self fetchedResultsController];)
  • 重新加载数据 ([self.tableView reloadData];)