【问题标题】:Group by in core data in ios swift not workingios swift中核心数据中的分组无法正常工作
【发布时间】:2015-08-11 07:23:36
【问题描述】:

我是 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

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

【问题讨论】:

    标签: ios swift core-data


    【解决方案1】:

    终于可以解决问题了。虽然我没有目标 C 的知识,但我已经在目标 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)
    

    【讨论】:

    • 但是现在您按 order_num AND cut_name 进行分组,这不是您想要的。您只想按 order_num 分组并获取 order_num、cust_name 和品牌。不幸的是,NSFetchRequest 似乎不可能做到这一点:-(
    • 可能这是一个很好的解决方案 - stackoverflow.com/questions/4865686/…
    猜你喜欢
    • 2018-01-02
    • 1970-01-01
    • 2016-01-05
    • 1970-01-01
    • 2018-07-11
    • 1970-01-01
    • 2015-07-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多