Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding .searchable to a SwiftUI List populated with ObservedResults makes List not updated with new result #8096

Closed
piersonleo opened this issue Jan 11, 2023 · 5 comments · Fixed by #8114
Assignees

Comments

@piersonleo
Copy link

piersonleo commented Jan 11, 2023

How frequently does the bug occur?

Always

Description

I have a SwiftUI list that I populate using ObservedResults. So everytime there's a change to the database my list would be updated.

Since I want to be able to filter my list, I added .searchable to the list. But after adding .searchable to the list, it doesn't get updated anymore when I added a new result to my list. To make the list update I have to search for it first from the search bar.

If I remove the searchable my list is updated with new result as expected after adding.

Stacktrace & log output

No response

Can you reproduce the bug?

Always

Reproduction Steps

Here's the code of my view

import SwiftUI
import RealmSwift

struct AccountList: View {

    @ObservedResults(
        AccountEntity.self,
        sortDescriptor: SortDescriptor(keyPath: "name", ascending: true)
    ) var accounts
    
    @State private var searchFilter = ""
    
    var body: some View {

        NavigationStack {
            List(accounts) { account in
                NavigationLink{
                    AccountDetail(account: account)
                } label: {
                    AccountRow(account: account)
                }
            }
            .searchable(text: $searchFilter,
                        collection: $accounts,
                        keyPath: \.name) {
                ForEach(accounts) { accountsFiltered in
                    
                }
            }
        }
        .navigationTitle("Accounts")
    }
}

And my add function is simple like this, which I call from another view

func addAccount(account: AccountEntity){
        let defaultRealm = try! Realm()
        
        try! defaultRealm.write{
            defaultRealm.add(account)
        }
}

Version

12.13

What Atlas Services are you using?

Local Database only

Are you using encryption?

No

Platform OS and version(s)

Monterey 12.6.1

Build environment

Xcode version: 14.1
Dependency manager and version: ...

@piersonleo piersonleo changed the title Adding .searchable to a SwiftUI List populated with ObservedResults makes List not updated Adding .searchable to a SwiftUI List populated with ObservedResults makes List not updated with new result Jan 11, 2023
@leemaguire
Copy link
Contributor

Hi @piersonleo I cannot reproduce your issue with the given steps. Could you create a small app that reproduces this issue?

@sync-by-unito sync-by-unito bot added the Waiting-For-Reporter Waiting for more information from the reporter before we can proceed label Jan 24, 2023
@piersonleo
Copy link
Author

piersonleo commented Jan 25, 2023

Hi @leemaguire here's the sample app that reproduces this issue. I've included a toolbar to quickly add a new object to the database.

https://github.com/piersonleo/RealmSearchable

@github-actions github-actions bot added Needs-Attention Reporter has responded. Review comment. and removed Waiting-For-Reporter Waiting for more information from the reporter before we can proceed labels Jan 25, 2023
@leemaguire
Copy link
Contributor

@piersonleo Thanks for this we will investigate.

@sync-by-unito sync-by-unito bot removed the Needs-Attention Reporter has responded. Review comment. label Jan 26, 2023
@leemaguire
Copy link
Contributor

I've found the cause of this. I'll get a PR up shortly.

@piersonleo
Copy link
Author

Great! Hope it gets merged soon!

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 17, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants