@FetchRequestで取得する
管理対象オブジェクトを直接ビューに読み込みます。
「@FetchRequestで取得する」はCoddyKit上の無料SwiftUI Academyレッスンです。 これはレッスン2/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはSwiftUI Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 SwiftUI Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
Reading Records in Views
To show saved data, a SwiftUI view needs to fetch it from Core Data and re-render whenever that data changes.
The @FetchRequest Wrapper
The @FetchRequest property wrapper runs a query for an entity and keeps the results live and current for you.
@FetchRequest(sortDescriptors: []) var tasks: FetchedResults<Task>What You Get Back
Results arrive as FetchedResults, a collection you can loop over just like an array inside your view body.
Showing Them in a List
Drop the results straight into a List with ForEach to render one row per stored record.
List(tasks) { task in
Text(task.title ?? "Untitled")
}Sorting the Results
Pass sortDescriptors so rows arrive in a predictable order instead of a random one.
@FetchRequest(sortDescriptors: [SortDescriptor(\.title)]) var tasks: FetchedResults<Task>Filtering with a Predicate
Add an NSPredicate to fetch only the records that match a rule, like done items or a search term.
@FetchRequest(sortDescriptors: [], predicate: NSPredicate(format: "isDone == true")) var done: FetchedResults<Task>It Stays Live
@FetchRequest watches the context, so when you add or delete a record the view updates automatically. ⚡
It Needs the Context
@FetchRequest reads the context from the environment, so the value you injected in the stack must be present.
Handling an Empty Result
When the fetch returns nothing, show a friendly empty state instead of a blank screen.
if tasks.isEmpty {
Text("No tasks yet")
}Optional Attributes
Core Data attributes are often optional in Swift, so unwrap them safely before you display their values.
Declare It as var
Declare a @FetchRequest property as var, since the wrapper updates its stored results as the data changes.
Quick Check
You wired up @FetchRequest. A quick check on how it behaves.
Recap
You used @FetchRequest to pull records into a view, sort and filter them, and let the list refresh itself as data changes. ✨
よくある質問
「@FetchRequestで取得する」レッスンは無料ですか?
はい。「@FetchRequestで取得する」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、SwiftUI Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 SwiftUI Academyコースには全4レッスンが含まれています。
「@FetchRequestで取得する」で何を学びますか?
管理対象オブジェクトを直接ビューに読み込みます。 ブラウザで直接実行するハンズオンコードでSwiftUI Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
SwiftUI Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのSwiftUI Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン2/4です。
「@FetchRequestで取得する」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このSwiftUI Academyレッスンでコードを書いて実行できますか?
はい。すべてのSwiftUI Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- Core Dataスタック
- @FetchRequestで取得する
- エンティティを作成して保存する
- 述語とソート記述子