Good day! I have a little problem. I want to seperate data from Realm DB by Bool value (true and false). If Bool data currencyStatusCode == true then show currencyName, currencyCode and balance at first tableview section "Active accounts", if Bool data currencyStatusCode == false then in to second section "Inactive accounts". As well as I add image of DB look. Will be very appreciate for your help.
var sections = ["Active accounts", "Inactive accounts"]
@IBOutlet weak var accountManagerTableView: UITableView!
let realm = try? Realm()
let accounts = try! Realm().objects(currencyAccounts.self).sorted(byKeyPath: "currencyID")
var accountManager: currencyAccounts?
var accountsRecord: Results<currencyAccounts> {
get {
return realm!.objects(currencyAccounts.self)
}
}
override func numberOfSections(in tableView: UITableView) -> Int {
return sections.count
}
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return sections[section]
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return accounts.count
/* switch (section) {
case 0:
return accounts.count
break;
case 1:
return accounts.count
break;
default:
break;
}
return section
*/
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = accountManagerTableView.dequeueReusableCell(withIdentifier: "activeCurrencyCell", for: indexPath) as! accountManagerTableViewCell
let sortingtInTableView = realm?.objects(currencyAccounts.self).sorted(byKeyPath: "currencyID", ascending: true)
let currentUserBalances = sortingtInTableView![indexPath.row]
cell.currencyFullName.text = currentUserBalances.currencyName
cell.currencyTitle.text = currentUserBalances.currencyCode
cell.selectedAccountBalance.text = String(currentUserBalances.currencyBalance)
return cell
}
You use filter again Realm object itself. Please refer documentation for more details. Please find the code to filter active and inactive accounts:
let realm = try! Realm()
let activeAccounts = realm.objects(Dog.self).filter("currencyStatusCode = true").sorted(byKeyPath: "currencyID")
// Persist your data easily
try! realm.write {
realm.add(activeAccounts)
}
let realm = try! Realm()
let inactiveAccounts = realm.objects(Dog.self).filter("currencyStatusCode = false").sorted(byKeyPath: "currencyID")
// Persist your data easily
try! realm.write {
realm.add(inactiveAccounts)
}