小编Jus*_*ock的帖子

从带有部分的 SwiftUI 列表中删除 CoreData

目标

SectionedFetchRequest我想从列表中删除一个项目ForEach。我找到的唯一解决方案是对于常规FetchRequest我已设法将其从 UIList 中删除,但没有从 CoreData 中删除ViewContext

我的问题很独特,因为我试图从与 FetchRequest 不同的SectionedFetchRequest 中删除

    @SectionedFetchRequest(entity: Todo.entity(), sectionIdentifier: \.dueDateRelative, sortDescriptors: [NSSortDescriptor(keyPath: \Todo.dueDate, ascending: true)], predicate: nil, animation: Animation.linear)
    var sections: SectionedFetchResults<String, Todo>
Run Code Online (Sandbox Code Playgroud)
    var body: some View {
        NavigationView {
            List {      
                ForEach(sections) { section in
                    Section(header: Text(section.id.description)) {
                        ForEach(section) { todo in
                            TodoRowView(todo: todo)
                                .frame(maxWidth: .infinity)
                                .listRowSeparator(.hidden)
                        }
                        .onDelete { row in
                            deleteTodo(section: section.id.description, row: row)
                            }
                        }

                    }
                }
Run Code Online (Sandbox Code Playgroud)
    func deleteTodo(section: String, row: IndexSet) {
        // Need …
Run Code Online (Sandbox Code Playgroud)

iphone core-data ios swift swiftui

8
推荐指数
1
解决办法
1999
查看次数

如何创建一个可以在 SwiftUI 中更新的单例

我想创建一个全局变量来显示加载视图。我尝试了很多不同的方法,但不知道该怎么做。我需要能够在整个应用程序中访问此变量,并在更改单例的布尔值时更新 MotherView 文件。

struct MotherView: View {
    
    @StateObject var viewRouter = ViewRouter()
        
    var body: some View {
        
        if isLoading { //isLoading needs to be on a singleton instance
            Loading()
        }

        
        switch viewRouter.currentPage {
        case .page1:
            ContentView()
        case .page2:
            PostList()
        }
    }
}

struct MotherView_Previews: PreviewProvider {
    static var previews: some View {
        MotherView(viewRouter: ViewRouter())
    }
}
Run Code Online (Sandbox Code Playgroud)

我已经尝试了下面的单例,但它不允许我更新共享实例?如何更新单例实例?

struct LoadingSingleton {
    static let shared = LoadingSingleton()
    var isLoading = false

    private init() { }
}
Run Code Online (Sandbox Code Playgroud)

singleton struct swiftui

3
推荐指数
1
解决办法
7167
查看次数

在 SwiftUI 列表中按 Date() 将 CoreData 分组为部分

我的目标:

我希望能够按其到期日期范围对 CoreData Todo 项目进行分组。(“今天”、“明天”、“接下来 7 天”、“未来”)

我尝试过的...

我尝试使用@SectionedFetchRequest,但sectionIdentifier需要一个字符串。如果它作为 Date() 存储在 coreData 中,如何将其转换以供使用?我收到了许多没有帮助的错误和建议。这也无法解决“接下来 7 天”等日期范围的问题。此外,我似乎甚至没有访问实体的 dueDate,因为它指向我的 ViewModel 表单。

    @Environment(\.managedObjectContext) private var viewContext
    
    //Old way of fetching Todos without the section fetch
    //@FetchRequest(sortDescriptors: []) var todos: FetchedResults<Todo>
    
    @SectionedFetchRequest<String, Todo>(
        entity: Todo.entity(), sectionIdentifier: \Todo.dueDate,
        SortDescriptors: [SortDescriptor(\.Todo.dueDate, order: .forward)]
    ) var todos: SectionedFetchResults<String, Todo>
Run Code Online (Sandbox Code Playgroud)
Cannot convert value of type 'KeyPath<Todo, Date?>' to expected argument type 'KeyPath<Todo, String>'

Value of type 'NSObject' has no member 'Todo'
Run Code Online (Sandbox Code Playgroud)

是否有另一种解决方案更适合我的情况,@SectionedFetchRequest?如果没有,我希望了解如何适当地对数据进行分组。

iphone core-data ios swift swiftui

2
推荐指数
1
解决办法
2676
查看次数

标签 统计

swiftui ×3

core-data ×2

ios ×2

iphone ×2

swift ×2

singleton ×1

struct ×1