realm cocoa:谓词选择不相关的项目

时间:2015-01-01 05:47:39

标签: ios cocoa swift realm

在这样的模型中:

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
}

问题:如何选择订单的所有产品?

这就是我现在正在做的事情:

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)

它有效,但我对它不满意,有没有办法在一次数据库访问中获取allProductsNotInOrder

感谢。

0 个答案:

没有答案