【发布时间】:2012-08-28 18:50:00
【问题描述】:
我正在使用 Core Data 为我的应用程序存储一些信息。 我有一个包含 8 个实体的 .xcdatamodeld 文件,我将它们提取到不同的视图中。
在其中一个 viewController 中,我调用了其中的三个。像这样:
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-array 的那个)不起作用..
arrAll 以所有正确数据注销,arrMelee 以所有正确数据注销(x4 出于某种原因,不知道这是否相关:S),但 arrRange 作为空数组注销。
[arrRange count]; 给了我 0,即使我知道那里有很多数据。
我在模拟器上运行了这段代码,找到了 .sqlite 文件,在 Firefox 的 SQLite Manager 中打开它,看到了正确的数据,40 行。
我进入 appDelegate,在必要时填写 CoreData,并看到以 JSON 格式下载数据的方法也成功将其发送到 sqlite。
这里我用来自 json 的数据填充 CoreData:
[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 中,但不会在查询中返回。我在所有这些中都使用了相同的方法,并且通过在 FireFox 的 SQLite 管理器中打开它来证明 .sqlite 包含数据。
-
你能遍历数组并打印
WeaponsRanged对象吗?例如for(NSManagedObject* obj in arrRange) { NSLog(@"--> %@", [obj valueForKey:@"aKey"]); }。让我知道你看到了什么。 -
@Flex_Addicted 嗯..当我现在在模拟器中启动应用程序时,我无法重现错误。我不知道它什么时候来袭。自从我写这篇文章以来,我什么也没做。所以注销了很多东西。但是,当我在 ios 设备上运行它时,它没有显示任何内容。数组不工作时为空。 [arrRange 计数] = 0。
标签: objective-c ios cocoa-touch core-data nsfetchrequest