我正在使用核心数据来存储我的应用程序的一些信息。 我有一个包含8个实体的.xcdatamodeld文件,我将它们提取到不同的视图中。没有从核心数据获取数据
在其中一个viewControllers中,我打电话给他们三个。就像这样:
AppDelegate *appDelegate = (AppDelegate *) [[UIApplication sharedApplication]delegate];
managedObjectContext = appDelegate.managedObjectContext;
NSManagedObjectContext *moc = [self managedObjectContext];
NSEntityDescription *entiAll = [NSEntityDescription entityForName:@"AllWeapons" inManagedObjectContext:moc];
NSFetchRequest *frAll = [[NSFetchRequest alloc] init];
[frAll setEntity:entiAll];
NSError *error = nil;
arrAll = [moc executeFetchRequest:frAll error:&error];
displayArray = [[NSMutableArray alloc]initWithArray:arrAll];
NSEntityDescription *entiRange = [NSEntityDescription entityForName:@"WeaponsRanged" inManagedObjectContext:moc];
NSFetchRequest *frRanged = [[NSFetchRequest alloc] init];
[frRanged setEntity:entiRange];
NSError *errorRanged = nil;
arrRange = [moc executeFetchRequest:frRanged error:&errorRanged];
NSLog(@"%i, %i", [arrRange count], [[moc executeFetchRequest:frRanged error:&errorRanged] count]);
NSEntityDescription *entiMelee = [NSEntityDescription entityForName:@"WeaponsMelee" inManagedObjectContext:moc];
NSFetchRequest *frMelee = [[NSFetchRequest alloc] init];
[frMelee setEntity:entiMelee];
NSError *errorMelee = nil;
arrMelee = [moc executeFetchRequest:frMelee error:&errorMelee];
NSLog(@"%i, %i", [arrMelee count], [[moc executeFetchRequest:frMelee error:&errorMelee] count]);
的问题是,中间的一个(一个填充arrRange阵列)不工作..
arrAll注销了所有正确的数据,arrMelee注销所有的正确的数据(由于某些原因x4,不知道这是否是相关的:S),但arrRange作为空数组注销。 [arrRange count];
给我0,即使我知道那里有很多数据。 我在模拟器上运行了这段代码,发现.sqlite文件,在Firefox的SQLite Manager中打开它,并看到了正确的数据,40行。
我进入appDelegate,在必要时填充CoreData,并看到以JSON格式下载数据的方法成功将其发送到sqlite。
这里我填的是CoreData从JSON数据:
[self deleteAllObjects:@"WeaponsRanged"];
NSManagedObjectContext *context = [self managedObjectContext];
for(NSDictionary *item in jsonWeaponRanged)
{
WeaponsRanged *wr = [NSEntityDescription insertNewObjectForEntityForName:@"WeaponsRanged"
inManagedObjectContext:context];
///***///
wr.recoil = [item objectForKey:@"Recoil"];
///***///
NSError *error;
if(![context save:&error])
NSLog(@"%@", [error localizedDescription]);
}
如果我在这里做NSLog(@"%@ - %@", wr.recoil, [item objectForKey:@"Recoil"]);
我得到正确的数据。 (两者的数据相同)
所以。正确的数据显然是核心。但是我的NSFetchRequest或者其他东西都失败了。在Objective-C中我非常喜欢,所以这可能是我的糟糕的代码语法再次引人注目。我意识到我应该再次使用这些东西,而不是一直创建新对象。但是,cmon,这是我的第一个应用程序。如果这实际上是问题,我可能会学习。但我卡住了。
有时我得到的数据,有时候我没有。有点奇怪。我重新启动了应用程序,并从中获取了数据,现在我不.. ..我还没有找到一个模式..
有人吗? 还是有另一种方式来从实体请求数据?
您是否从网络上获取JSON数据?如果是这样,你可以显示你的加载例程的代码。 –
嗯..我不认为这与它有任何关系..正如我所说的,数据总是在coredata中,但不会在查询时返回。我已经在所有这些方法中使用了相同的方法,并且我已经证明了.sqlite通过在FireFox中的SQLite管理器中打开它来包含数据。 – Sti
你可以遍历数组并打印'WeaponsRanged'对象吗?例如'for(NSManagedObject * obj in arrRange){NSLog(@“ - >%@”,[obj valueForKey:@“aKey”]); }'。让我知道你看到了什么。 –