TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

Swift CoreData view won't refresh If I have a predicate filter

Forums > SwiftUI

Hello, Im using a coredata to populate a list on a view. When I add a new object the list dosent update until close the app and open it again. But this only started happening after I started using a predicate in the fetch request, Taking the Predicate out makes it work again. I tried everything else. This is the only thing that is causing it. Has anyone else experienced this?

@FetchRequest(
sortDescriptors: [NSSortDescriptor(key: "sobrietyDate_month", ascending: true),NSSortDescriptor(key: "sobrietyDate_day", ascending: true),NSSortDescriptor(key: "first_name", ascending: true)],
predicate: NSPredicate(format: "isVisible == %@", "1")
) var allContacts: FetchedResults<User>

if I take the predicate out it starts updating again. I tried this with Apple standard out of the box CoreData project, by adding a new object to core data as a bool then

@FetchRequest(
        sortDescriptors: [NSSortDescriptor(keyPath: \Item.timestamp, ascending: true)],
        predicate: NSPredicate(format: "isVisible == %@", "1"),
        animation: .default)
    private var items: FetchedResults<Item>

Same thing happens, Won't update live until the app is closed. Any ideas? Thanks

1      

Actally never mind i think it was the way i was checking for the bool, Should have been

NSPredicate(format: "boolAttribute = %d", true) Its working now.

1      

Hacking with Swift is sponsored by String Catalog.

SPONSORED Get accurate app localizations in minutes using AI. Choose your languages & receive translations for 40+ markets!

Localize My App

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.