Reputation: 2105
I am php web developer. I have learnt ios swift recently. I am trying to fetch the records from core data and group the records based on one column or more appropriately based on one attribute in ios swift. I have tried the solutions from Stack Overflow but none of them are working for me. So please someone can point out what I'm doing wrong. Here is my code:
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)
And this is the error message:
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
I have tried the solution from here: Swift Core Data - Group doesn't Work
If any further information needed please let me know.
Upvotes: 4
Views: 7765
Reputation: 2105
Finally I could resolved the issue. Though I dont have knowledge in objective C, I have searched for the same issue in objective C and got the idea to resolve my problem in Swift. It could help someone who might be facing the same problem, so im sharing my solution here.
The issue was propertiesToGroupBy must contain all the elements which are to be fetched by propertiesToFetch. For clarity I am pasting those two lines of code separately here:
request.propertiesToGroupBy = ["order_num","cust_name"]
request.propertiesToFetch = ["order_num","cust_name"]
Observe the above lines of code have both elements(attributes) order_num and cust_name. (this is merely an example from my code. but we can add as many elements we want)
Here is the full code solution for my above question:
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)
Upvotes: 10