fermoya / SwiftUIPager

Native Pager in SwiftUI
MIT License
1.29k stars 172 forks source link

Populate from CoreData FetchRequest #140

Closed pearlyred closed 4 years ago

pearlyred commented 4 years ago

Hi, couldn't see anywhere else to ask this.. just wondering if it's possible to use a fetch request result as the data for the pager?

I currently have:

@FetchRequest(entity: Order.entity(), sortDescriptors: [])

var orders: FetchedResults<Order>

I tried setting the Pager.data parameter to orders, but that didn't work.

fermoya commented 4 years ago

Hi, Pager expects an Array of Equatable elements and a keyPath to an identifier. FetchedResults is a RandomAccessCollection

fermoya commented 4 years ago

@lsaunders80 , just use Array(orders) and make sure that either Order conforms to Identifiable or you pass a keyPath to an identifier:

struct Order {
    id: String
   // more variables
}

@State var page = 0

let pager = Pager(page: $page, data: Array(orders), id: \.id) {
    // your view
}
fermoya commented 4 years ago

As part of #135 , version 1.13.0-beta.1 supports an initializer that takes any RandomAccessCollection so you can pass your orders collection directly now:

struct Order {
    id: String
   // more variables
}

@State var page = 0

let pager = Pager(page: $page, data: orders, id: \.id) {
    // your view
}