Swift Core Data - Group doesn't Work

坚强是说给别人听的谎言 提交于 2019-12-08 12:15:12

问题


I am working on a simple iOS Swift app and can't find any solution for a small problem with core data. I have to group some data which i fetch out of core data, but it doesn't work.

This is my fetchRequest:

<NSFetchRequest: 0x7f82fbe105e0> (entity: Sets; predicate: ((null)); sortDescriptors: ((
    "(sort, ascending, compare:)"
)); type: NSDictionaryResultType; includesPendingChanges: NO; propertiesToFetch: ((
    name
)); propertiesToGroupBy: ((
    name
)); )

I have sort and added properties to group by. There is also set a result type - DictionaryResultType.

And here is my FetchRequest:

fetchRequest = NSFetchRequest(entityName:entity)
fetchRequest.propertiesToGroupBy = groupDescriptors
fetchRequest.propertiesToFetch = propertiesToFetch
fetchRequest.resultType = .DictionaryResultType
let fetchedResults = managedContext.executeFetchRequest(fetchRequest as NSFetchRequest, error: &error) as? [NSManagedObject]
  • Entity is a string variabel
  • groupDescriptors and propertiesToFetch are arrays of String

回答1:


Ok I found a solution and it's just simple.

managedContext.executeFetchRequest(fetchRequest as NSFetchRequest, error: &error) as? [NSManagedObject]

generates only NSManagedObject's. If you change the fetch request to

managedContext.executeRequest(fetchRequest, withContext: managedContext as! NSManagedObjectContext, error: nil) as! [AnyObject]

you will receive your data.



来源:https://stackoverflow.com/questions/30473737/swift-core-data-group-doesnt-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!