Hi there,
I am working on an ios todo list that can be sorted and filtered in various way. I created an observableobject that can be intialized with different variables so that as the variables change, the results are updated and passed on to a list. However when the variables change and the result is updated, it maxes out the phone cpu and make the whole application hang. Any Advice?
Here is my code:
final class TaskHelper: ObservableObject
{
private var taskToken: NotificationToken!
var taskResults: Results<Tasks>
init(filters: [String], quadrant: String, sort:Int, autoAdvance: Bool, focus: Int)
{
let realm = try! Realm()
if filters == [NSLocalizedString("_none", comment: "")] || filters.isEmpty || filters == [] || filters == [""]
{
taskResults = realm.objects(Tasks.self)
.sorted(by: sort.getSortDescriptor())
.getfocus(quadrant: quadrant, focus: focus, autoAdvance: autoAdvance)
}
else
{
taskResults = realm.objects(Tasks.self)
.sorted(by: sort.getSortDescriptor())
.getfocus(quadrant: quadrant, focus: focus, autoAdvance: autoAdvance)
.filter(NSPredicate(format: "filter IN %@", filters as CVarArg))
}
lateInit()
}
func lateInit(){
taskToken = taskResults.observe { [weak self] _ in
self?.objectWillChange.send()
}
}
deinit {
taskToken.invalidate()
}
var taskArray: [Tasks]
{
taskResults.map(Tasks.init)
}
}
ScrollView{
LazyVStack{
ForEach(taskHelper.taskResults)
{ task in
TaskRow(task: task, quadrant: quadrant)
}
}
}