每当在SwiftUI中更新CoreData时更新状态变量

时间:2020-01-28 17:43:30

标签: core-data swiftui

从CoreData更新了一些数据之后,我还想将State变量更新为返回结果的数量。

更改CoreData时,应始终将Stepper设置为返回结果的数量。但是,当我使用onAppear时也会触发Stepper。如何检查onAppear是否已更改CoreData或使用了Stepper?那有可能吗?

import SwiftUI

struct ContentView: View {
@State var numberOfResults = 0

@FetchRequest(entity: YourModel.entity(), sortDescriptors: [], predicate:NSPredicate(format: "isSelected == %@", NSNumber(booleanLiteral: true))) var objects: FetchedResults<YourModel>

var body: some View{
    return VStack{

        Stepper("text", value: $numberOfResults, in: 0...objects.wrappedValue.count, step:5)
            .onReceive(objects.publisher, perform: {_ in
                self.numberOfResults = self.objects.count
                print("onReceive")
            })
        }
    }
}

2 个答案:

答案 0 :(得分:1)

如果使用@FetchRequest,则发布者发送消息时onReceive numberOfResults将被更新

import SwiftUI

struct DidSetCoreData: View {
    @State var numberOfResults = 0
    @State var initSetup1: Bool = true
    @State var initSetup2: Bool = true
    @State var adjustedCount = 0
    @FetchRequest(entity: YourModel.entity(), sortDescriptors: [], predicate:NSPredicate(format: "isSelected == %@", NSNumber(booleanLiteral: true))) var objects: FetchedResults<YourModel>

    var body: some View{
    return VStack{
            Text("Total Count= \(objects.count)")
            Text("Adjusted Total = \($adjustedCount.wrappedValue)")
            //You need the separate adjusted count variable to save the changes

            //Option 1 Stepper - Keeps the max step flexible, eliminates the need for the numberOfResults var
            Stepper("AdjTotal - FlexibleMax", value: $adjustedCount, in: 0...objects.count, step:5)
            .onReceive(objects.publisher.count(), perform: {count in
                //onReceive will be called everytime there is a change to objects.count or body refresh
                print("onReceive - Option 1")
                if self.initSetup1{
                    //The variable will only be setup once
                    self.adjustedCount = count
                    self.initSetup1 = false
                    print("initSetupComplete - Option 1")
                }
            })

            //Option 2 Stepper
            Stepper("AdjTotal - initMax", value: $adjustedCount, in: 0...$numberOfResults.wrappedValue, step:5)

            .onReceive(objects.publisher.count(), perform: {count in
                //onReceive will be called everytime there is a change to objects.count or body refresh
                print("onReceive - Option 2")
                if self.initSetup2{
                    //The variables will only be setup once
                    self.numberOfResults = count //Limits the max step to only the original count
                    self.adjustedCount = self.numberOfResults
                    self.initSetup2 = false
                    print("initSetupComplete - Option 2")
                }
            })
            //Option 3 Stepper - Limits the StepperMax to the Stepper value
            Stepper("AdjTotal - ValueMax", value: $numberOfResults, in: 0...$numberOfResults.wrappedValue, step:5)
            .onReceive(objects.publisher.count(), perform: {count in
                //onReceive will be called everytime there is a change to objects.count or body refresh
                print("onReceive - Option 3")
                if self.initSetup3{
                    //The variable will only be setup once
                    self.numberOfResults = count
                    self.initSetup3 = false
                    print("initSetupComplete - Option 3")
                }
           })
        }
    }
}

答案 1 :(得分:0)

您不需要@State var numberOfResults。您只能在objects.count中使用Text()。包装器@FetchRequest为您完成所有工作。只要将YourModel实体对象添加到ManagedObjectContext中,它就会触发FetchRequest刷新并为您提供实际结果。 @FetchRequest已经做过@State

之类的事情