开发者

Core Data trouble ordering NSSet of related objects

Newbie to Core Data here:

Given a patient, I want to list all the allergies for that patient.

The en开发者_StackOverflowtity Patient has a to-many relationship allergies defined in the Object Model.

The child entity Allergy has an inverse relationship patient also defined.

Allergy has one attribute, description who's value is an NSString.

I want to get the NSSet returned by the allergies relationship, order it by description, and put into an array instance variable that will be mapped to the table view.

The code:

NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"description" ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];  
NSSet *unorderedAllergies = [selectedPatient valueForKey:@"allergies"];  
NSArray *sortedArray = [unorderedAllergies sortedArrayUsingDescriptors:sortDescriptors];  
[allergiesArray addObjectsFromArray:sortedArray];

Notes: selectedPatient is an instance variable of the parent NSManagedObject Patient that was passed in from a previous controller. allergiesArray is the array I want mapped to the table view.

The problem occurs at:

NSArray *sortedArray = [unorderedAllergies sortedArrayUsingDescriptors:sortDescriptors]; 

The debugger shows an EXEC_BAD_ACCESS at this line and the app crashes. My guess is it has to do with the related objects being lazily loaded by Core Data (i.e. "faulted objects").

Any help is greatly appreciated.


description is a method name of NSObject that returns a string describing the object. It is the method called when you use NSLog to log an object. All classes that inherit from NSObject already have a description method.

Don't use description as a attribute or property name because you might not get what you expect. Use allegryDescription or something similar to change the property name to something that won't conflict.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜