Wazza
Wazza

Reputation: 1885

Predicate matching one to many relationship coredata

I am trying to get the total price of a bill which has many items related to it in coredata. I am struggling to understand the predicate syntax required to retrieve the items so that i can access their prices. I keep getting the following error :

    2016-12-14 14:13:12.309 Splitter[43533:5597917] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'unimplemented SQL generation for predicate : (ANY items IN <Splitter.Bill: 0x7fe6e0cebbd0> (entity: Bill; id: 0xd000000000300000 <x-coredata://D13BAFFD-A78A-4C49-9992-F975887B7AB7/Bill/p12> ; data: {
    date = "December 14, 2016";
    id = "6D672183-44C2-4961-ABB6-27FD657E5C68";
    items =     (
    "0xd000000001fc0012 <x-coredata://D13BAFFD-A78A-4C49-9992-F975887B7AB7/Item/p127>",
    "0xd0000000020c0012 <x-coredata://D13BAFFD-A78A-4C49-9992-F975887B7AB7/Item/p131>",
    "0xd000000002080012 <x-coredata://D13BAFFD-A78A-4C49-9992-F975887B7AB7/Item/p130>",
    "0xd000000002100012 <x-coredata://D13BAFFD-A78A-4C49-9992-F975887B7AB7/Item/p132>",
    "0xd000000002040012 <x-coredata://D13BAFFD-A78A-4C49-9992-F975887B7AB7/Item/p129>",
    "0xd000000001f00012 <x-coredata://D13BAFFD-A78A-4C49-9992-F975887B7AB7/Item/p124>",
    "0xd000000001f80012 <x-coredata://D13BAFFD-A78A-4C49-9992-F975887B7AB7/Item/p126>",
    "0xd000000001e80012 <x-coredata://D13BAFFD-A78A-4C49-9992-F975887B7AB7/Item/p122>",
    "0xd000000001f40012 <x-coredata://D13BAFFD-A78A-4C49-9992-F975887B7AB7/Item/p125>",
    "0xd000000001ec0012 <x-coredata://D13BAFFD-A78A-4C49-9992-F975887B7AB7/Item/p123>",
    "(...and 1 more...)"
    );
    location = hgfhgfd;
    name = hgfhgfd;
    total = 0;
    }))'

From the following code:

func setBillTotal() {
    let managedContext = billObject.managedObjectContext
    let fetchRequest = NSFetchRequest(entityName: "Item")
    let predicate = NSPredicate(format: "ANY items IN %@", billObject)
    fetchRequest.predicate = predicate
    var items = [Item]()
    do {
        let results =
            try managedContext!.executeFetchRequest(fetchRequest)
        items = results as! [Item]
    } catch let error as NSError {
        print("Could not fetch \(error), \(error.userInfo)")
    }

    var sum = Int(0.0)
    for item in items {
        sum += Int(item.price)
    }

    billObject.setValue(sum, forKey: "total")
    do {
        try managedContext!.save()
    } catch let error as NSError  {
        print("Could not save \(error), \(error.userInfo)")
    }
}

Database relationship images: Item Bill

Any help would be great, im sure its a simple fix but the syntax is going over my head somewhere. Thanks

Upvotes: 0

Views: 2203

Answers (1)

pbasdf
pbasdf

Reputation: 21536

The direct answer is that you should be using the inverse relationship, bill, in your predicate:

let predicate = NSPredicate(format: "bill == %@", billObject)

However, there is an easier way to achieve your goal of calculating the total price:

let total = billObject.valueForKeyPath("[email protected]")
billObject.setValue(total, forKey:"total")

No need for the fetch request, nor for the for loop to iterate through the items: these two lines will achieve the same.

Upvotes: 5

Related Questions