Structure

FetchedResults

A collection of results retrieved from a Core Data store.

Declaration 宣言

struct FetchedResults<Result> where Result : NSFetchRequestResult

Overview 概要

Use a FetchedResults instance to show or edit Core Data managed objects in your app’s user interface. You request a particular set of results by specifying a Result type as the entity type, and annotating the fetched results property declaration with a FetchRequest property wrapper. For example, you can create a request to list all Quake managed objects that the Loading and Displaying a Large Data Feed sample code project defines to store earthquake data, sorted by their time property:


@FetchRequest(sortDescriptors: [SortDescriptor(\.time, order: .reverse)])
private var quakes: FetchedResults<Quake>

The results instance conforms to RandomAccessCollection, so you access it like any other collection. For example, you can create a List that iterates over all the results:


List(quakes) { quake in
    NavigationLink(destination: QuakeDetail(quake: quake)) {
        QuakeRow(quake: quake)
    }
}

When you need to dynamically change the request’s predicate or sort descriptors, set the result instance’s nsPredicate and sortDescriptors or nsSortDescriptors properties, respectively.

The fetch request and its results use the managed object context stored in the environment, which you can access using the managedObjectContext environment value. To support user interface activity, you typically rely on the viewContext property of a shared NSPersistentContainer instance. For example, you can set a context on your top level content view using a container that you define as part of your model:


ContentView()
    .environment(
        \.managedObjectContext,
        QuakesProvider.shared.container.viewContext)

Topics 話題

Configuring the Associated Fetch Request

Getting Indices

Subscripts 添え字

Type Aliases 型エイリアス

Relationships 関係

See Also 参照

Core Data