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];
Related
In a section of my data model I'm using -
Department <-->> Person <<-->> Project
I want to list all person's of departmentA that have at least 1 project in common with persons in departmentB. For example, of the list of projects personA has there needs to be at least 1 person from departmentB who also has the project. If a personB also has that project then personA is on the list. I've been struggling for a while with subquery and the direction I'm going is below. It fails with 'to-many key not allowed' other attempt have given me 'parse issue'.
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription
entityForName:#"Project" inManagedObjectContext:_managedObjectContext];
[fetchRequest setEntity:entity];
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"projectToPerson.personToDepartment = %#", departmentB];
[fetchRequest setPredicate:predicate];
NSError *error = nil;
NSArray *projects = [_managedObjectContext executeFetchRequest:fetchRequest error:&error];
NSFetchRequest *fetchRequest2 = [[NSFetchRequest alloc] init];
NSEntityDescription *entity2 = [NSEntityDescription
entityForName:#"Person" inManagedObjectContext:_managedObjectContext];
[fetchRequest2 setEntity:entity2];
NSPredicate *predicate2 = [NSPredicate predicateWithFormat:#"personToDepartment = %# AND SUBQUERY(personToProject, $x, $x.projectName = %#).#count >0", departmentA,projects];
[fetchRequest2 setPredicate:predicate2];
I'm attempting to get an array of all projects and then list all persons of departmentA with a project that matches. Is this possible, and is this the right direction?
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"];
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
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.
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.