App Link: https://drive.google.com/open?id=0BwU5Y4Xqfa42cXFNQTFtYW9BY2c
AppDelegate.h
AppDelegate.h
#import <UIKit/UIKit.h>
#import <CoreData/CoreData.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;
@property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel;
@property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator;
- (void)saveContext;
- (NSURL *)applicationDocumentsDirectory;
@end
AppDelegate.m
#import "AppDelegate.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
- (void)applicationWillTerminate:(UIApplication *)application {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
// Saves changes in the application's managed object context before the application terminates.
[self saveContext];
}
#pragma mark - Core Data stack
@synthesize managedObjectContext = _managedObjectContext;
@synthesize managedObjectModel = _managedObjectModel;
@synthesize persistentStoreCoordinator = _persistentStoreCoordinator;
- (NSURL *)applicationDocumentsDirectory {
// The directory the application uses to store the Core Data store file. This code uses a directory named "com.brninfotech.CoreDataImgStorage" in the application's documents directory.
return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
}
- (NSManagedObjectModel *)managedObjectModel {
// The managed object model for the application. It is a fatal error for the application not to be able to find and load its model.
if (_managedObjectModel != nil) {
return _managedObjectModel;
}
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"CoreDataImgStorage" withExtension:@"momd"];
_managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
return _managedObjectModel;
}
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
// The persistent store coordinator for the application. This implementation creates and returns a coordinator, having added the store for the application to it.
if (_persistentStoreCoordinator != nil) {
return _persistentStoreCoordinator;
}
// Create the coordinator and store
_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"CoreDataImgStorage.sqlite"];
NSError *error = nil;
NSString *failureReason = @"There was an error creating or loading the application's saved data.";
if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) {
// Report any error we got.
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
dict[NSLocalizedDescriptionKey] = @"Failed to initialize the application's saved data";
dict[NSLocalizedFailureReasonErrorKey] = failureReason;
dict[NSUnderlyingErrorKey] = error;
error = [NSError errorWithDomain:@"YOUR_ERROR_DOMAIN" code:9999 userInfo:dict];
// Replace this with code to handle the error appropriately.
// abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
return _persistentStoreCoordinator;
}
- (NSManagedObjectContext *)managedObjectContext {
// Returns the managed object context for the application (which is already bound to the persistent store coordinator for the application.)
if (_managedObjectContext != nil) {
return _managedObjectContext;
}
NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (!coordinator) {
return nil;
}
_managedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
[_managedObjectContext setPersistentStoreCoordinator:coordinator];
return _managedObjectContext;
}
#pragma mark - Core Data Saving support
- (void)saveContext {
NSManagedObjectContext *managedObjectContext = self.managedObjectContext;
if (managedObjectContext != nil) {
NSError *error = nil;
if ([managedObjectContext hasChanges] && ![managedObjectContext save:&error]) {
// Replace this implementation with code to handle the error appropriately.
// abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
}
}
@end
VC.h
#import <UIKit/UIKit.h>
#import "AppDelegate.h"
@interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>
@property NSEntityDescription *myDiscTable;
@property NSMutableURLRequest *urlReqest;
@property NSURLSession *session;
@property NSURLSessionDataTask *dataTask;
@property NSMutableDictionary *dataDic;
@property NSMutableArray *names;
@property NSMutableArray *imagesPath;
@property (weak, nonatomic) IBOutlet UITableView *dataTableView;
@end
VC.m
#import "ViewController.h"
@interface ViewController (){
AppDelegate *ad;
}
@end
NSArray *results;
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
ad=[[UIApplication sharedApplication]delegate];
_names=[[NSMutableArray alloc]init];
_imagesPath=[[NSMutableArray alloc]init];
_dataTableView.delegate=self;
_dataTableView.dataSource=self;
_myDiscTable=[NSEntityDescription entityForName:@"ObjectStore" inManagedObjectContext:ad.managedObjectContext];
[self getDataFromServer];
// Do any additional setup after loading the view, typically from a nib.
}
-(void)getDataFromServer{
_urlReqest=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://itunes.apple.com/in/rss/newfreeapplications/limit=100/json"]];
_session=[NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
_dataTask=[_session dataTaskWithRequest:_urlReqest completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
_dataDic=[NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSLog(@"data dictionary is %@",_dataDic);
for (int i=0; i<100; i++) {
[_names addObject:[[[[[_dataDic objectForKey:@"feed"] objectForKey:@"entry"] objectAtIndex:i] objectForKey:@"im:name"] objectForKey:@"label"]];
[_imagesPath addObject:[[[[[[_dataDic objectForKey:@"feed"] objectForKey:@"entry"] objectAtIndex:i] objectForKey:@"im:image"] objectAtIndex:2] objectForKey:@"label"]];
}
NSLog(@" data array %@ images %@",_names,_imagesPath);
[self savingCoreData];
}];
[_dataTask resume];
// [self savingCoreData];
}
-(void)savingCoreData{
NSError *err;
for (int i=0; i<100; i++) {
NSManagedObject *saveManage=[[NSManagedObject alloc]initWithEntity:_myDiscTable insertIntoManagedObjectContext:ad.managedObjectContext];
[saveManage setValue:[_names objectAtIndex:i] forKey:@"name"];
NSLog(@"%@",[_names objectAtIndex:i]);
[saveManage setValue:[_imagesPath objectAtIndex:i] forKey:@"imagePath"];
NSLog(@"%@",[_imagesPath objectAtIndex:i]);
}
[ad.managedObjectContext save:&err];
if (err) {
NSLog(@"Data not Saved");
}
else {
NSLog(@"Data saved");
}
[self fetchingRecords];
}
-(void)fetchingRecords{
NSFetchRequest *request=[NSFetchRequest fetchRequestWithEntityName:@"ObjectStore"];
NSError *err;
results=[ad.managedObjectContext executeFetchRequest:request error:&err];
NSLog(@"array count is %lu",results.count);
for (int i=0; i<results.count; i++) {
NSManagedObject *MO=[results objectAtIndex:i];
NSLog(@"The name is %@ \n and the imagePath is %@",[MO valueForKey:@"name"],[MO valueForKey:@"imagePath"]);
}
[_dataTableView reloadData];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return results.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *str=@"reuseID";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:str];
if (cell==nil) {
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:str];
}
NSManagedObject *MOMD=[results objectAtIndex:indexPath.row];
cell.textLabel.text=[MOMD valueForKey:@"name"];
cell.detailTextLabel.text=[MOMD valueForKey:@"imagePath"];
NSData *imgData=[NSData dataWithContentsOfURL:[NSURL URLWithString:[MOMD valueForKey:@"imagePath"]]];
cell.imageView.image=[UIImage imageWithData:imgData];
//cell.textLabel.text=[_names objectAtIndex:indexPath.row];
// cell.imageView.image=[UIImage imageWithData:imgData];
return cell;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
Sign up here with your email
8 comments
Write commentshttps://drive.google.com/open?id=0BwU5Y4Xqfa42ZFRuN3p1QTVKTm1xeUprcWZHZUVOTVRFbUhn
Replykrak task
Replyhttps://drive.google.com/open?id=0BwU5Y4Xqfa42enlyX3NjUFRlOHc
Replyshare to fb
Search in MapView https://drive.google.com/open?id=0BwU5Y4Xqfa42V3dVbEUxSXljYm8
Replyhttps://drive.google.com/open?id=0B9iYp_BYG46LUThiMG9HckhDXzFxTzRFSjk2Qk96OG8zeDh3
Replyxml: https://drive.google.com/open?id=0BwU5Y4Xqfa42NEpjaEstV29CUms
Replyhttps://drive.google.com/open?id=0BwU5Y4Xqfa42NDVYM1JxazdlM1U
Replyhttps://drive.google.com/open?id=0BwU5Y4Xqfa42TFdIR1lwRG5jdk0
ReplyConversionConversion EmoticonEmoticon