The fetched object at index 2 has an out of order section name 'Txt. #354

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

Originally created by @jerryga on GitHub (Dec 22, 2020).

Fatal error: 'try!' expression unexpectedly raised an error: Error Domain=NSCocoaErrorDomain Code=134060 "A Core Data error occurred." UserInfo={reason=The fetched object at index 2 has an out of order section name 'Txt. Objects must be sorted by section name'}: file CoreStore/ListPublisher.swift, line 387
截屏2020-12-22 下午3 39 48

Originally created by @jerryga on GitHub (Dec 22, 2020). Fatal error: 'try!' expression unexpectedly raised an error: Error Domain=NSCocoaErrorDomain Code=134060 "A Core Data error occurred." UserInfo={reason=The fetched object at index 2 has an out of order section name 'Txt. Objects must be sorted by section name'}: file CoreStore/ListPublisher.swift, line 387 ![截屏2020-12-22 下午3 39 48](https://user-images.githubusercontent.com/3733563/102862438-0ff80000-446c-11eb-89f8-ccf66efd5ce7.png)
adam added the question label 2025-12-29 15:29:49 +01:00
adam closed this issue 2025-12-29 15:29:49 +01:00
Author
Owner

@jerryga commented on GitHub (Dec 22, 2020):

@Field.Virtual(
"typeGroup",
customGetter: { object, field in

        if let typeGroup = field.primitiveValue {
            
            return typeGroup
        }
        let typeGroup: String
        switch object.$cameraType.value {
            
        case 0: typeGroup = "Txt"
        case 1: typeGroup = "General"
        case 2: typeGroup = "Cer"
        default: typeGroup = "Others"
        }
        field.primitiveValue = typeGroup
        return typeGroup
    }
)
var typeGroup: String
@jerryga commented on GitHub (Dec 22, 2020): @Field.Virtual( "typeGroup", customGetter: { object, field in if let typeGroup = field.primitiveValue { return typeGroup } let typeGroup: String switch object.$cameraType.value { case 0: typeGroup = "Txt" case 1: typeGroup = "General" case 2: typeGroup = "Cer" default: typeGroup = "Others" } field.primitiveValue = typeGroup return typeGroup } ) var typeGroup: String
Author
Owner

@jerryga commented on GitHub (Dec 22, 2020):

static let ScanPublisher: ListPublisher = iScanCoreData.dataStack.publishList(
From()
.sectionBy(.$typeGroup)
.orderBy(.ascending(.$date))

    )
@jerryga commented on GitHub (Dec 22, 2020): static let ScanPublisher: ListPublisher<ScanResult> = iScanCoreData.dataStack.publishList( From<ScanResult>() .sectionBy(\.$typeGroup) .orderBy(.ascending(\.$date)) )
Author
Owner

@JohnEstropia commented on GitHub (Dec 22, 2020):

@jerryga The error is telling you what's missing: Objects must be sorted by section name
You specified typeGroup as your section grouping, but you are ordering them by date. This means the objects with similar typeGroups would not be ordered in a way that they'll appear in the same section. Your first orderBy clause should always be relevant for your sectionBy:

From<Object>()
    .sectionBy(.$typeGroup)
    .orderBy(
        .ascending(\.$cameraType), // since this is the `Field.Stored` attribute that your `typeGroup` derives from, it should be the first sorting key
        .ascending(\.$date)
    )
@JohnEstropia commented on GitHub (Dec 22, 2020): @jerryga The error is telling you what's missing: `Objects must be sorted by section name` You specified `typeGroup` as your section grouping, but you are ordering them by `date`. This means the objects with similar `typeGroup`s would not be ordered in a way that they'll appear in the same section. Your first `orderBy` clause should always be relevant for your `sectionBy`: ```swift From<Object>() .sectionBy(.$typeGroup) .orderBy( .ascending(\.$cameraType), // since this is the `Field.Stored` attribute that your `typeGroup` derives from, it should be the first sorting key .ascending(\.$date) ) ```
Author
Owner

@jerryga commented on GitHub (Dec 22, 2020):

@JohnEstropia Thank you very much! You save me time.

@jerryga commented on GitHub (Dec 22, 2020): @JohnEstropia Thank you very much! You save me time.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/CoreStore#354