Objective c 从web服务器检查Api版本以更新核心数据
提前感谢您的帮助,如果我用objective-c语言写一些奇怪的东西,请谅解。 我从web服务获取json,并尝试存储所有核心数据,以便在网络状态关闭的情况下,我可以在保存数据的情况下运行应用程序Objective c 从web服务器检查Api版本以更新核心数据,objective-c,json,core-data,Objective C,Json,Core Data,提前感谢您的帮助,如果我用objective-c语言写一些奇怪的东西,请谅解。 我从web服务获取json,并尝试存储所有核心数据,以便在网络状态关闭的情况下,我可以在保存数据的情况下运行应用程序 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] init
- (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;
-(BOOL)应用程序:(UIApplication*)应用程序使用选项完成启动:(NSDictionary*)启动选项
{
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
在json中,我有一个带ApiVersion的密钥,有一种方法可以检查核心数据是否需要在新版本的json中更新(如果网络状态为on)??
谢谢您将以前的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.
}
代码完成了它的工作:)再次感谢!如果您满意,请接受答案,以便其他人知道您的问题已经解决。我不建议使用
NSUserDefaults
。您正在混合持久层。此类信息应存储在存储服务器数据的NSPersistentStore
的元数据中。This将您的数据保存在一起,而不是将一个数据以一种格式保存,另一个数据以另一种格式保存。我同意应该将它们存储在一起,但您需要一个全新的实体来实现这一点。他指的是