I have a BehaviorRelay with an Array of FamilyTaskCoreData inside it. In FamilyTaskCoreData I have the "owner" parameter and I want to filter the Array where it has the id of "45332523dqwd" or an other query.
This is my BehaviorRelay:
private var familyTask = BehaviorRelay<[FamilyTasksCoreData]>(value: [])
And this is the code I use to bind it:
let item = memberData.getTaskData(memberID: queryID)
item
.filter(
$0.filter{ $0.name.hasPrefix("M")}
)
.bind(to: tableView.rx.items(cellIdentifier: "familyCleaningPlanCell", cellType: FamilyCleaningPlanTableViewCell.self)) {[weak self] (row, element, cell) in
cell.titleLabel.text = element.title
cell.checkMarcButton.isSelected = element.status
cell.categoryImage.image = self?.defineImage(name: element.category ?? "")
self?.updateAnItem(cell: cell, data: element)
}.addDisposableTo(disposeBag)
}
I tried to filter it with the filter statement...because I saw it on another question, but I can't find something after $0.. in my case there is no value which I can select.
FamilyTasksCoreData:
@NSManaged public var category: String?
@NSManaged public var end: Date
@NSManaged public var id: String?
@NSManaged public var start: Date
@NSManaged public var status: Bool
@NSManaged public var title: String?
@NSManaged public var createdAt: Date
@NSManaged public var owner: String?
@NSManaged public var familyID: String?
If I understand correctly, you want the tableView to only show FamilyTasksCoreData objects that have a name beginning with "M".
To do that, you need to use a map on the relay instead of a filter. If you use a filter on the relay, you will filter out the entire array all at once, instead of the individual elements.
Instead, you want to use map to transform each array you get from the relay, filtering out the elements that do not begin with "M".
Your code should look something like this:
item
.map {
$0.filter { $0.name.hasPrefix("M") }
}
.bind(...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With