Reputation: 95
I am trying to fetch the stateName in below code and show it in a label. but it always returns fault.
-(NSArray *) getStateNameFromCode :(NSString *) stateCode
{
NSManagedObjectContext *objectContext = [self managedObjectContext ];
NSEntityDescription *entity = [NSEntityDescription entityForName:ENTITY_STATES inManagedObjectContext:objectContext];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K == %@", COLUMN_STATE_CODE, stateCode];
NSLog(@"Predicate : %@" ,predicate);
NSFetchRequest *fetchRequest = [[ NSFetchRequest alloc]init];
[fetchRequest setEntity:entity];
[fetchRequest setPredicate:predicate];
NSArray *stateName = [objectContext executeFetchRequest:fetchRequest error:Nil];
if (stateName.count > 0)
{
return [stateName objectAtIndex:0];
}
return nil ;
}
self.labelTeams.text = [NSString stringWithFormat:@"%@\n" ,
[sharedController getStateNameFromCode:_checklist.stateHomeTeam]]
;
OUTPUT:
<States: 0xa571560> (entity: States; id: 0xa565300 <x-coredata://D6B54D51-4556-41E1-A372-F39EB871A076/States/p8> ; data: <fault>)
Tried using setreturnsobjectsasfaults=NO but thats not working. found so many links most of them say to use setreturnsobjectsasfaults . I killing my brian. Any ideas will be appreciated.
Regards,
Upvotes: 0
Views: 547
Reputation: 5182
Try this,
//return state name as NSString (assumes state name is string type)
-(NSString *) getStateNameFromCode :(NSString *) stateCode
{
....
NSArray *states = [objectContext executeFetchRequest:fetchRequest error:Nil];
if (states.count > 0)
{
//Matched state object
NSManagedObject *state = [states objectAtIndex:0];
//or States *state = [states objectAtIndex:0];, if States is the NSManagedObject subclass name
return //return name of state from state object, like state.name
}
return nil ;
}
self.labelTeams.text = [NSString stringWithFormat:@"%@\n" ,
[sharedController getStateNameFromCode:_checklist.stateHomeTeam]]
;
Upvotes: 1