Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone Core Data objects added to NSMutableArray

I am trying to have a bunch of Core Data objects (NSStrings) be added to an NSMutableArray so that I pickerView can use the strings as its data values.

Currently I am able to save strings from a textField one at a time into the sqlite file, in Core Data.

I am having trouble pulling those Objects back out of the Core Data sqlite file and into a NSMutable array.

This is the code to add objects to the sqlite file...

-(IBAction)addToPicker
{
 CoreDataPickerAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
 NSManagedObjectContext *context = [appDelegate managedObjectContext]; 
 NSManagedObject *thePerson = [NSEntityDescription insertNewObjectForEntityForName:@"Event" inManagedObjectContext:context];

 [thePerson setValue:textField.text forKey:@"eventName"];

 [context save:&error];

 textField.text=@"";
 self.viewDidLoad;

 [textField resignFirstResponder];
}

Right now I have...

- (void)viewDidLoad {

 NSMutableArray *array = [[NSMutableArray alloc] init];

        //code needed to send all the objects in the sqlite Core Data store into array to be read by the pickerView

        self.pickerData = array;   // the picker uses NSMutableArray pickerData for its data

 [array release];

 [super viewDidLoad];

}

any help would greatly be appreciated. Chris

like image 766
OscarTheGrouch Avatar asked Dec 21 '25 04:12

OscarTheGrouch


1 Answers

You can use the NSFetchRequest to fetch the data into the array. Take a look at the Core Data Programming guide - Fetching Managed Object. You just need to read the first section of the link.

Executing a fetch request will return an array

NSArray *array = [moc executeFetchRequest:request error:&error];
like image 95
Hoang Pham Avatar answered Dec 22 '25 19:12

Hoang Pham



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!