ListPublisher doesn't have initial snapshot when use @Virtual.Field. #413

Closed
opened 2025-12-29 15:31:15 +01:00 by adam · 2 comments
Owner

Originally created by @noppefoxwolf on GitHub (Jun 14, 2023).

Environment Version
CoreStore 9.1.0
Swift 5.8
Xcode 14.3
import XCTest
import CoreStore

class CoreStoreTests: XCTestCase {
    func testCoreStore() async throws {
        final class Object: CoreStoreObject {
            @Field.Stored("id", dynamicInitialValue: { UUID() })
            var id: UUID
            
            @Field.Virtual("VirtualProperty", customGetter: { _,_ in
                true
            })
            var virtualProperty: Bool
        }
        
        let schema = CoreStoreSchema(
            modelVersion: "V1",
            entities: [
                Entity<Object>("Object")
            ]
        )
        
        let dataStack = DataStack(schema)
        try dataStack.addStorageAndWait(InMemoryStore())
        CoreStoreDefaults.dataStack = dataStack
        
        let listPublisher = {
            dataStack.publishList(From<Object>().where(format: "VirtualProperty == %@", true).orderBy(.ascending(\.$id)))
        }
        
        let listPublisher1 = listPublisher()
        
        try dataStack.perform { transaction in
            let object = transaction.create(Into<Object>())
            object.id = UUID()
        }
        
        let listPublisher2 = listPublisher()
        
        XCTAssertEqual(listPublisher1.snapshot.count, 1)
        XCTAssertEqual(listPublisher2.snapshot.count, 1) //testCoreStore(): XCTAssertEqual failed: ("0") is not equal to ("1")
    }
}

When it not using virtual property, listPublisher2 has snapshot.

Originally created by @noppefoxwolf on GitHub (Jun 14, 2023). |Environment| Version | |---|---| |CoreStore| 9.1.0| |Swift| 5.8| |Xcode| 14.3| ``` import XCTest import CoreStore class CoreStoreTests: XCTestCase { func testCoreStore() async throws { final class Object: CoreStoreObject { @Field.Stored("id", dynamicInitialValue: { UUID() }) var id: UUID @Field.Virtual("VirtualProperty", customGetter: { _,_ in true }) var virtualProperty: Bool } let schema = CoreStoreSchema( modelVersion: "V1", entities: [ Entity<Object>("Object") ] ) let dataStack = DataStack(schema) try dataStack.addStorageAndWait(InMemoryStore()) CoreStoreDefaults.dataStack = dataStack let listPublisher = { dataStack.publishList(From<Object>().where(format: "VirtualProperty == %@", true).orderBy(.ascending(\.$id))) } let listPublisher1 = listPublisher() try dataStack.perform { transaction in let object = transaction.create(Into<Object>()) object.id = UUID() } let listPublisher2 = listPublisher() XCTAssertEqual(listPublisher1.snapshot.count, 1) XCTAssertEqual(listPublisher2.snapshot.count, 1) //testCoreStore(): XCTAssertEqual failed: ("0") is not equal to ("1") } } ``` When it not using virtual property, listPublisher2 has snapshot.
adam closed this issue 2025-12-29 15:31:15 +01:00
Author
Owner

@JohnEstropia commented on GitHub (Jun 16, 2023):

This is expected behavior. Virtual (transient) properties cannot be queried on because they don't exist in the store

@JohnEstropia commented on GitHub (Jun 16, 2023): This is expected behavior. Virtual (transient) properties cannot be queried on because they don't exist in the store
Author
Owner

@JohnEstropia commented on GitHub (Jun 16, 2023):

If you try to use the type-safe predicates, you'll get a compiler error instead because this is not supported:

.where(\.virtualProperty == true)
@JohnEstropia commented on GitHub (Jun 16, 2023): If you try to use the type-safe predicates, you'll get a compiler error instead because this is not supported: ```swift .where(\.virtualProperty == true) ```
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/CoreStore#413