Group by in core data in ios swift not working

前端 未结 1 1633
旧巷少年郎
旧巷少年郎 2021-01-20 21:59

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 bas

相关标签:
1条回答
  • 2021-01-20 22:32

    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)
    
    0 讨论(0)
提交回复
热议问题