fetchedResultsController 方法中的 NSPredicate 问题
我正在创建一个应用程序,让销售人员可以通过 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,看来在我的挫败感中……我实际上是个白痴。猜猜我忘了包括什么。
Well, it seems that in my frustration.... I am, in fact, an idiot. Guess what I forgot to include.