Core Data

#import 

@interface ViewController : UIViewController

@property (strong, nonatomic) IBOutlet UITextField *nameTF;
@property (strong, nonatomic) IBOutlet UITextField *lastNameTF;
@property (strong, nonatomic) IBOutlet UITextField *studentIDTF;
- (IBAction)addToDatabaseClicked:(id)sender;
@property (strong, nonatomic) IBOutlet UITextView *outputTV;
- (IBAction)searchClicked:(id)sender;
- (IBAction)deleteClicked:(id)sender;

- (IBAction)updateClicked:(id)sender;
@end

#import "ViewController.h"
#import "Students+Add.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
- (IBAction)addToDatabaseClicked:(id)sender {
NSDictionary *studentInfo= @{@"name":_nameTF.text, @"lastName":_lastNameTF.text, @"studentId":_studentIDTF.text};
_outputTV.text= [[Students addStudentInfoFromDictionary:studentInfo]description];
}
- (IBAction)searchClicked:(id)sender {
_outputTV.text=[[Students searchStudentByName:self.nameTF.text]description];
}
- (IBAction)deleteClicked:(id)sender {
_outputTV.text=[[Students deleteStudentByName:self.nameTF.text]description];
}
- (IBAction)updateClicked:(id)sender {
_outputTV.text=[[Students updateStudentByName:self.studentIDTF.text]description];
}
@end

#import "Students.h"

@interface Students (Add)

+(Students *)addStudentInfoFromDictionary:(NSDictionary *)studentInfo;
+(NSArray *)searchStudentByName:(NSString *)name;
+(NSArray *)deleteStudentByName:(NSString *)name;
+(NSArray *)updateStudentByName:(NSString *)studentId;
@end

#import "Students+Add.h"
#import "AppDelegate.h"
@implementation Students (Add)

+(Students *)addStudentInfoFromDictionary:(NSDictionary *)studentInfo{
AppDelegate *appDelegate= (AppDelegate *)[[UIApplication sharedApplication]delegate];
NSManagedObjectContext *context=[appDelegate managedObjectContext];
Students *studentEntity = nil;

//do not add same id
//start
NSString *studentId=studentInfo[@"studentId"];
NSFetchRequest *request=[NSFetchRequest fetchRequestWithEntityName:@"Students"];
request.predicate=[NSPredicate predicateWithFormat:@"studentId = %@",studentId];
NSError *error;
NSArray *matches=[context executeFetchRequest:request error:&error];
if (!matches || error || ([matches count]>1))
{
//Handle errors
NSLog(@"error found");
}
else if ([matches count])
{
//Returns the existing object
studentEntity=[matches firstObject];

NSLog(@"exist user");
}
else{

//create a new object
studentEntity=[NSEntityDescription insertNewObjectForEntityForName:@"Students" inManagedObjectContext:context];

studentEntity.studentId=[studentInfo valueForKey:@"studentId"];
studentEntity.name=[studentInfo valueForKey:@"name"];
studentEntity.lastName=[studentInfo valueForKey:@"lastName"];
}

return studentEntity;

}

+(NSArray *)searchStudentByName:(NSString *)name
{
AppDelegate *appDelegate= (AppDelegate *)[[UIApplication sharedApplication]delegate];
NSManagedObjectContext *context=[appDelegate managedObjectContext];

NSError *error;
NSFetchRequest *request=[NSFetchRequest fetchRequestWithEntityName:@"Students"];
request.predicate=[NSPredicate predicateWithFormat:@"name == %@",name];

NSArray *fetchedObject = [context executeFetchRequest:request error:&error];

NSMutableArray *results=[[NSMutableArray alloc]init];
for (Students *studentEntity in fetchedObject) {
[results addObject:[self createObjectFromEntity:studentEntity]];
}

return results;
}


+(NSDictionary *)createObjectFromEntity:(Students *)studentInfo
{
NSMutableDictionary *tempDictionary=[[NSMutableDictionary alloc]init];
tempDictionary[@"name"] = studentInfo.name;
tempDictionary[@"lastName"]=studentInfo.lastName;
tempDictionary[@"studentId"]=studentInfo.studentId;
return tempDictionary;
}

+(NSArray *)deleteStudentByName:(NSString *)name
{
AppDelegate *appDelegate= (AppDelegate *)[[UIApplication sharedApplication]delegate];
NSManagedObjectContext *context=[appDelegate managedObjectContext];

NSError *error;
NSFetchRequest *request=[NSFetchRequest fetchRequestWithEntityName:@"Students"];
request.predicate=[NSPredicate predicateWithFormat:@"name == %@",name];

// [request setEntity:entity];
// [request setPredicate:predicate];


NSArray *items = [context executeFetchRequest:request error:&error];

for (NSManagedObject *managedObject in items)
{
[context deleteObject:managedObject];
}

return items;
}

+(NSArray *)updateStudentByName:(NSString *)studentId
{
AppDelegate *appDelegate= (AppDelegate *)[[UIApplication sharedApplication]delegate];
NSManagedObjectContext *context=[appDelegate managedObjectContext];

NSError *error;
NSFetchRequest *request=[NSFetchRequest fetchRequestWithEntityName:@"Students"];
request.predicate=[NSPredicate predicateWithFormat:@"studentId == %@",studentId];


NSArray *arrResult = [context executeFetchRequest:request error:&error];

Students *entity = arrResult[0];
entity.studentId = @"2";
[appDelegate saveContext];

return arrResult;
}
@end
Previous
Next Post »