问题
in a model like this:
class Order : RLMObject {
dynamic var orderId = ""
// ... other fields
}
class Product: RLMObject {
dynamic var productId = ""
dynamic var productName = ""
}
// This class relates products to orders, so an order may have multiple products and
// a product may appear in multiple orders
class OrderedProduct : RLMObject {
dynamic var order: Order
dynamic var product: Product
dynamic var quantity: 1
}
Question: How do I select all products that are not part of an order?
here's how I am doing it right now:
let order = //... an order object user selected in the interface
let allOrderedProducts = OrderedProduct.objectsWhere("order.orderId == %@", order.orderId)
var productIds = Array<String()
for op in allOrderedProducts {
let orderedProduct = op as OrderedProduct
productIds.append(orderedProduct.product.productId)
}
let allProductsNotInOrder = Product.objectsWhere("NOT (productId IN %@)", productIds)
It works, but I am not happy with it, is there a way to fetch allProductsNotInOrder in one db access.
Thanks.
来源:https://stackoverflow.com/questions/27728575/realm-cocoa-predicate-to-select-items-not-related