Core Data Xcode 8.0 & later, ios 10.0 & later version ...
@property AppDelegate *appDelg;
@property NSEntityDescription *enterTextED;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
_appDelg = (AppDelegate *)[[UIApplication sharedApplication]delegate];
_enterTextED = [NSEntityDescription entityForName:@"Data" inManagedObjectContext:_appDelg.persistentContainer.viewContext];
}
-(void) SaveData {
_appDelg = (AppDelegate *)[[UIApplication sharedApplication]delegate];
_enterTextED = [NSEntityDescription entityForName:@"Data" inManagedObjectContext:_appDelg.persistentContainer.viewContext];
NSManagedObject *managedObject = [[NSManagedObject alloc]initWithEntity:_enterTextED insertIntoManagedObjectContext:_appDelg.persistentContainer.viewContext];
[managedObject setValue:_enterTextTF.text forKey:@"enterText"];
NSError *errorObj;
[_appDelg.persistentContainer.viewContext save:&errorObj];
}
// To Fetch Data
-(void) FetchData {
_appDelg = (AppDelegate *)[[UIApplication sharedApplication]delegate];
NSFetchRequest * fetchReq = [NSFetchRequest fetchRequestWithEntityName:@"Data"];
NSError * fetchErrorObj;
storedData = [_appDelg.persistentContainer.viewContext executeFetchRequest:fetchReq error:&fetchErrorObj];
NSLog(@"array count is %lu",storedData.count);
}
No comments:
Post a Comment