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];
Related
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!
I have to entity, A and B (A <<---> B) so an A object can have one B object and a B object can have more than one A object (Correct me if I'm wrong, I'm new to core data relationships).
Now, I want to know how many A objects are in a B object. For example I have a shop (employee <<---> shop) and I want to know how many employee work in that shop.
I've create something like this without success:
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:#"Task" inManagedObjectContext:self.managedObjectContext];
[request setEntity:entity];
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"project == %#", aProject];
[request setPredicate:predicate];
NSError *error = nil;
NSInteger resultNumber = [self.managedObjectContext countForFetchRequest:request error:&error];
if (!resultNumber) {
NSLog(#"Risultati della richiesta nulli!");
abort();
}
NSLog(#"getCountOfProjects called");
NSLog(#"Results: %#", resultNumber);
return resultNumber;
Can you help me? Than you so much! ;)
Update
I've tried to use this code but it doesn't work...
-(void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
Project *projects = (Project *)[fetchedResultsController objectAtIndexPath:indexPath];
cell.textLabel.text = projects.title;
cell.detailTextLabel.text = [NSString stringWithFormat:#"%# entries", [projects.task count]];
}
Why not just say something like [[myShop employees] count]?
I have a NSFetchRequest to search of core data entities. It finds them fine, but I want to set the cell's text to the entity's name attribute.
I currently have this but I am setting the object itself to the title, instead of the name attribute, how can I fix this?
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:#"Routine" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
NSError *error = nil;
NSArray *results = [managedObjectContext executeFetchRequest:fetchRequest error:&error];
self.routineArray = results;
[fetchRequest release];
cell.textLabel.text = [self.routineArray objectAtIndex:indexPath.row];
Try :
cell.textLabel.text = [[self.routineArray objectAtIndex:indexPath.row] name];
EDIT
If your Routine instances are in routineArray (given that name, I suppose) :
// get the Routine instance
Routine *r = [self.routineArray objectAtIndex:indexPath.row];
// now you got your instance, set anything you want on r...
// ...
// and then set its name as the text for textLabel using previous instruction
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.
I have the following code that fetches a Session object for a particular Exercise object.
This fetch loads data into my UITableView.
The count is fine, I just need a way to extract the Session.timeStamp property so I can set it to UITableViewCell's textLabel property.
Does anyone know how?
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setPredicate:[NSPredicate predicateWithFormat:#"name == %#", exercise.name]];
NSEntityDescription *sessionEntity = [NSEntityDescription entityForName:#"Exercise" inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:sessionEntity];
NSError *error = nil;
NSArray *results = [managedObjectContext executeFetchRequest:fetchRequest error:&error];
NSMutableArray *mutableSessionArray = [NSMutableArray array];
for (Exercise *ex in results) {
Session *session = [ex exercises];
[mutableSessionArray addObject:session];
}
self.sessionArray = [NSArray arrayWithArray:mutableSessionArray];
Call timestamp property of your mananged object subclass or call valueForKey.