Problem with NSFetchRequest/error handling

Hello all,


I've update to Xcode Version 7.1 beta (7B75) and have suddenly a problem with a fetch request (line 10).


class func getObjects<T: NSManagedObject>(predicate: NSPredicate?, sortKey: String? = nil, sortAscending: Bool? = true) throws -> [T]? {
    let entityName = T.entityName()
    let fetchRequest = NSFetchRequest(entityName: entityName)
    if sortKey != nil {
      fetchRequest.sortDescriptors = [NSSortDescriptor(key: sortKey!, ascending: sortAscending!)]
    }
    if predicate != nil {
      fetchRequest.predicate = predicate
    }
    let fetchedResults = try? MPAppDelegate.moc.executeFetchRequest(fetchRequest)
    return fetchedResults as? [T]
  }
}


Previously (iOS 9, Xcode 7), the fetch request worked without problems, even with passing a computed property as the sortKey.

Now, an unhandled exception is thrown (even with a do-catch block):


2015-10-01 15:35:32.775 My People[34759:527829] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'keypath displayName not found in entity <NSSQLEntity MPContactEntry id=1>'
*** First throw call stack:
(
  0   CoreFoundation                      0x00000001107d4f45 __exceptionPreprocess + 165
  1   libobjc.A.dylib                     0x00000001124f0deb objc_exception_throw + 48
  2   CoreData                            0x000000011042e82f -[NSSQLGenerator newSQLStatementForRequest:ignoreInheritance:countOnly:nestingLevel:nestIsWhereScoped:] + 1583
  3   CoreData                            0x0000000110321c43 -[NSSQLGenerator newSQLStatementForFetchRequest:ignoreInheritance:countOnly:nestingLevel:nestIsWhereScoped:] + 35
  4   CoreData                            0x0000000110413f88 -[NSSQLAdapter _statementForFetchRequest:ignoreInheritance:countOnly:nestingLevel:] + 344
  5   CoreData                            0x0000000110321adc -[NSSQLAdapter _newSelectStatementWithFetchRequest:ignoreInheritance:] + 316
  6   CoreData                            0x0000000110321756 -[NSSQLCore newRowsForFetchPlan:] + 118
  7   CoreData                            0x000000011032100c -[NSSQLCore objectsForFetchRequest:inContext:] + 524
  8   CoreData                            0x00000001103209a9 -[NSSQLCore executeRequest:withContext:error:] + 377
  9   CoreData                            0x000000011040567f __65-[NSPersistentStoreCoordinator executeRequest:withContext:error:]_block_invoke + 3311
  10  CoreData                            0x000000011040ecad gutsOfBlockToNSPersistentStoreCoordinatorPerform + 189
  11  libdispatch.dylib                   0x00000001157d549b _dispatch_client_callout + 8
  12  libdispatch.dylib                   0x00000001157b9fb5 _dispatch_barrier_sync_f_invoke + 393
  13  CoreData                            0x00000001103ffda5 _perform + 197
  14  CoreData                            0x0000000110320614 -[NSPersistentStoreCoordinator executeRequest:withContext:error:] + 500
  15  CoreData                            0x000000011031edd3 -[NSManagedObjectContext executeFetchRequest:error:] + 579
  16  My People                           0x000000010fe26b45 _TZFC9My_People14CoreDataHelper10getObjectsuRdq_CSo15NSManagedObject_fMS0_FzTGSqCSo11NSPredicate_7sortKeyGSqSS_13sortAscendingGSqSb__GSqGSaq___ + 1413
  17  My People                           0x000000010fe247f0 _TZFC9My_People14MPContactEntryg10allEntriesGSqGSaS0___ + 160
  18  My People                           0x000000010fe2fb9c _TFC9My_People14MPMasterListVC11viewDidLoadfS0_FT_T_ + 92
  19  My People                           0x000000010fe2fc82 _TToFC9My_People14MPMasterListVC11viewDidLoadfS0_FT_T_ + 34
  20  UIKit                               0x0000000111185590 -[UIViewController loadViewIfRequired] + 1344
  21  UIKit                               0x00000001111c9246 -[UINavigationController _layoutViewController:] + 54
  22  UIKit                               0x00000001111c9b1a -[UINavigationController _updateScrollViewFromViewController:toViewController:] + 462
  23  UIKit                               0x00000001111c9c8c -[UINavigationController _startTransition:fromViewController:toViewController:] + 126
  24  UIKit                               0x00000001111caee5 -[UINavigationController _startDeferredTransitionIfNeeded:] + 890
  25  UIKit                               0x00000001111cbe42 -[UINavigationController __viewWillLayoutSubviews] + 57
  26  UIKit                               0x00000001113714e5 -[UILayoutContainerView layoutSubviews] + 248
  27  UIKit                               0x00000001110a79f8 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 710
  28  QuartzCore                          0x000000011529858a -[CALayer layoutSublayers] + 146
  29  QuartzCore                          0x000000011528ce60 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 366
  30  QuartzCore                          0x000000011528ccde _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24
  31  QuartzCore                          0x0000000115281465 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 277
  32  QuartzCore                          0x00000001152aebfa _ZN2CA11Transaction6commitEv + 486
  33  UIKit                               0x000000011101deda _afterCACommitHandler + 174
  34  CoreFoundation                      0x0000000110700947 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
  35  CoreFoundation                      0x00000001107008b7 __CFRunLoopDoObservers + 391
  36  CoreFoundation                      0x00000001106f650b __CFRunLoopRun + 1147
  37  CoreFoundation                      0x00000001106f5e08 CFRunLoopRunSpecific + 488
  38  GraphicsServices                    0x0000000115063ad2 GSEventRunModal + 161
  39  UIKit                               0x0000000110ff3031 UIApplicationMain + 171
  40  My People                           0x000000010fe33dbd main + 109
  41  libdyld.dylib                       0x000000011580a92d start + 1
  42  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException


Shouldn't try? just assign nil to fetchedResults?

Replies

Key Value Coding throws an exception when a property doesn't exist, because it is considered programmer error.

That's beyond the normal error handling that try? covers, which covers cases where methods would return an NSError.


Have you tried explicitly marking the displayName computed property in MPContactEntry as @objc? Maybe it is no longer visible to obj-C code for some reason.

Thank you, @LCS.


Yes, I tried this as well, same result.

I've tried both as computed and transient property; the result is the same error.

In the meantime, I will avoid calling this function with a transient/computed property, and it works.

Previously (iOS 9, Xcode 7), the fetch request worked without problems, even with passing a computed property as the sortKey.

That’s very surprising. Core Data has always had the requirement that the sort key must be stored in the database. A ’net search for “keypath transient not found in entity” will turn up a bunch of folks who’ve been bitten by this problem.

Share and Enjoy

Quinn "The Eskimo!"
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

"Core Data has always had the requirement that the sort key must be stored in the database."

Thanks eskimo, found this after trying to use a computed property in a swift extension as sort key in an objective C FRC! So after a few days of trying, I'll just go back to the original model...