代码之家  ›  专栏  ›  技术社区  ›  Rits

一些排序问题(核心数据初学者)

  •  1
  • Rits  · 技术社区  · 16 年前

    也许有一个简单的解决方案,但我很头疼,我对所有这些核心数据都相当陌生:

    我有一个BankAccount类/实体,它有一个“index”属性,用于排序,还有一个与事务类/实体的“transactions”到many关系。这个事务实体有一个“date”属性,我也想用它来排序。

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    [fetchRequest setEntity:[NSEntityDescription entityForName:@"BankAccount" inManagedObjectContext:self.managedObjectContext]];
    NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"index" ascending:YES];
    [fetchRequest setSortDescriptors:[NSArray arrayWithObject:sortDescriptor]];
    

    非常感谢。

    1 回复  |  直到 16 年前
        1
  •  1
  •   vfn    16 年前

    为此,您需要获取所订购的事务。

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    [fetchRequest setEntity:[NSEntityDescription entityForName:@"Transaction"
                                        inManagedObjectContext:self.managedObjectContext]]; 
    [fetchRequest setPropertiesToFetch :[NSArray arrayWithObjects:@"date", @"bankAccount", nil]];
    [fetchRequest setSortDescriptors:[NSArray arrayWithObjects:
                                      [[[NSSortDescriptor alloc] initWithKey:@"date"
                                                                   ascending:YES
                                                                    selector:@selector(compare:)] autorelease],
                                      [[[NSSortDescriptor alloc] initWithKey:@"bankAccount.index"
                                                                   ascending:YES] autorelease],
                                      nil]];
    

    我是这么想的 Transaction 是交易实体的名称 bankAccount 交易 BankAccount .