2014-06-24 67 views
0

在此先感謝您的幫助,並瞭解我是否使用objective-c語言編寫了奇怪的東西。 我從Web服務獲取JSON並嘗試將所有內容存儲在覈心數據中,以便在網絡狀態關閉的情況下我可以運行應用程序白色保存的數據。從Web服務器檢查Api版本以更新核心數據

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
    self.managedObjectContext = [self managedObjectContextWithName:@"CoreData"]; 


    NSMutableArray * competition = [self.jsonCompetition objectForKey:@"Competition"]; 
    NSMutableDictionary * competizione = [[NSMutableDictionary alloc] init]; 

    for (int i = 0; i< competition.count; i++) { 
     NSManagedObject * competion = [NSEntityDescription 
             insertNewObjectForEntityForName:@"Competition" 
             inManagedObjectContext:self.managedObjectContext]; 

     competizione = [competition objectAtIndex:i]; 
     [competion setValue:[competizione objectForKey:@"id"] forKeyPath:@"id"]; 
     [competion setValue:[competizione objectForKey:@"name"] forKeyPath:@"name"]; 
     [competion setValue:[competizione objectForKey:@"region"] forKeyPath:@"region"]; 
     NSError *error; 

     if (![self.managedObjectContext save:&error]) { 

      NSLog(@"Errore: %@", [error localizedDescription]); 
     } 


    } 

    NSError *errore; 

    if (!errore) { 
     // NSLog(@"%@",_jsonDict); 

    } else { 

     NSLog(@"ERROR!"); 
    } 
    [self.managedObjectContext save:&errore]; 

    Reachability *reachability = [Reachability reachabilityForInternetConnection]; 
    NetworkStatus networkStatus = [reachability currentReachabilityStatus]; 
    if (networkStatus == ReachableViaWWAN) { 



    } else if (networkStatus == ReachableViaWiFi) { 

     NSData *data = [self callWS]; 
     NSError *errore; 

     self.jsonCompetition = [NSJSONSerialization JSONObjectWithData:data                options:kNilOptions 
                    error:&errore]; 

     self.managedObjectContext = [self managedObjectContextWithName:@"CoreData"]; 


     NSMutableArray * competition = [self.jsonCompetition objectForKey:@"Competition"]; 
     NSMutableDictionary * competizione = [[NSMutableDictionary alloc] init]; 

     for (int i = 0; i< competition.count; i++) { 
      NSManagedObject * competion = [NSEntityDescription 
              insertNewObjectForEntityForName:@"Competition" 
              inManagedObjectContext:self.managedObjectContext]; 

      competizione = [competition objectAtIndex:i]; 
      [competion setValue:[competizione objectForKey:@"id"] forKeyPath:@"id"]; 
      [competion setValue:[competizione objectForKey:@"name"] forKeyPath:@"name"]; 
      [competion setValue:[competizione objectForKey:@"region"] forKeyPath:@"region"]; 


      if (![self.managedObjectContext save:&errore]) { 

       NSLog(@"Errore: %@", [errore localizedDescription]); 
      } 


     } 

     [self.managedObjectContext save:&errore]; 
     if (!error) { 
      // NSLog(@"%@",_jsonDict); 
     } else { 
      NSLog(@"ERROR!"); 
     } 

     NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; 

     NSEntityDescription *entityCompetizione = [NSEntityDescription 
             entityForName:@"Competition" inManagedObjectContext:self.managedObjectContext]; 

     [fetchRequest setEntity:entityCompetizione]; 
     NSArray *arrayCompetizioni = [self.managedObjectContext executeFetchRequest:fetchRequest error:&errore]; 


    } else { 



     NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; 

     NSEntityDescription *entityCompetizione = [NSEntityDescription 
                entityForName:@"Competition" inManagedObjectContext:self.managedObjectContext]; 

     [fetchRequest setEntity:entityCompetizione]; 

     NSArray *arrayCompetizioni = [self.managedObjectContext executeFetchRequest:fetchRequest error:&errore]; 



    } 

    FirstViewController * fVC = [[FirstViewController alloc]initWithNibName:@"FirstViewController" bundle:nil]; 
    self.window.rootViewController = fVC; 
    [self.window makeKeyAndVisible]; 
    return YES; 

在JSON我有鑰匙絲毫ApiVersion,有一種方法來檢查,如果需要的核心數據進行更新絲毫JSON的新版本(在網絡狀態情況下是)? 感謝

回答

0

你以前ApiVersion保存到NSUserDefaults

NSNumber *currentApiVersion = ...; //You get the version here from JSON 

[[NSUserDefaults standardUserDefaults] setObject:currentApiVersion forKey:@"ApiVersion"]; 

新數據到達時,你所保存的值進行比較:

NSNumber *currentApiVersion = ...; //You get the version here from the JSON 

NSNumber *previousApiVersion = [[NSUserDefaults standardUserDefaults] objectForKey:@"ApiVersion"]; 

if (![currentApiVersion isEqualToNumber:previousApiVersion]) { 
    //Update the stored version 
    [[NSUserDefaults standardUserDefaults] setObject:currentApiVersion forKey:@"ApiVersion"]; 

    //The Api Version is different. 
} 
+0

碼再次,它的工作:)謝謝! – Alex21

+0

如果您滿意,請接受答案,以便其他人知道您的問題已解決。 –

+3

我不會推薦使用'NSUserDefaults'。您正在混合持久層。這種類型的信息應該存儲在存儲服務器數據的'NSPersistentStore'的元數據中。這樣可以將數據保存在一起,而不是一個格式的文件和另一個格式的文件。 –