Advertisement
wkerswell

first

Oct 23rd, 2012
339
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //
  2. //  FirstViewController.m
  3. //  CPOP Test 4
  4. //
  5. //  Created by Will Kerswell on 22/10/2012.
  6. //  Copyright (c) 2012 Will Kerswell. All rights reserved.
  7. //
  8.  
  9. #import "FirstViewController.h"
  10.  
  11. @interface FirstViewController ()
  12.  
  13. @end
  14.  
  15. @implementation FirstViewController
  16. @synthesize firstLoader,downloadingLabel;
  17.  
  18.  
  19. /////////////////////////////
  20. #pragma mark - View lifecycle
  21. /////////////////////////////
  22.  
  23. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  24. {
  25.     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  26.     if (self) {
  27.         // Custom initialization
  28.     }
  29.     return self;
  30. }
  31.  
  32. - (void)viewDidLoad
  33. {
  34.     //start the loader animation
  35.     [[self firstLoader]startAnimating];
  36.    
  37.  
  38.    
  39.    
  40.     //feilds for the poi db
  41.     NSArray *POIdbFields = [[NSArray alloc]initWithObjects:
  42.                             @"id",
  43.                             @"name",
  44.                             @"catID",
  45.                             @"description",
  46.                             @"lat" ,
  47.                             @"long" ,
  48.                             @"compLink" ,
  49.                             @"www" ,
  50.                             @"email" ,
  51.                             @"phone",
  52.                             @"status",
  53.                             @"retailer" ,
  54.                             @"sponsor",
  55.                             @"sponsorLevel" ,
  56.                             @"images",
  57.                             @"baner", nil];
  58.    
  59.     //feilds for cat db
  60.     NSArray *CatdbFields = [[NSArray alloc]initWithObjects:
  61.                             @"catID",
  62.                             @"name",
  63.                             @"pinImage",nil];
  64.    
  65.    
  66.     //create a new queue
  67.     queue = dispatch_queue_create("com.hazardousfrog.CPOPDataGet", NULL);
  68.    
  69.     //do all the database stuff in the back ground
  70.     dispatch_async(queue, ^{
  71.    
  72.         //create database
  73.         [self createDB];
  74.        
  75.         NSLog(@"JSON Start");
  76.        
  77.         //create an instance of that json objetc
  78.         GetJSON * JSON = [[GetJSON alloc] init];
  79.         //call the get json function and set it to an array
  80.         NSArray *JSONPOIArray = [[NSArray alloc]init];
  81.         NSArray *JSONCatArray = [[NSArray alloc]init];
  82.        
  83.         JSONPOIArray = [JSON getJSON:@"http://willkerswell.com/stuff/tests/testSponsor.php"];
  84.         JSONCatArray = [JSON getJSON:@"http://willkerswell.com/stuff/tests/testCat.php"];
  85.        
  86.         NSLog(@"JSON Finish");
  87.        
  88.         //create an instance of the database oject
  89.         DataBase * dataBase = [[DataBase alloc] init];
  90.        
  91.         //open the database connection
  92.         [dataBase openDB];
  93.        
  94.         //store the data
  95.         NSLog(@"Database Store Start");
  96.         NSLog(@"--Database Store POI");
  97.         [dataBase store:JSONPOIArray withFields:POIdbFields TableName:@"POI"];
  98.         NSLog(@"--Database Store Cats");
  99.         [dataBase store:JSONCatArray withFields:CatdbFields TableName:@"categories"];
  100.         NSLog(@"Database Store Finish");
  101.        
  102.         NSLog(@"Database Get Start");
  103.         [dataBase getCatNames];
  104.         [dataBase getSponsors];
  105.         NSLog(@"Database Get Finish");
  106.        
  107.         sleep(2);
  108.        
  109.         dispatch_async(dispatch_get_main_queue(), ^{
  110.         //stop the loader once the database stuff has finished and get rid of the text
  111.         [[self firstLoader]stopAnimating];
  112.         self.downloadingLabel.text = @"";
  113.        
  114.  
  115.         });
  116.    
  117.     });
  118.    
  119.     dispatch_async(dispatch_get_main_queue(), ^{
  120.         //once all the loading is done change view
  121.         [self performSegueWithIdentifier:@"first" sender:self];
  122.     });
  123.    
  124.     //can the view on the main queue. UIView related stuff is not fully thread safe
  125.     //dispatch_sync(dispatch_get_main_queue(), ^{ [self performSegueWithIdentifier:@"first" sender:self];});
  126.    
  127.    
  128.     [super viewDidLoad];
  129.     // Do any additional setup after loading the view.
  130. }
  131.  
  132. - (void)didReceiveMemoryWarning
  133. {
  134.     [super didReceiveMemoryWarning];
  135.     // Dispose of any resources that can be recreated.
  136. }
  137.  
  138. //-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
  139. //    if ([[segue identifier] isEqualToString:@"first"]) {
  140. //        
  141. //        // Get destination view
  142. //        TabBarViewController *vc = [segue destinationViewController];
  143. //        NSMutableArray *array = [[NSMutableArray alloc]init ];
  144. //        [array addObject:@"53.057801 "];
  145. //        [array addObject:@"-2.68779504298 "];
  146. //        [array addObject:@"0.011323 "];
  147. //        [array addObject:@"0.027444 "];
  148. //    
  149. //        TabBarViewController.coord = array;
  150. //    }
  151. //}
  152.  
  153. /////////////////////////////
  154. #pragma mark - Database
  155. /////////////////////////////
  156.  
  157. -(void)createDB{
  158.     //make and array containing all the field types
  159.     NSArray *dbColumnsPOIs =[[NSArray alloc]initWithObjects:@"'id' TEXT PRIMARY KEY",
  160.                              @"'name' TEXT",
  161.                              @"'catID' TEXT",
  162.                              @"'description' TEXT",
  163.                              @"'lat' TEXT",
  164.                              @"'long' TEXT",
  165.                              @"'compLink' TEXT",
  166.                              @"'www' TEXT",
  167.                              @"'email' TEXT",
  168.                              @"'phone' TEXT",
  169.                              @"'status' TEXT",
  170.                              @"'retailer' TEXT",
  171.                              @"'sponsor'TEXT",
  172.                              @"'sponsorLevel' TEXT",
  173.                              @"'images'TEXT",
  174.                              @"'baner'TEXT", nil];
  175.    
  176.     //make and array containing all the field types
  177.     NSArray *dbColumnsCats =[[NSArray alloc]initWithObjects:
  178.                              
  179.                              @"'catID' TEXT PRIMARY KEY",
  180.                              @"'name' TEXT",
  181.                              @"'pinImage' TEXT",nil];
  182.    
  183.     //create an instance of the database oject
  184.     DataBase * dataBase = [[DataBase alloc] init];
  185.     //open the database connect
  186.     [dataBase openDB];
  187.     //create the table.
  188.     [dataBase createTable:@"POI" withFields:dbColumnsPOIs];
  189.     [dataBase createTable:@"categories" withFields:dbColumnsCats];
  190.  
  191. }
  192.  
  193. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement