I seem to be getting an error I can’t really understand. Hopefully this doesn’t get too confusing and longwinded, but as I can’t seem to find any answers in my searches, I think it might be necessary. In my program, I have several entities:
ItemSold
Invoice
Customer
Customer‘s can have many Invoices, Invoices can have many ItemSolds.
In my Interface Builder, I have three tables. One table represents customers, another represents invoices, and the last one represents itemsolds in that invoice.
So as you select a customer, their invoices show up in the invoice table. Once you click a invoice, the items on that invoice show up in the last table.
I seem to be getting the error when I select on a table. Interestingly, in another view, I can do this fine (when I’m trying to sort Items based on their relationship to Type). However I can’t get it to work here. This is my fetch to get the items that should be stored:
if (managedObjectContext == nil)
{
managedObjectContext = [(HistoryController *)[[NSApplication sharedApplication] delegate] managedObjectContext];
}
NSError *error = nil;
// fetch all customers
NSFetchRequest *fetchRequest2 = [[NSFetchRequest alloc] init];
NSEntityDescription *entity2 = [NSEntityDescription entityForName:@"Customer"
inManagedObjectContext:managedObjectContext];
[fetchRequest2 setEntity:entity2];
fetchedCustomers = [managedObjectContext executeFetchRequest:fetchRequest2 error:&error];
if (fetchedCustomers == nil) {
NSLog(@"ERROR");
}
[fetchRequest2 release];
// end of customer fetch
// fetch all invoices
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Invoice"
inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];
NSLog(@"A");
if ([customerTable selectedRow] >= 0)
{
NSLog(@"B");
int index = (int)[customerTable selectedRow];
NSLog(@"%@", index);
NSString *customerString = [[fetchedCustomers objectAtIndex:(index)] valueForKey:@"CustomerNumber"];
NSNumber *customerNumber = [NSNumber numberWithInt:([customerString intValue])];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"customer.CustomerNumber like %@", customerNumber];
[fetchRequest setPredicate:predicate];
}
fetchedInvoices = [managedObjectContext executeFetchRequest:fetchRequest error:&error];
if (fetchedInvoices == nil) {
NSLog(@"ERROR");
}
[fetchRequest release];
// end of invoice fetch
// fetch specific invoice
NSFetchRequest *fetchRequest3 = [[NSFetchRequest alloc] init];
NSEntityDescription *entity3 = [NSEntityDescription entityForName:@"ItemSold"
inManagedObjectContext:managedObjectContext];
[fetchRequest3 setEntity:entity3];
if ([invoicesListTable selectedRow] >= 0)
{
NSLog(@"C");
int index = (int)[invoicesListTable selectedRow];
NSLog(@"%i", (int)[invoicesListTable selectedRow]);
NSLog(@"%i", index);
NSString *invoiceString = [[fetchedInvoices objectAtIndex:(index)]
valueForKey:@"InvoiceNumber"];
NSNumber *invoiceNumber = [NSNumber numberWithInt:([invoiceString intValue])];
NSPredicate *predicate3 = [NSPredicate predicateWithFormat:@"invoice.InvoiceNumber like %@", invoiceNumber];
[fetchRequest3 setPredicate:predicate3];
}
NSLog(@"D");
fetchedObjects = [managedObjectContext executeFetchRequest:fetchRequest3 error:&error];
if (fetchedObjects == nil)
{
NSLog(@"ERROR");
}
[fetchRequest3 release];
[invoiceTable reloadData];
NSLog(@"D2");
}
If I execute, and click on a row, this is what I get:
2012-02-13 16:42:35.326 InventoryProgram[7056:707] A
2012-02-13 16:42:35.327 InventoryProgram[7056:707] C
2012-02-13 16:42:35.327 InventoryProgram[7056:707] 0
2012-02-13 16:42:35.328 InventoryProgram[7056:707] 0
2012-02-13 16:42:35.328 InventoryProgram[7056:707] D
2012-02-13 16:42:35.329 InventoryProgram[7056:707] Can't create a regex expression from object 2.
I’m showing the output of the letters just to show where control follows. It follows as I select a row, outputs the correct row number (0), and in fact makes it past where I thought the problem was. Outputting the letter D, it passes the NSPredicate area, which is what I thought the regex problem was coming from. I’m assuming now that it gets to the executeFetchRequest, and encounters the problem of the NSPredicate assigned to it.
Does anyone have any ideas/anything at all to point me in the right direction here? I can’t wrap my mind around what the problem is. I really hope there wasn’t too much code here, I’m just trying to give all the information I can.
Replace:
With:
Solves the problem.