首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >每当CoreData在SwiftUI中更新时更新状态变量

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

Stack Overflow用户
提问于 2020-01-28 17:43:30
回答 2查看 1.1K关注 0票数 2

在更新了CoreData中的一些数据之后,我还希望将State-variable更新为返回结果的数量。

当CoreData被更改时,Stepper应该始终设置为返回结果的数量。然而,当我使用onAppear时,Stepper也会触发。如何在onAppear中签入CoreData是更改了还是使用了Stepper?这可能吗?

代码语言:javascript
复制
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")
            })
        }
    }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-01-28 19:03:05

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

代码语言:javascript
复制
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")
                }
           })
        }
    }
}
票数 4
EN

Stack Overflow用户

发布于 2020-01-28 23:24:31

你不需要@State var numberOfResults。您可以只在objects.count中使用Text()。包装器@FetchRequest为您完成了所有的工作。每当您将YourModel实体对象添加到ManagedObjectContext中时,它将触发FetchRequest刷新并给出实际结果。@FetchRequest已经在做像@State这样的事情了

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59954342

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档