2014-06-24 32 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'的元数据中。这样可以将数据保存在一起,而不是一个格式的文件和另一个格式的文件。 –