fetchedResultsController 方法中的 NSPredicate 问题

发布于 2024-11-09 13:58:36 字数 2141 浏览 0 评论 0原文

我正在创建一个应用程序,让销售人员可以通过 iPhone 为客户订购库存。

用户导航到客户并创建订单。将出现一个空白表格视图,然后用户通过从库存屏幕中选择项目将其添加到表格视图中。

当他们向订单添加项目时,导航控制器会弹出视图并再次显示订单视图。用户应该只能看到该客户的订单。

我最初完全在 sqlite 中构建了该应用程序,并通过使用现在已转移到 Core Data 的查询来实现这一点

SELECT PRODUCT FROM TRANSLINE WHERE CUSTOMERACCNO = ?

,并且我需要实现相同的功能。我试图使用 NSPredicate 在 fetchedResultsController 方法中实现此行为,但我似乎无法让它工作 - 我得到的只是一个空白屏幕。但是,当我不实施它时,我会收到所有订单,即每个客户的订单,而不仅仅是这个客户的订单。

这是我的代码:

- (NSFetchedResultsController *)fetchedResultsController
{
    if (__fetchedResultsController != nil) {
        return __fetchedResultsController;
    }

    // Create the fetch request for the entity.
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    // Configure the Entity
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"TransLine" inManagedObjectContext:__managedObjectContext];
    [fetchRequest setEntity:entity];

    [fetchRequest setFetchBatchSize:20];

    //Configure the predicate
    [NSFetchedResultsController deleteCacheWithName:@"Root"];  
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"customerAccountNo == %@", _customerAccountNo];
    [fetchRequest setPredicate:predicate];

    //Configure Sort Descriptors
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"PRODAC" ascending:YES];
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
    [fetchRequest setSortDescriptors:sortDescriptors];


    // nil for section name key path means "no sections".
    NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:__managedObjectContext sectionNameKeyPath:nil cacheName:@"Root"];
    aFetchedResultsController.delegate = self;

    self.fetchedResultsController = aFetchedResultsController;

    [aFetchedResultsController release];
    [fetchRequest release];
    [sortDescriptor release];
    [sortDescriptors release];

    return __fetchedResultsController;
}  

提前感谢您的所有帮助。

I'm creating an app that lets a salesperson order stock for their customers from their iPhone.

The user navigates to a customer and creates an order. A blank tableview appears and the user then adds items to the tableview by selecting them from an inventory screen.

When they add an item to the order, the navigation controller pops the view and shows the order view again. The user should only be able to see orders for that customer.

I originally built the app entirely in sqlite and I achieved this by using the query

SELECT PRODUCT FROM TRANSLINE WHERE CUSTOMERACCNO = ?

I have now moved onto Core Data and I need to achieve the same functionality. I'm trying to implement this behaviour in the fetchedResultsController method using NSPredicate, but I can't seem to get it working - all I get is a blank screen. However, when I don't implement it, I get ALL orders i.e. orders for every customer, not just this one.

Here's my code :

- (NSFetchedResultsController *)fetchedResultsController
{
    if (__fetchedResultsController != nil) {
        return __fetchedResultsController;
    }

    // Create the fetch request for the entity.
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    // Configure the Entity
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"TransLine" inManagedObjectContext:__managedObjectContext];
    [fetchRequest setEntity:entity];

    [fetchRequest setFetchBatchSize:20];

    //Configure the predicate
    [NSFetchedResultsController deleteCacheWithName:@"Root"];  
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"customerAccountNo == %@", _customerAccountNo];
    [fetchRequest setPredicate:predicate];

    //Configure Sort Descriptors
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"PRODAC" ascending:YES];
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
    [fetchRequest setSortDescriptors:sortDescriptors];


    // nil for section name key path means "no sections".
    NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:__managedObjectContext sectionNameKeyPath:nil cacheName:@"Root"];
    aFetchedResultsController.delegate = self;

    self.fetchedResultsController = aFetchedResultsController;

    [aFetchedResultsController release];
    [fetchRequest release];
    [sortDescriptor release];
    [sortDescriptors release];

    return __fetchedResultsController;
}  

Thanks in advance for all your help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小红帽 2024-11-16 13:58:36

好吧,看来在我的挫败感中……我实际上是个白痴。猜猜我忘了包括什么。

    NSError *error = nil;
    if (![self.fetchedResultsController performFetch:&error])
    {
      NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }

Well, it seems that in my frustration.... I am, in fact, an idiot. Guess what I forgot to include.

    NSError *error = nil;
    if (![self.fetchedResultsController performFetch:&error])
    {
      NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文