Note that there are some explanatory texts on larger screens.

plurals
  1. PONot getting data from Core Data
    text
    copied!<p>I am using Core Data to store some information for my app. I have a .xcdatamodeld file containing 8 entities, and I extract them on different views.</p> <p>In one of the viewControllers, I call three of them. Like this:</p> <pre><code> 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:&amp;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:&amp;errorRanged]; NSLog(@"%i, %i", [arrRange count], [[moc executeFetchRequest:frRanged error:&amp;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:&amp;errorMelee]; NSLog(@"%i, %i", [arrMelee count], [[moc executeFetchRequest:frMelee error:&amp;errorMelee] count]); </code></pre> <p>The problem is that the middle one (the one filling the arrRange-array) doesn't work..</p> <p>arrAll logs out with all correct data, arrMelee logs out with all the correct data (x4 for some reason, don't know if this is related :S), but arrRange logs out as an empty array. <code>[arrRange count];</code> gives me 0, even though I know there is lots of data there. I ran this code on the simulator, and found the .sqlite file, opened it in Firefox's SQLite Manager, and saw the correct data, 40 rows.</p> <p>I went into the appDelegate, where I fill the CoreData when necessary, and saw that the method which downloads the data in JSON-format successfully sends it to the sqlite aswell.</p> <p>Here I fill the CoreData with data from the json:</p> <pre><code>[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:&amp;error]) NSLog(@"%@", [error localizedDescription]); } </code></pre> <p>And if I here do <code>NSLog(@"%@ - %@", wr.recoil, [item objectForKey:@"Recoil"]);</code> I get the correct data. (Same data on both)</p> <p>So. The correct data is obviously in the core. But my NSFetchRequest or something is failing. I am pretty noob at Objective-C, so it might be my bad code-grammar striking again. I realize I should use things again etc, not creating new objects all the time.. But cmon, this is my first app.. And if that is actually the problem, I might learn. But I'm stuck.</p> <p>SOMETIMES I get data, sometimes I don't. It's weird. I re-launched the app, and got data from it, and now I don't.. I haven't found a pattern yet..</p> <p>Anyone? Or is there another way to request data from the entity?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload