Ansari
Ansari

Reputation: 23

Realm Database Relationship

I need help in realm database relationship for Swift 2.0. I have two data models with one to many relationship of category and expense.

class Category: Object {
  dynamic variable categoryid = 0
  dynamic variable category_name = ""
}

class Expense: Object {
  dynamic var expenseid = 0
  dynamic var expensename = "" 
  dynamic var category: Category? = nil
}

For this i need to filter my result of expenses for a particular category

let records = try! Realm().objects(Expense).filter(??????)

How can I filter the records for just the selected category?

Also, I am maintaining a category thorough which I want to filter my results rather than the table index. Should I set the categoryid from one view to another through the prepareForSegue function?

Upvotes: 2

Views: 995

Answers (1)

marius
marius

Reputation: 7806

You can filter your Expense objects by retrieving first a given Category object and using that in your query:

let category = …
let records = try! Realm().objects(Expense).filter("category = ?", category)

Upvotes: 2

Related Questions