Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/104.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 使用FetchedResultsController使用TypeB表的多对多关系呈现TypeA对象_Ios_Core Data_Nsfetchedresultscontroller - Fatal编程技术网

Ios 使用FetchedResultsController使用TypeB表的多对多关系呈现TypeA对象

Ios 使用FetchedResultsController使用TypeB表的多对多关系呈现TypeA对象,ios,core-data,nsfetchedresultscontroller,Ios,Core Data,Nsfetchedresultscontroller,我知道在这方面也有类似的问题,但没有一个对我有用 我正在编写我的第一个iOS应用程序——一个简单的书店应用程序,其模型由一个Book表和一个User表组成。他们有一种称为purchasedBook的多对多关系 Book <--purchasedBooks--> User 这是我的尝试: NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"User"]; request.predicate =

我知道在这方面也有类似的问题,但没有一个对我有用

我正在编写我的第一个iOS应用程序——一个简单的书店应用程序,其模型由一个Book表和一个User表组成。他们有一种称为purchasedBook的多对多关系

Book <--purchasedBooks--> User
这是我的尝试:

NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"User"];
request.predicate = [NSPredicate predicateWithFormat:@"ALL purchasedBooks"];
request.sortDescriptors = @[];
self.fetchedResultsController = [[NSFetchedResultsController alloc]initWithFetchRequest:request
                                                                   managedObjectContext:managedObjectContext
                                                                     sectionNameKeyPath:nil
                                                                              cacheName:nil];

非常感谢

你是从错误的方向来的。您应该获取PurchasedBook实体,这些实体的用户与您引用的用户相同,然后获取关联的图书

请注意,我将PurchasedBook视为它自己的适当实体,而不仅仅是一个隐藏的联接表。您可以在PurchasedBook实体中添加其他元数据,如购买日期、价格等

NSFetchedResultsController的创建将是:

NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"PurchasedBook"];
[request setPredicate:[NSPredicate predicateWithFormat@"user == %@", [self currentUser]]];
[self setFetchedResultsController:[[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:managedObjectContext sectionNameKeyPath:nil cacheName:nil];
顺便说一句,实体名称应始终为单数,而不是复数。请注意此代码中的更改

使用此FetchedResultsController,您的单元格结构只会发生一点变化:

- (UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell* cell;
    //this should be using a static for the identifier. Magic strings are risky
    cell = [self.tableView dequeueReusableCellWithIdentifier:@"bookPlace" forIndexPath:indexPath];

    //You should make sure your cell is not nil just in case your magic string has a typo
    if (!cell) {
        //build it by hand here
    }

    // Get the book for this indexPath
    PurchasedBook* purchasedBook = [[self fetchedResultsController] objectAtIndexPath:indexPath];
    Book *book = [purchasedBook book];

    [[cell textLabel] setText:[book title]];
    [[cell detailedTextLabel] setText:[book subtitle]];

    return cell;
}
这有助于停止将核心数据对象视为表。核心数据不是数据库。恰好是您的对象模型能够持久化到数据库。持久性是核心数据的次要功能,而不是主要功能。一旦你在头脑中改变了这种观点,这些事情就会变得更容易解决

使现代化 但是,通过用户访问书籍不是更有效吗?用户可能有100本左右的书籍,而不是从图书表中将书籍与用户进行比较?其中可能有50000个对象

不,因为你想看的是书,而不是用户。如果您正在显示用户列表,那么您将获取用户。这是关于获取要显示的数据,即书本


书和用户之间的关系在问题中被描述为多对多。在这种情况下,谓词可能应该是ANY users==%@


正如马丁指出的那样,这是正确的。然而,我跳过了一步。在这样的情况下,您总会发现您需要更多关于用户与购买的图书年份、图书质量、支付的价格等之间关系的信息,并最终在用户与图书之间放置一个元对象。我在回答中添加它是出于习惯和经验,因为我怀疑你最终还是会添加它:

问题中描述了书籍和用户之间的关系。在这种情况下,谓词可能应该是ANY users==%@。啊!我明白了。这对我有用。但是,通过用户访问书籍不是更有效吗?用户可能有100本左右的书籍,而不是从图书表中将书籍与用户进行比较?其中可能有50000个对象?对我来说这并不重要,只是好奇而已。正如Martin所说,任何用户而不是用户==。
- (UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell* cell;
    //this should be using a static for the identifier. Magic strings are risky
    cell = [self.tableView dequeueReusableCellWithIdentifier:@"bookPlace" forIndexPath:indexPath];

    //You should make sure your cell is not nil just in case your magic string has a typo
    if (!cell) {
        //build it by hand here
    }

    // Get the book for this indexPath
    PurchasedBook* purchasedBook = [[self fetchedResultsController] objectAtIndexPath:indexPath];
    Book *book = [purchasedBook book];

    [[cell textLabel] setText:[book title]];
    [[cell detailedTextLabel] setText:[book subtitle]];

    return cell;
}