0

In my core date in the entity name called Event and in that there is an attribute called "name". I want to fetch all the values of term from coredata to an nsarray. I used the below code and it is not working. Anybody please helpp.

NSFetchRequest *request = [[NSFetchRequest alloc] init];

[request setEntity:[NSEntityDescription entityForName:@"Event" inManagedObjectContext:managedObjectContext]];

NSError *error = nil;
NSArray *events = [managedObjectContext executeFetchRequest:request error:&error];
NSAssert2(events != nil && error == nil, @"Error fetching events: %@\n%@", [error localizedDescription], [error userInfo]);
NSMutableArray *namesArray = [[NSMutableArray alloc]init];
namesArray = [events valueForKey:@"name"];
7
  • what statement isn't working? Commented Jul 9, 2010 at 5:25
  • I want to load all values of names to an array. But it is not loading. Program crashes with error message " *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSCFArray objectAtIndex:]: index (0) beyond bounds (0)'" Please help. Commented Jul 9, 2010 at 5:28
  • It looks like your request is executing successfully but finding no Events. Are you sure there are Events in your store? What happens if you try inserting and saving one right before this section of code? Commented Jul 9, 2010 at 6:17
  • chrispix suggestion sounds good, additionaly check events.count for the actual number of events found. Commented Jul 9, 2010 at 6:22
  • Yes exactly that was the problem. Now I am having another problem. Can you tell, how can we convert the values returned by [events valueForKey:@"name"]; to NSString format. Commented Jul 9, 2010 at 6:48

1 Answer 1

2

Your code is close and should have worked even though you were leaking memory.

NSFetchRequest *request = [[NSFetchRequest alloc] init];

[request setEntity:[NSEntityDescription entityForName:@"Event" inManagedObjectContext:managedObjectContext]];

NSError *error = nil;
NSArray *events = [managedObjectContext executeFetchRequest:request error:&error];
NSAssert2(events != nil && error == nil, @"Error fetching events: %@\n%@", [error localizedDescription], [error userInfo]);
//You were leaking your request here
[request release], request = nil;
//The following line is redundant.  You are leaking an array here
//NSMutableArray *namesArray = [[NSMutableArray alloc]init];
NSArray *namesArray = [events valueForKey:@"name"];

At this point you should have an array of names which are NSString instances.

The next question is -- Why? Why do you need to pull them out into an array of strings when you already have the NSManagedObject instances? Why disconnect the data from the Core Data objects.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.