3

enter image description here

I currently have two entities called as Person and Rem.

Person entity has a relationship called theRem with Rem and Person is a parent Event.

Rem entity has a attribute called Date which i need to retrieve using the Person entity,how do i do this?

here is my code:

AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication]delegate];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
// Edit the entity name as appropriate.  
NSManagedObjectContext *managedObjectContext=[appDelegate managedObjectContext];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Person" inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];
 NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc]initWithKey:@"Date" ascending:YES];
[fetchRequest setSortDescriptors:@[sortDescriptor]];
 //Fetch from core data in background thread
NSError *error;
allArray = [managedObjectContext executeFetchRequest:fetchRequest error:&error];

i know the above code doesnt work,but somewhere in the apple documentation i read i could try something like this

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc]initWithKey:@"theRem.Rem" ascending:YES];

can anyone help me out? Thanks

vin
  • 1,258
  • 5
  • 20
  • 33
  • You need to set up the relationships properly, one to one, one to many or many. Set the inverse relationship. Make `ManagedObject` subclasses. Fetch the parent and by default you would get the children objects. – Anupdas May 23 '13 at 10:09

2 Answers2

1

I have not tried this, but it should work, considering the name of relationship from Person to Rem is hasRem and date is a property of Rem

[request setPropertiesToFetch:@[@"hasRem.date"]];

Refer : Link to Fetch Distinct Values

Puneet Sharma
  • 9,369
  • 1
  • 27
  • 33
0

I guess you are confusing predicate with sort descriptor. When creating a predicate you could specify a key path (@"theRem.date") to reach out to your relationship objects property. Thats not the case with sort descriptor.
Since you have the person object, for accessing the relationship object's date use the following code:

AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication]delegate];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
// Edit the entity name as appropriate.  
NSManagedObjectContext *managedObjectContext=[appDelegate managedObjectContext];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Person" inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];
NSError *error;
allArray = [managedObjectContext executeFetchRequest:fetchRequest error:&error];

//Fetch the date for all person objects

for (Person *perObj in allArray){
   NSLog("Date of rem: %@", perObj.theRem.date);
}

// If you don't have a Person class created you can also use the below for loop instead:

for(NSManagedObject *perObj in allArray){
   NSLog("Date of rem: %@", [[perObj valueForKey:@"theRem"] valueForKey:@"date"]];
}
Rakesh
  • 3,370
  • 2
  • 23
  • 41
  • the above code doesnt work for me..especially the line perObj.theRem.date,it gives me an error – vin May 24 '13 at 05:44
  • What is the error you are getting? For this to work you should have an NSManagedObject subclass called Person (which you can create from your data model). And is this the functionality you want? Fetch all the person objects and access the date from the relationship? – Rakesh May 24 '13 at 06:02
  • property date not found on object of type NSSet * – vin May 24 '13 at 06:07
  • Alright that is because i think you have set up Person to Rem relationship as 1:m(one-to-many). Is that what you want? If it is do you want the date of all m objects (of type Rem)? – Rakesh May 24 '13 at 06:09
  • well the for loop which you added is working fine,but i need to sort them according to the date which i have got,what do i do for that? – vin May 24 '13 at 06:14
  • Since you have the objects in an array and the value based on which you want to use is part of the relationship , you can sort the array like any array without using NSSortDescriptor. For that look http://stackoverflow.com/questions/805547/how-to-sort-an-nsmutablearray-with-custom-objects-in-it or http://stackoverflow.com/questions/2780590/sorting-an-array-of-objective-c-objects . – Rakesh May 24 '13 at 06:33