ios - ios swift 中的核心数据分组不起作用

标签 ios swift core-data

我是 php 网络开发人员。最近学习了ios swift。我正在尝试从核心数据中获取记录,并根据 ios swift 中的一个属性根据一列或更适本地对记录进行分组。我已经尝试过 Stack Overflow 的解决方案,但没有一个对我有用。所以请有人指出我做错了什么。这是我的代码:

    var context : NSManagedObjectContext = appdel.managedObjectContext!

    var request = NSFetchRequest(entityName: "TblOrders")
    request.returnsObjectsAsFaults = false
    request.propertiesToGroupBy = ["order_num"]
    request.propertiesToFetch = ["cust_name", "brand"]
    request.resultType = .DictionaryResultType
    context.executeFetchRequest(request, error: nil)!
    var results = context.executeRequest(request, error: nil)!

    println(results)

这是错误信息:

2015-08-11 10:54:30.423 newapp[470:6013] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'SELECT clauses in queries with GROUP BY components can only contain properties named in the GROUP BY or aggregate functions ((<NSAttributeDescription: 0x7fa8ca5b4b70>), name cust_name, isOptional 1, isTransient 0, entity TblOrders, renamingIdentifier cust_name, validation predicates (
), warnings (
), versionHashModifier (null)
 userInfo {
}, attributeType 700 , attributeValueClassName NSString, defaultValue (null) is not in the GROUP BY)'
*** First throw call stack:
(
    0   CoreFoundation                      0x0000000101e35c65 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x00000001039a0bb7 objc_exception_throw + 45
    2   CoreData                            0x0000000101a4fbbc -[NSSQLGenerator newSQLStatementForRequest:ignoreInheritance:countOnly:nestingLevel:] + 1724
    3   CoreData                            0x0000000101a3bdc4 -[NSSQLAdapter _statementForFetchRequest:ignoreInheritance:countOnly:nestingLevel:] + 244
    4   CoreData                            0x0000000101953e0c -[NSSQLAdapter _newSelectStatementWithFetchRequest:ignoreInheritance:] + 316
    5   CoreData                            0x0000000101953a86 -[NSSQLCore newRowsForFetchPlan:] + 118
    6   CoreData                            0x000000010195333c -[NSSQLCore objectsForFetchRequest:inContext:] + 524
    7   CoreData                            0x0000000101952dbb -[NSSQLCore executeRequest:withContext:error:] + 299
    8   CoreData                            0x0000000101a2da6c __65-[NSPersistentStoreCoordinator executeRequest:withContext:error:]_block_invoke + 3356
    9   CoreData                            0x0000000101a36c30 gutsOfBlockToNSPersistentStoreCoordinatorPerform + 192
    10  libdispatch.dylib                   0x00000001040a2614 _dispatch_client_callout + 8
    11  libdispatch.dylib                   0x0000000104088002 _dispatch_barrier_sync_f_invoke + 365
    12  CoreData                            0x0000000101a28245 _perform + 197
    13  CoreData                            0x0000000101952a58 -[NSPersistentStoreCoordinator executeRequest:withContext:error:] + 504
    14  CoreData                            0x00000001019512ca -[NSManagedObjectContext executeFetchRequest:error:] + 586
    15  newapp                              0x000000010165ea9a _TFC6newapp25OrderStatusViewController11viewDidLoadfS0_FT_T_ + 1418
    16  newapp                              0x000000010165efe2 _TToFC6newapp25OrderStatusViewController11viewDidLoadfS0_FT_T_ + 34
    17  UIKit                               0x00000001028051d0 -[UIViewController loadViewIfRequired] + 738
    18  UIKit                               0x00000001028053ce -[UIViewController view] + 27
    19  UIKit                               0x000000010282a257 -[UINavigationController _startCustomTransition:] + 633
    20  UIKit                               0x000000010283637f -[UINavigationController _startDeferredTransitionIfNeeded:] + 386
    21  UIKit                               0x0000000102836ece -[UINavigationController __viewWillLayoutSubviews] + 43
    22  UIKit                               0x00000001029816d5 -[UILayoutContainerView layoutSubviews] + 202
    23  UIKit                               0x00000001027549eb -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 536
    24  QuartzCore                          0x0000000106522ed2 -[CALayer layoutSublayers] + 146
    25  QuartzCore                          0x00000001065176e6 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 380
    26  QuartzCore                          0x0000000106517556 _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24
    27  QuartzCore                          0x000000010648386e _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 242
    28  QuartzCore                          0x0000000106484a22 _ZN2CA11Transaction6commitEv + 462
    29  QuartzCore                          0x00000001064850d3 _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 89
    30  CoreFoundation                      0x0000000101d68ca7 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
    31  CoreFoundation                      0x0000000101d68c00 __CFRunLoopDoObservers + 368
    32  CoreFoundation                      0x0000000101d5ea33 __CFRunLoopRun + 1123
    33  CoreFoundation                      0x0000000101d5e366 CFRunLoopRunSpecific + 470
    34  GraphicsServices                    0x0000000105e09a3e GSEventRunModal + 161
    35  UIKit                               0x00000001026d48c0 UIApplicationMain + 1282
    36  newapp                              0x0000000101678747 main + 135
    37  libdyld.dylib                       0x00000001040d6145 start + 1
    38  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

我已经尝试过这里的解决方案: Swift Core Data - Group doesn't Work

如果需要任何进一步的信息,请告诉我。

最佳答案

终于解决了这个问题。虽然我不了解 objective-c ,但我在 objective-c 中搜索了相同的问题,并得到了在 Swift 中解决我的问题的想法。它可以帮助可能面临同样问题的人,所以我在这里分享我的解决方案。

问题是 propertiesToGroupBy 必须包含将由 propertiesToFetch 获取的所有元素。为清楚起见,我将这两行代码分别粘贴到此处:

    request.propertiesToGroupBy = ["order_num","cust_name"]
    request.propertiesToFetch = ["order_num","cust_name"]

观察上面的代码行有两个元素(属性)order_num 和cust_name。 (这只是我的代码中的一个示例。但我们可以添加任意数量的元素)

这是我上述问题的完整代码解决方案:

    var context : NSManagedObjectContext = appdel.managedObjectContext!
    var request = NSFetchRequest(entityName: "TblOrders")
    request.returnsObjectsAsFaults = false
    request.propertiesToGroupBy = ["order_num","cust_name"]
    request.propertiesToFetch = ["order_num","cust_name"]
    request.resultType = .DictionaryResultType
    context.executeFetchRequest(request, error: nil)!
    var res = context.executeFetchRequest(request, error: nil)!
    var results = res as NSArray
    println(results)

关于ios - ios swift 中的核心数据分组不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31935618/

相关文章:

ios - UiCollectionView 间距问题 - swift

ios - 如何通过 iOS 应用程序中的 UIPickerView 实现 View 之间的转换?

ios - swift : "Mixing in"属性

swift - 如何在 Swift 中将泛型和 Any.Type 与函数一起使用

ios - 从核心数据中删除特定对象 (Swift)

iOS黑屏大问题

android - xamarin表单上的“Can' t Write”错误

ios - Swift 3 过滤 Json 和重新排序

ios - 在 Swift 5 中配置核心数据持久性

ios - CoreDataBooks 问题断言失败。