如何在SwiftUI中限制FetchRequest中结果的数量

时间:2019-12-23 05:57:37

标签: swiftui

对CoreData制作FetchedResults时,如何限制检索到的FetchRequest的大小?

struct ContentView: View {
    var fetchRequest:FetchRequest<Kana>

    init(){
        fetchRequest = FetchRequest<Kana>(entity: Kana.entity(), sortDescriptors: [], predicate: NSPredicate(format: "alphabet == %@", "hiragana"))
    }

    var body: some View {
        List{
            Text("10 Kanas")
            // Display 10 Kanas
        }
    }
}

1 个答案:

答案 0 :(得分:2)

您需要使用相应的初始化程序使用FetchRequest创建NSFeatchRequest,如下所示

init() {
    let request: NSFetchRequest<Kana> = Kana.fetchRequest()
    request.fetchLimit = 10
    request.predicate = NSPredicate(format: "alphabet == %@", "hiragana")
    fetchRequest = FetchRequest<Kana>(fetchRequest: request)
}