UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Core Data Update all values that are ""

Forums > Swift

I have a variable named stop_Time = ""

I may have at anytime a total of 1 to 20 variables that have no stop_Time with a value that has been assigned = ""

All my objects when I start recording have data simular to below except for the stop_time which is = ""

How can I loop threw my Entity named "Log" and apply my stop_time when I click the Stop Button.

<Log: 0x600000ee8c80> (entity: Log; id: 0x94b88d14f31a11a2 <x-coredata://B6D8A30D-BE62-4349-BCA0-FFF5461C83F4/Log/p10>; data: {
    id = "1988D1D6-71D2-4071-8E29-207E4C2D3184";
    metaData = l;
    note = "Temp Note March 5th";
    "start_time" = "2021-03-11 10:47:48";
    "stop_time" = "";
    "story_id" = 3224;
    "user_id" = 74;
})

Here is my context

@main
struct MyFirstCoreDataApp: App {

    @StateObject var environmentObject = LoggerVariables()

    @StateObject var dataController: DataController

    init() {
        let dataController = DataController()
        _dataController = StateObject(wrappedValue: dataController)
    }

    var body: some Scene {
        WindowGroup {
            ContentView()
                .environmentObject(environmentObject)
                .environment(\.managedObjectContext, dataController.container.viewContext)
                .environmentObject(dataController)
                // Automatically save when we detect that we are
                // no longer the foreground app. Use this rather than
                // scene phase so we can port to macOS, where scene
                // phase won't detect our app losing focus.
                .onReceive(NotificationCenter.default.publisher(for: UIApplication.willResignActiveNotification),
                           perform: save)

}
    }

    func save(_ note: Notification) {
        dataController.deleteAll()
        dataController.save()
    }
   }

3      

to clarify my understanding:

1) you have records in core data in an entity called Log

2) some or all of these entries may have stop time set to ""

3) when you click the stop button, you would like all of your entries where stop time is "" to be updated so that their stop time is set to the value of thr stop time variable?

4) is there just one stop time variable so all values in core data will be set to the same value or are there multiple stop time variables?

6) if there are multiple then can you explain that a little more - are you having one stop time variable for each entry in core data?

you need to start by doing a fetch request on your core data entry with a predicate to only retrieve recors where stop time = "".

Then you would return that as an array and then (given you only have 20 or less) loop through them and update the stop times and then save.

are you familiar with core data fetch requests etc or would you like some help with that aspect?

I can help with the details and the code once I know a little more and you let me know my understanding is correct on thr above points.

hear from you soon

Richard

3      

Thank you for replying. I finally got it to work properly. Thank you for your input. robert

3      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

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.