#import <UIKit/UIKit.h>
#import "AppDelegate.h"
@interface ViewController : UIViewController
@property (weak, nonatomic) IBOutlet UITextField *actorNameTF;
@property (weak, nonatomic) IBOutlet UITextField *industryTF;
- (IBAction)submitBtn:(id)sender;
@property NSManagedObjectContext *MOC;
@property NSManagedObjectContext *MOC1;
@property AppDelegate *appDel;
@property NSEntityDescription *actorEntityRef;
- (IBAction)fetchDetails:(id)sender;
- (IBAction)deleteBtn:(id)sender;
@property NSArray *actorArray;
- (IBAction)update:(id)sender;
@property NSFetchRequest *fetchReq;
@property NSManagedObject *managedObject;
@property NSManagedObject *managedObject1;
@property NSURLSession *urlSession;
@property NSMutableURLRequest *urlReq;
@property NSEntityDescription *premDetailsRef;
@property NSURLSessionDataTask *dataTask;
@property NSMutableDictionary *responseDict;
@property NSMutableArray *arr;
@property NSMutableArray *arr1;
@end
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.appDel=(AppDelegate *)[[UIApplication sharedApplication]delegate];
self.MOC=self.appDel.persistentContainer.viewContext;
self.MOC1=self.appDel.persistentContainer.viewContext;
self.actorEntityRef=[NSEntityDescription entityForName:@"Actors" inManagedObjectContext:self.MOC];
self.premDetailsRef=[NSEntityDescription entityForName:@"Prem" inManagedObjectContext:self.MOC1];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)submitBtn:(id)sender
{
self.urlSession=[NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
self.urlReq=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://rss.itunes.apple.com/api/v1/in/ios-apps/top-free/all/10/explicit.json"]];
self.dataTask=[self.urlSession dataTaskWithRequest:self.urlReq completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error)
{
dispatch_async(dispatch_get_main_queue(), ^{
self.responseDict=[NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSLog(@"%@",self.responseDict);
self.managedObject1=[[NSManagedObject alloc]initWithEntity:self.premDetailsRef insertIntoManagedObjectContext:self.MOC1];
self.arr =[[NSMutableArray alloc]init];
// for (int i=0; i<10; i++)
//{
[self.arr addObject:[[[[self.responseDict objectForKey:@"feed"]objectForKey:@"results"]objectAtIndex:0]objectForKey:@"name"]];
[self.managedObject1 setValue:[self.arr objectAtIndex:0] forKey:@"name"];
[self.appDel saveContext];
//}
});
}];
[self.dataTask resume];
// self.managedObject=[[NSManagedObject alloc]initWithEntity:self.actorEntityRef insertIntoManagedObjectContext:self.MOC];
//
// [self.managedObject setValue:self.actorNameTF.text forKey:@"name"];
// [self.managedObject setValue:self.industryTF.text forKey:@"industry"];
//
// [self.appDel saveContext];
}
- (IBAction)fetchDetails:(id)sender
{
NSArray *myarr=[[NSArray alloc]init];
self.fetchReq=[NSFetchRequest fetchRequestWithEntityName:@"Prem"];
NSError *err;
NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"Prem" inManagedObjectContext:self.MOC];
[self.fetchReq setEntity:entityDescription];
myarr=self.arr;
NSLog(@"%@",myarr);
myarr=[self.MOC executeFetchRequest:self.fetchReq error:&err];
if (err) {
NSLog(@"somthing shhh!!!");
}
else
{
NSLog(@"sucessfully fetched");
for (int i =0; i<myarr.count; i++)
{
NSManagedObject *premObj=[myarr objectAtIndex:i];
NSLog(@"app name is %@ ",[premObj valueForKey:@"name"]);
}
}
// self.fetchReq=[NSFetchRequest fetchRequestWithEntityName:@"Actors"];
// NSError *err;
// self.actorArray=[self.MOC executeFetchRequest:_fetchReq error:&err];
// if (err)
// {
// NSLog(@"something shhhhhh!!!");
// }else
//
// {
// NSLog(@"sucessfilly fetched details");
//
// for (int i=0; i<_actorArray.count; i++)
// {
// NSManagedObject *actorObj=[_actorArray objectAtIndex:i];
// NSLog(@"actor name %@ and industry is %@",[actorObj valueForKey:@"name"],[actorObj valueForKey:@"industry"]);
//
//
// }
//
//
// }
}
- (IBAction)deleteBtn:(id)sender
{
self.fetchReq = [[NSFetchRequest alloc] init];
self.actorEntityRef = [NSEntityDescription entityForName:@"Actors" inManagedObjectContext:self.MOC];
[self.fetchReq setEntity:self.actorEntityRef];
NSError *error;
self.actorArray = [self.MOC executeFetchRequest:self.fetchReq error:&error];
[self.MOC deleteObject:[self.actorArray objectAtIndex:0]];
//for (NSManagedObject *managedObject in items) {
NSLog(@"%@ object deleted",self.actorArray);
//}
if (![self.MOC save:&error]) {
NSLog(@"Error deleting %@ - error:%@",self.actorEntityRef,error);
}
}
- (IBAction)update:(id)sender
{
//NSFetchRequest *request = [[NSFetchRequest alloc] init];
self.actorEntityRef=[NSEntityDescription entityForName:@"Actors" inManagedObjectContext:self.MOC];
[self.fetchReq setEntity:self.actorEntityRef];
NSError *error = nil;
NSArray *results = [self.MOC executeFetchRequest:self.fetchReq error:&error];
self.managedObject = [results objectAtIndex:0];
[self.managedObject setValue:@"prem" forKey:@"name"];
}
@end
Sign up here with your email
ConversionConversion EmoticonEmoticon