【问题标题】:Swift DynamicFetchView fetchlimitSwift DynamicFetchView fetchlimit
【发布时间】:2020-05-06 09:19:19
【问题描述】:

我一直在处理一个获取请求,使用这里的答案似乎可以正常工作:https://stackoverflow.com/a/60723054/1254397

import CoreData
import SwiftUI

struct DynamicFetchView<T: NSManagedObject, Content: View>: View {
    let fetchRequest: FetchRequest<T>
    let content: (FetchedResults<T>) -> Content

    var body: some View {
        self.content(fetchRequest.wrappedValue)
    }

    init(predicate: NSPredicate?, fetchLimit:Int = 5, sortDescriptors: [NSSortDescriptor], @ViewBuilder content: @escaping (FetchedResults<T>) -> Content) {
        fetchRequest = FetchRequest<T>(entity: T.entity(), sortDescriptors: sortDescriptors, predicate: predicate, fetchLimit)
        self.content = content
    }

    init(fetchRequest: NSFetchRequest<T>, @ViewBuilder content: @escaping (FetchedResults<T>) -> Content) {
        self.fetchRequest = FetchRequest<T>(fetchRequest: fetchRequest)
        self.content = content
    }
}

在我看来:

DynamicFetchView(predicate: NSPredicate(format: "Brand CONTAINS %@", "GAP"), sortDescriptors: [NSSortDescriptor(keyPath: \store.brandName, ascending: false)]) { (clothing: FetchedResults<Store>) in
    HStack {
        Text("Number of Gap products: \(clothing.count)")
            .bold()
            .underline()
        List {
            ForEach(clothing) { Store in
                Text("Clothing Name: \(Store.clothingName!)")
            }
        }
    }

DynamicFetchView 可以很好地显示 fetch 请求中的所有内容,但我想将其限制为 5。我尝试添加 fetchLimit=5 但似乎没有任何区别。

【问题讨论】:

    标签: swift core-data swiftui nsfetchrequest


    【解决方案1】:

    这是一个解决方案:

    struct DynamicFetchView<T: NSManagedObject, Content: View>: View {
        let fetchRequest: FetchRequest<T>
        let content: (FetchedResults<T>) -> Content
    
        var body: some View {
            self.content(fetchRequest.wrappedValue)
        }
    
        init(predicate: NSPredicate?, fetchLimit:Int = 5, sortDescriptors: [NSSortDescriptor], @ViewBuilder content: @escaping (FetchedResults<T>) -> Content) {
            guard let entityName = T.entity().name else { fatalError("Unknown entity") }
    
            let request = NSFetchRequest<T>(entityName: entityName)
            request.fetchLimit = fetchLimit
            request.sortDescriptors = sortDescriptors
            request.predicate = predicate
    
            self.init(fetchRequest: request, content: content)
        }
    
        init(fetchRequest: NSFetchRequest<T>, @ViewBuilder content: @escaping (FetchedResults<T>) -> Content) {
            self.fetchRequest = FetchRequest<T>(fetchRequest: fetchRequest)
            self.content = content
        }
    }
    

    【讨论】:

    • 哇,真的非常感谢你,它工作得很好,只有一个小问题,当我调用 DynamicFetchView 时,是否可以在我的视图中说明我想要多少结果?
    • @zeem,你是什么意思?只需在构造函数中指定DynamicFetchView(predicate: NSPredicate(...), fetchLimit: 10, sortDescriptors...
    • 我的错,我读到它总是会返回 5 个结果,添加到构造函数和宾果游戏中它工作。再次非常感谢您!
    猜你喜欢
    • 2011-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-30
    相关资源
    最近更新 更多