Using Where clause always results in empty or nil #392

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

Originally created by @iarata on GitHub (Sep 10, 2022).

In my app, I have the following CoreStoreObject class which I use to store assignments:

class AssignmentCore: CoreStoreObject {
    @Field.Stored("id", dynamicInitialValue: { UUID() })
    var id: UUID
    
    @Field.Stored("isHidden", dynamicInitialValue: { false })
    var isHidden: Bool
    
    @Field.Coded("assignment", coder: FieldCoders.Json.self)
    var assignment: Assignment?
}

I can successfully create AssignmnetCore objects and store them using the following method:

func build(assignment: Assignment, completion handler: @escaping (Result<Void, Error>) -> Void) {
        self.dataStack.perform(
            asynchronous: { (transaction) in
                let assignmentRecord = transaction.create(Into<AssignmentCore>())
                assignmentRecord.id = assignment.id
                assignmentRecord.assignment = assignment
                assignmentRecord.isHidden = false
            },
            completion: { (result) in
                switch result {
                case .success:
                    handler(.success(()))
                case .failure(let error):
                    handler(.failure(error))
                }
            }
        )
    }

But every time that I try to use the Where clause to get a specific object using fetchAll or fetchOne it returns nil or empty. Also, the deleteAll is not deleting the object and I'm using the Where just like below.
This has been happening recently and it was working fine even before I change to develop branch.

I tried the following ways but all resulted in empty data (the data exists because if I just use fetchAll I get all the objects):

func isAssignmentExists(assignment asign: Assignment) -> Bool {
        do {
            let datas = try self.dataStack.fetchAll(From<AssignmentCore>()
//                                                    .where(format: "assignment == %@", asign)
                                                      .where(format: "id == %@", asign.id)
            )
            if datas.isEmpty {
                return false
            } else {
                return true
            }
        } catch {
            print(error.localizedDescription)
            return false
        }
    }

In addition, every time I try to use the == operator in the where clause I get Operator function '==' requires that 'AssignmentCore' inherit from 'NSManagedObject' error.
The example below results in the above error:

let datas = try self.dataStack.fetchAll(From<AssignmentCore>()
                .where(\.id == asign.id)
            )

Running on iOS 15.0
Using CoreStore develop branch
Xcode 14.0 (14A309)

Originally created by @iarata on GitHub (Sep 10, 2022). In my app, I have the following `CoreStoreObject` class which I use to store assignments: ``` class AssignmentCore: CoreStoreObject { @Field.Stored("id", dynamicInitialValue: { UUID() }) var id: UUID @Field.Stored("isHidden", dynamicInitialValue: { false }) var isHidden: Bool @Field.Coded("assignment", coder: FieldCoders.Json.self) var assignment: Assignment? } ``` I can successfully create `AssignmnetCore` objects and store them using the following method: ``` func build(assignment: Assignment, completion handler: @escaping (Result<Void, Error>) -> Void) { self.dataStack.perform( asynchronous: { (transaction) in let assignmentRecord = transaction.create(Into<AssignmentCore>()) assignmentRecord.id = assignment.id assignmentRecord.assignment = assignment assignmentRecord.isHidden = false }, completion: { (result) in switch result { case .success: handler(.success(())) case .failure(let error): handler(.failure(error)) } } ) } ``` But every time that I try to use the `Where` clause to get a specific object using `fetchAll` or `fetchOne` it returns nil or empty. Also, the `deleteAll` is not deleting the object and I'm using the `Where` just like below. This has been happening recently and it was working fine even before I change to develop branch. I tried the following ways but all resulted in empty data (the data exists because if I just use `fetchAll` I get all the objects): ``` func isAssignmentExists(assignment asign: Assignment) -> Bool { do { let datas = try self.dataStack.fetchAll(From<AssignmentCore>() // .where(format: "assignment == %@", asign) .where(format: "id == %@", asign.id) ) if datas.isEmpty { return false } else { return true } } catch { print(error.localizedDescription) return false } } ``` In addition, every time I try to use the `==` operator in the where clause I get `Operator function '==' requires that 'AssignmentCore' inherit from 'NSManagedObject'` error. The example below results in the above error: ``` let datas = try self.dataStack.fetchAll(From<AssignmentCore>() .where(\.id == asign.id) ) ``` Running on iOS 15.0 Using CoreStore develop branch Xcode 14.0 (14A309)
adam added the question label 2025-12-29 15:30:52 +01:00
adam closed this issue 2025-12-29 15:30:52 +01:00
Author
Owner

@JohnEstropia commented on GitHub (Sep 10, 2022):

Since you are using the @Field property wrappers, use this syntax when querying:

.where(\.$id == asign.id)
@JohnEstropia commented on GitHub (Sep 10, 2022): Since you are using the `@Field` property wrappers, use this syntax when querying: ```swift .where(\.$id == asign.id) ```
Author
Owner

@iarata commented on GitHub (Sep 10, 2022):

I think I missed that note in the docs.
Thank you it works now 🎉

@iarata commented on GitHub (Sep 10, 2022): I think I missed that note in the docs. Thank you it works now 🎉
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/CoreStore#392