-0.4 C
New York
Saturday, February 22, 2025

ios – SwiftData propertiesToFetch solely fetches the requested attributes, however every file is absolutely chosen even when not accessing different properties


I’ve a quite simple mannequin outlined:

@Mannequin
ultimate class Film: Identifiable {
    #Index([.name])

    var id = UUID()
    var title: String
    var style: String?

    init(title: String, style: String?) {
        self.title = title
        self.style = style
    }
}

I turned on SQL debugging by together with ‘-com.apple.CoreData.SQLDebug 3’ argument on launch.

Once I fetch the info utilizing the next code, it selects 3 data initially, however then additionally selects every file individually although I’m not referencing every other attributes.

var fetchDescriptor = FetchDescriptor()
fetchDescriptor.propertiesToFetch = [.id, .name]
fetchDescriptor.fetchLimit = 3

do {
   print("SELECT START")
   films = strive modelContext.fetch(fetchDescriptor)
   print("SELECT END")
} catch {
    print("Did not load Film mannequin.")
}

Right here is the debug information:

SELECT START
CoreData: annotation: fetch utilizing NSSQLiteStatement <0x6000021581e0> on entity 'Film' with sql textual content 'SELECT 1, t0.Z_PK, t0.ZID, t0.ZNAME FROM ZMOVIE t0  LIMIT 3' returned 3 rows with values: (
CoreData: annotation: fetch utilizing NSSQLiteStatement <0x600002154b40> on entity 'Film' with sql textual content '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 utilizing NSSQLiteStatement <0x600002154b40> on entity 'Film' with sql textual content '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 utilizing NSSQLiteStatement <0x600002154b40> on entity 'Film' with sql textual content '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 deciding on the three rows initialsy, however then it selects every one individually. Why wouldn’t it do that on the preliminary fetch? I hoped to pick the info that I wish to show and let the system choose all the file solely once I entry a variable that I didn’t initially fetch.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles