开发者

Write SortDescriptor for a transforable attribute in core data?

开发者 https://www.devze.com 2023-01-08 22:09 出处:网络
I have an entity header and it has a attribute NSDictionary wich was declared transformable in model. Now I want to not sort the array of headers in fetchedRequestController. And passing nil or null o

I have an entity header and it has a attribute NSDictionary wich was declared transformable in model. Now I want to not sort the array of headers in fetchedRequestController. And passing nil or null object gives error. Please help its urgent.

Let me reframe from the last time : if i have an entity, with a transformable attribute headers. I change the id type to NSDictionary in the generated class. I now need to access keys of the dictionary as entity.attribute name.key ... i get an error as this class is not key value coding complaint for the key :(key) ... what is a work around this problem.

// Create and configure a fetch request with the Book entity. NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; NSEntityDescription *entity = [NSEntityDescription entityForName:@"headers" inManagedObjectContext:self.tableCellGenericMoc]; [fetchRequest setEntity:entity]; // Create the sort descriptors array.开发者_StackOverflow中文版 NSSortDescriptor *headersDescriptor = [[NSSortDescriptor alloc] initWithKey:@"headersDictionary" ascending:NO]; NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:headersDescriptor, nil]; [fetchRequest setSortDescriptors:sortDescriptors]; // Create and initialize the fetch results controller. NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.tableCellGenericMoc sectionNameKeyPath:@"headresDictionary" cacheName:@"Root"]; self.fetchedResultsController = aFetchedResultsController; fetchedResultsController.delegate = self;

the above selector gives an error cause it can not sort the headersDictionary...


Why are you storing a NSDictionary in Core Data? That is to put it simply, doing it wrong. If you want something like a dictionary just create a child object on the other end of a one to many relationship that has a name and a value.

If you design your model correctly then you will not have the problem you are running into now and you can use a predicate to filter and a sort descriptor in your NSFetchRequest directly.

Update

ohh thankYou ... the problem is the dictionaries i am getting from the web does not have a fixed Keys structure .. so I have to get the dictionary as it is as a transformable attribute

Still does not make any sense. The design I described is identical to having a dictionary inside a transformable object except that it is useful at the Core Data level. You do not need to have fixed keys in the design I described.

Rethink your design and this question becomes irrelevant.

0

精彩评论

暂无评论...
验证码 换一张
取 消