Swift DynamicFetchView 获取限制
Swift DynamicFetchView fetchlimit
我一直在处理一个提取请求,使用这里的答案似乎工作正常:
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
可以很好地显示获取请求中的所有内容,但我想将其限制为 5。我尝试添加 fetchLimit=5 但这似乎没有任何区别。
解决方法如下:
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
}
}
我一直在处理一个提取请求,使用这里的答案似乎工作正常:
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
可以很好地显示获取请求中的所有内容,但我想将其限制为 5。我尝试添加 fetchLimit=5 但这似乎没有任何区别。
解决方法如下:
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
}
}