Preloading data into core data - core-data

I am following a tutorial on preloading an existing database into core data. the code compiles fine until I get to this line:
NSEntityDescription *entity = [NSEntityDescription insertNewObjectForEntityForName:#"FailedBankInfo" inManagedObjectContext:context];
where it returns the following error: 2012-11-19 12:45:02.247 CoreDatTutorial2[2049:403] -[FailedBankInfo subentitiesByName]: unrecognized selector sent to instance 0x100301010'
I've checked to make sure the is not empty and that it has been saved into the the product directory. Any idea what I'm doing wrong? If I comment out that line of code, the project compiles without a problem.
here's the rest of the code:
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription insertNewObjectForEntityForName:#"FailedBankInfo" inManagedObjectContext:context];
[fetchRequest setEntity:entity];
NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];
for (FailedBankInfo *info in fetchedObjects){
{

insertNewObjectForEntityForName:inManagedObjectContext:
creates a new managed object and inserts it into the managed object context. In particular, it returns an object of the class FailedBankInfo (in your case), and not a NSEntityDescription.
To create a NSEntityDescription, use entityForName:inManagedObjectContext::
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:#"FailedBankInfo"
inManagedObjectContext:context];
[fetchRequest setEntity:entity];
Note that (starting with iOS 5.0) you can simplify this to
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:#"FailedBankInfo"];

Related

CoreData Field to a TableView

I checked before on the website, but no success.
I Think this is pretty simple but, I could't find it.
I'd like to update my TableView from a CoreData (Structure is entity : Projectors , Attribute : model)
Following code doesn't work (I would say of course, because something is missing, but I don't know). How can I load all data in my model to a NSMutableArray ?
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription
entityForName:#"model" inManagedObjectContext:context];
[fetchRequest setEntity:entity];
NSError *error;
mesProduits = [context executeFetchRequest:fetchRequest error:&error];

Fetching set of NSManagedObjects using attribute

How does one fetch a set of unique managed objects by requesting a specific attribute to be checked.
e.g. A number of people objects and I would like to retrieve all the unique names, one managed object for each unique name, sorted by name.
What about using a request like this
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:#"Person" inManagedObjectContext:managedObjectContext];
request.entity = entity;
request.propertiesToFetch = [NSArray arrayWithObject:[[entity propertiesByName] objectForKey:#"name"]];
request.returnsDistinctResults = YES;
request.resultType = NSDictionaryResultType;
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:#"name" ascending:YES];
[request setSortDescriptors:[NSArray arrayWithObject:sortDescriptors]];
NSError *error = nil;
NSArray *distinctResults = [managedObjectContext executeFetchRequest:request error:&error];
// Use distinctResults
Try and let me know.
P.S. Code is ARC enabled. If you are not using it, call release when necessary.

NSSortDescriptor is not working while fetching records from Core Data

I am trying to use NSSortDescriptor in Core Data to fetch my records. Array of modal-objects doesn't get affected by sort descriptor. It gives records in same order.Here is my code:
NSManagedObjectContext *moc=[self managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription
entityForName:#"To_Do" inManagedObjectContext:moc];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:#"repeatDate" ascending:NO];
NSArray *sortDescriptors = [NSArray arrayWithObjects:sortDescriptor, nil];
[sortDescriptor release];
[fetchRequest setSortDescriptors:sortDescriptors];
[fetchRequest setEntity:entity];
NSError * error = nil;
NSArray *arrEntity = nil;
arrEntity=[moc executeFetchRequest:fetchRequest error:&error];
The code looks fine and should work. Since it does not there a couple of possible causes.
The unsorted fetch order might be the same as sorted order. This can happen sometimes if you create objects in series and use a a key like a creation date or the like.
Your keys might all have the same value. This can happen if you have a default value.
You have the wrong key or misspelled its name. You should get a complaint but it won't crash if it can't find the key.
I had same problem if I had done like this
fetchRequest.sortDescriptors.append("key")
But if I use
fetchRequest.sortDescriptors = ["key"]
Then it works fine

CoreData: fetch only last elements, not all (iPhone)

Welcome
i use Core Data to store datas. i need such a method which returns only the last 7 elements of entity. my question is how should i modify this code ( it fetchs all of elements, but i need only last 7)
NSEntityDescription *entity = [NSEntityDescription entityForName:#"Trip" inManagedObjectContext:managedObjectContext];
NSFetchRequest *request = [[ NSFetchRequest alloc] init];
[request setEntity:entity];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:#"distance" ascending:NO];
NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
[request setSortDescriptors:sortDescriptors];
[sortDescriptor release];
NSError *error;
tripArray = [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy];
Define last? Core Data does not have a concept of order internally. If you mean by the farthest away based on your distance property then you can do the following:
NSEntityDescription *entity = [NSEntityDescription entityForName:#"Trip" inManagedObjectContext:managedObjectContext];
NSFetchRequest *request = [[ NSFetchRequest alloc] init];
[request setEntity:entity];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:#"distance" ascending:NO];
NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
[request setSortDescriptors:sortDescriptors];
[request setFetchLimit:7];
[sortDescriptor release];
NSError *error;
NSArray *tripArray = [managedObjectContext executeFetchRequest:request error:&error];
Note that the addition of the -setFetchLimit: will cause this request to only return 7 results. It will return the "first" 7 based on your sort. So if you want the closest, reverse the ascending: portion of your sort.
-mutableCopy
There is absolutely no point in calling -mutableCopy on the NSArray that is returned from -executeFetchRequest: error:. Adding objects to that NSArray will not add them to Core Data and removing them from that NSArray will not remove them from Core Data. Therefore it has absolutely no value and is just wasteful.
Do you remember where you saw that? I have been trying to track it down for a while now.

Core Data NSSortDescriptor causing memory leak

I am getting a memory leak when I add a sort descriptor to my Fetch Request in Core Data. If I comment out the NSSortDescriptor block it runs without a memory leak in Instruments.
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:#"Pools" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"game.league.id=%i",[lid intValue]];
[fetchRequest setPredicate:predicate];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:#"game.date" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
[sortDescriptors release];
[sortDescriptor release];
NSError *error;
NSArray *items = [managedObjectContext executeFetchRequest:fetchRequest error:&error];
// Do Something
[fetchRequest release];
[items release];
Any ideas?
In the sort descriptor, you're walking a relationship using the keypath game.date.
Your leak is probably associated with one of those objects in the keypath. It disappears when you remove the sort because then the objects in the key path don't do anything.
If you have transient properties, custom accessors, non-entity properties etc in the game entity I would look there. Custom value transformers are also a good bet.
The stack in Instrument should tell you exactly which object is leaking.

Resources