Error when populating table view with coredata - iphone

I am working with coredata and successfully saved the data in data storage but when I try to output what I stored using table view ,I am not able to do it.
Here the user will enter and click save ..so when he goes back..I want the table to populated with what name he or she has entered.
So I wrote the below code for saving and find data:
- (void) saveData
{
coredata123AppDelegate *appDelegate =
[[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context =
[appDelegate managedObjectContext];
Contacts *newContact;
newContact = [NSEntityDescription
insertNewObjectForEntityForName:#"Contacts"
inManagedObjectContext:context];
[newContact setValue:name.text forKey:#"name"];
[newContact setValue:address.text forKey:#"address"];
[newContact setValue:phone.text forKey:#"phone"];
name.text = #"";
address.text = #"";
phone.text = #"";
NSError *error;
[context save:&error];
status.text = #"Contact saved";
}
- (void) findContact
{
coredata123AppDelegate *appDelegate =
[[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context =
[appDelegate managedObjectContext];
NSEntityDescription *entityDesc =
[NSEntityDescription entityForName:#"Contacts"
inManagedObjectContext:context];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entityDesc];
NSPredicate *pred =
[NSPredicate predicateWithFormat:#"(name = %#)",
name.text];
[request setPredicate:pred];
NSManagedObject *matches = nil;
NSError *error;
NSArray *objects = [context executeFetchRequest:request
error:&error];
for(Contacts *info in objects)
{
NSLog(#"Name:%#",info.name);
NSLog(#"Address:%#",info.address);
}
if ([objects count] == 0) {
status.text = #"No matches";
} else {
matches = [objects objectAtIndex:0];
address.text = [matches valueForKey:#"address"];
phone.text = [matches valueForKey:#"phone"];
status.text = [NSString stringWithFormat:
#"%d matches found", [objects count]];
}
[request release];
}
And to populate the table view i have written this code..
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription
entityForName:#"Contacts" inManagedObjectContext:context];
[fetchRequest setEntity:entity];
NSError *error;
self.contacts = [context executeFetchRequest:fetchRequest error:&error];
[fetchRequest release];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
Contacts *info =[contacts objectAtIndex:indexPath.row];
cell.textLabel.text =info.name;
cell.detailTextLabel.text=info.address;
// Configure the cell...
return cell;
}

Are you getting proper number of elements in this objects array?
NSArray *objects = [context executeFetchRequest:request
error:&error];
if not, try changing the predicate with,
[NSPredicate predicateWithFormat:#"name=%#", name.text];
Otherwise, your code seems correct to me.

Related

Update value core data

I need to update the value of the desired cell in my core data. I have a cell (ID) with a known value to me. I need to find the value and replace. I know how to change the elements of the array. The following shows how I do it. But I have to change the field (ID) in MyBase. value that I need to change will be equal to fromIndexPath.row. The value on which I need to change toIndexPath.row.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath
*)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
if (fromIndexPath.section == toIndexPath.section) {
NSFetchRequest* fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription* entityDescription = [NSEntityDescription entityForName:#"MyBase"
inManagedObjectContext:self.objectContext];
[fetchRequest setEntity:entityDescription];
empArray = [(NSArray*)[self.objectContext executeFetchRequest:fetchRequest error:nil] mutableCopy];
MyBase *employee = [empArray objectAtIndex:fromIndexPath.row];
[empArray removeObjectAtIndex:fromIndexPath.row];
[empArray insertObject:objectToMove atIndex:toIndexPath.row];
}
}
for update core data, you can try this:
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:#:#"Employee"
inManagedObjectContext:self.managedObjectContext];
[request setEntity:entity];
NSPredicate *pred = [NSPredicate predicateWithFormat:[NSString stringWithFormat:#"empId = %d", 12345]];
[request setPredicate:pred];
NSArray *empArray=[self.managedObjectContext executeFetchRequest:request error:nil];
[request release];
if ([empArray count] > 0){
Employee *employee = [empArray objectAtIndex:fromIndexPath.row];;
employee.empSalary=[NSNumber numberWithInt:45000];
employee.empName=#"John";
employee.empDesignation=#"Analysist";
employee.empExp=#"4 Years";
[self.managedObjectContext save:nil];

Table view and Predicates

I have a table view with 40 objects. I have to filter them by gender when Click in a UISegment (male, female and both). It seems to be working, but table view does not refresh. Please any help would be appreciatte.
-(void)viewDidLoad
{
[super viewDidLoad];
self.navigationItem.title = #"People";
[self loadall];
[[NSBundle mainBundle] loadNibNamed:#"FilterSortView" owner:self options:nil];
self.filterControl.selectedSegmentIndex = -1;
[self.filterControl addTarget:self action:#selector( changeSegge ) forControlEvents:UIControlEventValueChanged];
}
#
My second method to filter by gender
- (void)changeSegge
{
NSEntityDescription *personEntity = [NSEntityDescription entityForName:#"Person"
inManagedObjectContext:self.managedObjectContext];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:personEntity];
NSError *error = nil;
self.people = [self.managedObjectContext executeFetchRequest:request error:&error];
if (error)
{
[NSException raise:NSInternalInconsistencyException format:#"Could not fetch Core Data records: %#",error];
}
if(filterControl.selectedSegmentIndex == 0){
NSPredicate *predicatem =[NSPredicate predicateWithFormat:#"gender == %#", #"m" ];
request.predicate=predicatem;
[self.tableView reloadData];
[request release];
NSLog(#"button 1");
}
Thanks a lot.
You forgot to fill people array after predicate set to fetch request.
Here is updated changeSegge method:
- (void)changeSegge
{
NSEntityDescription *personEntity = [NSEntityDescription entityForName:#"Person"
inManagedObjectContext:self.managedObjectContext];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:personEntity];
NSError *error = nil;
self.people = [self.managedObjectContext executeFetchRequest:request error:&error];
if (error)
{
[NSException raise:NSInternalInconsistencyException format:#"Could not fetch Core Data >records: %#",error];
}
if(filterControl.selectedSegmentIndex == 0){
NSPredicate *predicatem =[NSPredicate predicateWithFormat:#"gender == %#", #"m" ];
request.predicate=predicatem;
self.people = [self.managedObjectContext executeFetchRequest:request error:&error];
[self.tableView reloadData];
[request release];
NSLog(#"button 1");
}

Display two entities into one table view

i have a custom table view cell. With two labels.
The Label on the right loads its text from an entity. (See picture Below)
-(void)viewWillAppear:(BOOL)animated{
if (self.context == nil)
{
self.context = [(RootAppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
}
NSFetchRequest *request = [[NSFetchRequest alloc]init];
NSEntityDescription *entity = [NSEntityDescription entityForName:#"Entity" inManagedObjectContext:self.context];
[request setEntity:entity];
NSError *error;
NSMutableArray *array = [[self.context executeFetchRequest:request error:&error] mutableCopy];
[self setTableArray:array];
[self.myTableView reloadData];
[super viewWillAppear:YES];
}
And i set the text accordingly as per documentation.
My problem lies when i try to load the second labels text from a different entity, So simply, i tried this:
-(void)viewWillAppear:(BOOL)animated{
if (self.context == nil)
{
self.context = [(RootAppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
}
NSFetchRequest *request = [[NSFetchRequest alloc]init];
NSFetchRequest *sRequest = [[NSFetchRequest alloc]init];
NSEntityDescription *entity = [NSEntityDescription entityForName:#"Entity" inManagedObjectContext:self.context];
NSEntityDescription *sEntity = [NSEntityDescription entityForName:#"Entity2" inManagedObjectContext:self.context];
[request setEntity:entity];
[sRequest setEntity:sEntity];
NSError *error;
NSError *sError;
NSMutableArray *array = [[self.context executeFetchRequest:request error:&error] mutableCopy];
NSMutableArray *sArray = [[self.context executeFetchRequest:sRequest error:&sError]mutableCopy];
[self setTableArray:array];
[self setSecondTableArray:sArray];
[self.ammoTable reloadData];
[super viewWillAppear:YES];
}
And i set the table View's cell's text via:
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString * identifier = #"identifier";
self.cell = [tableView dequeueReusableCellWithIdentifier:identifier];
Entity2 *entity2 = [self.secondTableArray objectAtIndex:indexPath.row];
Entity *entity = [self.tableArray objectAtIndex:indexPath.row];
self.cell.rightLabel.text = [[entity.brand stringByAppendingString:#" "] stringByAppendingString:entity.model];
self.cell.leftLabel.text = entity2.numberOfRounds;
return self.cell;
}
and i get this error, when i push this view:
*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array'
and i get a SIGABRT error on this line:
Entity2 *entity2 = [self.secondTableArray objectAtIndex:indexPath.row];
Any help would be greatly appreciated, Thank you!
Instead of trying to load 2 different entities, i just added another attribute to the original entity, and i imported/used the newly created attribute in the entity to populate the right label!

Not Able to Retrieve data using core data

I am using coredata in my application to store data.i have to add the data in one view controller and retrieve it in another view controller.i tried the following code but it is not working.
//addViewController.m
-(IBAction)save:(id)sender
{
CoreDataOneAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context = [appDelegate managedObjectContext];
NSManagedObject *newContact;
newContact = [NSEntityDescription insertNewObjectForEntityForName:#"Employee"
inManagedObjectContext:context];
[newContact setValue:name.text forKey:#"name"];
[newContact setValue:amount.text forKey:#"amount"];
name.text = #"";
amount.text = #"";
//label
status.text = #"saved";
NSError *error;
[context save:&error];
}
I want to retrieve the values and display them in a tableView
//retrieveViewController.m
- (void)viewDidLoad
{
objects = [[NSArray alloc]init];
CoreDataOneAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context = [appDelegate managedObjectContext];
NSEntityDescription *entityDesc = [NSEntityDescription entityForName:#"Employee"
inManagedObjectContext:context];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entityDesc];
NSError *error;
objects = [context executeFetchRequest:request
error:&error];
[request release];
[super viewDidLoad];
}
//tableView
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [objects count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
labelOne = [[UILabel alloc]initWithFrame:CGRectMake(5, 11, 110, 21)];
labelTwo = [[UILabel alloc]initWithFrame:CGRectMake(230, 11, 70, 21)];
static NSString *cellIdentifier = #"CellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:cellIdentifier]autorelease];
}
[cell.contentView addSubview:labelTwo];
[cell.contentView addSubview:labelOne];
NSManagedObject *matches = nil;
matches = [objects objectAtIndex:indexPath.row];
NSString *str1=[NSString stringWithFormat:#"%#",[matches valueForKey:#"name"]];
labelOne.text = str1;
NSString *str2=[NSString stringWithFormat:#"%#",[matches valueForKey:#"amount"]];
labelTwo.text = str2;
return cell;
}
I am getting EXC_BAD_ACCESS error.i tried using NSZombieEnabled and i got the following error.
2012-04-27 11:59:18.153 CoreDataOne[4370:207] *** -[_PFArray objectAtIndex:]: message sent to deallocated instance 0x5931e40
i am able to retrieve the values if write the code what i have written in viewDidLoad in cellForRowAtIndexPath but how to declare the numberOfRows.
It looks like you are not using ARC. I think you need to retain the result of your fetch request in viewDidLoad (don't forget to release it in dealloc). Also, you are leaking by alloc/initing an array and then overwriting it.
Store the values in MutableArray in view didload where you are retrieving values. And then use it in your table view. Declare the noof Rows as array count.

UITableView cellForRowAtIndexPath causing slow scrolling

My table is scrolling incredibly slow, and I think it's caused by the Core Data methods within my cellForRowAtIndexPath. Below is my cellForRowAtIndexPath method:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
NSManagedObject *info = [buildingArray objectAtIndex: [indexPath row]];
// all rooms have been scanned for building
if([self allRoomsScanned: [[info valueForKey:#"buildingid"] intValue]]) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
[cell.textLabel setTextColor: [UIColor lightGrayColor]];
}
else {
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
[cell.textLabel setTextColor: [UIColor blackColor]];
}
[cell.textLabel setFont:[UIFont fontWithName:#"Helvetica-Bold" size:16.0]];
[cell.textLabel setText:[info valueForKey:#"buildingname"]];
return cell;
}
And here is the allRoomsScanned method and allDevicesScanned method:
- (BOOL) allRoomsScanned: (int) buildingID {
NSMutableArray *scannedRoomArray = [[NSMutableArray alloc] init];
// Get all user_device
NSManagedObjectContext *context = [self managedObjectContext];
NSError *error;
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription
entityForName:#"user_device" inManagedObjectContext:context];
[fetchRequest setEntity:entity];
NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];
NSNumber *deviceid = [NSNumber numberWithInt: 0];
NSNumber *roomid = [NSNumber numberWithInt: 0];
//int lastRoomID = 0;
for (NSManagedObject *info in fetchedObjects) {
// Get all device
deviceid = [info valueForKey:#"deviceid"];
fetchRequest = [[NSFetchRequest alloc] init];
entity = [NSEntityDescription
entityForName:#"device" inManagedObjectContext:context];
[fetchRequest setEntity:entity];
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"(deviceid = %d)", [deviceid intValue]];
[fetchRequest setPredicate:predicate];
NSArray *fetchedDevices = [context executeFetchRequest:fetchRequest error:&error];
for (NSManagedObject *infod in fetchedDevices) {
// Get all room
roomid = [infod valueForKey:#"roomid"];
fetchRequest = [[NSFetchRequest alloc] init];
entity = [NSEntityDescription
entityForName:#"room" inManagedObjectContext:context];
[fetchRequest setEntity:entity];
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"(roomid = %d) AND (buildingid = %d)", [roomid intValue], buildingID];
[fetchRequest setPredicate:predicate];
NSMutableArray *fetchedRoom = [[context executeFetchRequest:fetchRequest error:&error] mutableCopy];
// add room to array if room belongs to selected building and room not already added
if([fetchedRoom count] > 0) { //&& lastRoomID != [roomid intValue]) {
for (NSManagedObject *info in fetchedRoom) {
NSLog(#"room id: %#", [info valueForKey:#"roomid"]);
// add room ids to array if not already there
if (![scannedRoomArray containsObject:[info valueForKey:#"roomid"]] && [self allDevicesScanned:[[info valueForKey:#"roomid"] intValue]])
[scannedRoomArray addObject: [info valueForKey:#"roomid"]];
}
//lastRoomID = [roomid intValue];
}
}
}
fetchRequest = [[NSFetchRequest alloc] init];
entity = [NSEntityDescription
entityForName:#"room" inManagedObjectContext:context];
[fetchRequest setEntity:entity];
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"(buildingid = %d)", buildingID];
[fetchRequest setPredicate:predicate];
NSArray *fetchedRoomTotal = [context executeFetchRequest:fetchRequest error:&error];
//NSLog(#"Total Rooms for Building: %d", [fetchedRoomTotal count]);
//NSLog(#"Scanned Rooms for Building: %d", [scannedRoomArray count]);
//NSLog(#"Scanned rooms: %#", scannedRoomArray);
if([fetchedRoomTotal count] == [scannedRoomArray count] && [fetchedRoomTotal count] > 0) {
return YES;
}
else {
return NO;
}
}
- (BOOL) allDevicesScanned: (int) roomID {
NSMutableArray *scannedDeviceArray = [[NSMutableArray alloc] init];
// Get all user_device
NSManagedObjectContext *context = [self managedObjectContext];
NSError *error;
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription
entityForName:#"user_device" inManagedObjectContext:context];
[fetchRequest setEntity:entity];
NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];
NSNumber *deviceid = [NSNumber numberWithInt: 0];
//NSNumber *roomid = [NSNumber numberWithInt: 0];
for (NSManagedObject *info in fetchedObjects) {
// Get all device
deviceid = [info valueForKey:#"deviceid"];
fetchRequest = [[NSFetchRequest alloc] init];
entity = [NSEntityDescription
entityForName:#"device" inManagedObjectContext:context];
[fetchRequest setEntity:entity];
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"(deviceid = %d) AND (roomid = %d)", [deviceid intValue], roomID];
[fetchRequest setPredicate:predicate];
NSArray *fetchedDevices = [context executeFetchRequest:fetchRequest error:&error];
for (NSManagedObject *infod in fetchedDevices) {
// add device to array
if([fetchedDevices count] > 0) {
NSLog(#"room id: %d", roomID);
// add device ids to array if not already there
if (![scannedDeviceArray containsObject:deviceid])
[scannedDeviceArray addObject: deviceid];
}
}
}
fetchRequest = [[NSFetchRequest alloc] init];
entity = [NSEntityDescription
entityForName:#"device" inManagedObjectContext:context];
[fetchRequest setEntity:entity];
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"(roomid = %d)", roomID];
[fetchRequest setPredicate:predicate];
NSArray *fetchedDeviceTotal = [context executeFetchRequest:fetchRequest error:&error];
//NSLog(#"Total Devices for Room: %d", [fetchedDeviceTotal count]);
//NSLog(#"Scanned Devices for Room: %d", [scannedDeviceArray count]);
//NSLog(#"Scanned Devices: %#", scannedDeviceArray);
if([fetchedDeviceTotal count] == [scannedDeviceArray count] && [fetchedDeviceTotal count] > 0) {
return YES;
}
else {
return NO;
}
}
Any idea on how to get rid of the latency when scrolling? I'm assuming I may be doing something inefficiently with either my core data calls or the way I'm calling the method in cellForRowAtIndexPath.
Thank for any help. It is greatly appreciated.
You really should not make Fetch Requests and processing like that on the main thread while scrolling a table view.
As rokjarc said, you should definitely save the result of your (quite heavy) allRoomsScanned method. I'd suggest adding a new style, i.e. with an activity indicator, that the cell gets when you don't have a result for that input yet. As soon as the load is complete you refresh the table view cell.
Attention: You can't use your default NSManagedObjectContext in allRoomsScanned and allDevicesScanned.
You need to initialize a new context in the background thread. Either initialize a new context at the beginning of the block and pass it as a method parameter or create a new one right in the methods.
NSManagedObject *info = [buildingArray objectAtIndex: [indexPath row]];
NSNumber *cachedResult = [self.scanResults objectForKey:info.objectID];
if (cachedResult == nil) {
// style loading state
int scanInfo = [[info valueForKey:#"buildingid"] intValue];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
BOOL result = [self allRoomsScanned: scanInfo];
[self.scanResults setObject:[NSNumber numberWithBool:result] forKey:info.objectID];
[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]
});
} else if (cachedResult.boolValue == YES) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
[cell.textLabel setTextColor: [UIColor lightGrayColor]];
} else if (cachedResult.boolValue == NO) {
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
[cell.textLabel setTextColor: [UIColor blackColor]];
}
[cell.textLabel setFont:[UIFont fontWithName:#"Helvetica-Bold" size:16.0]];
[cell.textLabel setText:[info valueForKey:#"buildingname"]];
return cell;