最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

ios - SwiftData propertiesToFetch only fetches the requested attributes, but each record is fully selected even when not accessi

programmeradmin1浏览0评论

I have a very simple model defined:

@Model
final class Movie: Identifiable {
    #Index\<Movie\>(\[.name\])

    var id = UUID()
    var name: String
    var genre: String?

    init(name: String, genre: String?) {
        self.name = name
        self.genre = genre
    }
}

I turned on SQL debugging by including '-com.apple.CoreData.SQLDebug 3' argument on launch.

When I fetch the data using the following code, it selects 3 records initially, but then also selects each record individually even though I am not referencing any other attributes.

var fetchDescriptor = FetchDescriptor\<Movie\>()
fetchDescriptor.propertiesToFetch = \[.id, .name\]
fetchDescriptor.fetchLimit = 3

do {
   print("SELECT START")
   movies = try modelContext.fetch(fetchDescriptor)
   print("SELECT END")
} catch {
    print("Failed to load Movie model.")
}

Here is the debug info:

SELECT START
CoreData: annotation: fetch using NSSQLiteStatement <0x6000021581e0> on entity 'Movie' with sql text 'SELECT 1, t0.Z_PK, t0.ZID, t0.ZNAME FROM ZMOVIE t0  LIMIT 3' returned 3 rows with values: (
CoreData: annotation: fetch using NSSQLiteStatement <0x600002154b40> on entity 'Movie' with sql text 'SELECT 0, t0.Z_PK, t0.Z_OPT, t0.ZGENRE, t0.ZID, t0.ZNAME FROM ZMOVIE t0 WHERE  t0.Z_PK = ? ' returned 1 rows
CoreData: annotation: fetch using NSSQLiteStatement <0x600002154b40> on entity 'Movie' with sql text 'SELECT 0, t0.Z_PK, t0.Z_OPT, t0.ZGENRE, t0.ZID, t0.ZNAME FROM ZMOVIE t0 WHERE  t0.Z_PK = ? ' returned 1 rows
CoreData: annotation: fetch using NSSQLiteStatement <0x600002154b40> on entity 'Movie' with sql text 'SELECT 0, t0.Z_PK, t0.Z_OPT, t0.ZGENRE, t0.ZID, t0.ZNAME FROM ZMOVIE t0 WHERE  t0.Z_PK = ? ' returned 1 rows
SELECT END

I see it selecting the 3 rows initialsy, but then it selects each one separately. Why would it do this on the initial fetch? I was hoping to select the data that I want to display and let the system select the entire record only when I access a variable that I did not initially fetch.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论