My data model is multi-tiered . Let me give an example to demonstrate the data model.
I have three entities say X , Y and Z .
X could have 1 or more Y's.
Y is associated only with 1 X.
Y could have 1 or more Z's.
Z is associated only with 1 Y .
Now my top tableview controller - Controller A uses a fetchedResults controller to get me all the X's from core data
When the user selects a particular row , he/she selects a particular X from which I have the list of all Y's under this X . A particular Y which is passed to controller B . I also pass the managed objected context from A to B .
In B I want to be able to display all the Z's associated with the Y but in some order . Currently I find that the order is indicative of the Most recently used Z . I would want it in the reverse order .
How do I specify ( in terms of core data) to be able to act upon this specific Y and get me results based on a particular criterion ? All the examples for core data that i ve seen so far start from scratch as shown below
NSManagedObjectContext *context = <#Get the context#>;
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:#"<#Entity name#>"
inManagedObjectContext:context];
[fetchRequest setEntity:entity];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:#"<#Sort key#>"
ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
NSError *error = nil;
NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];
if (fetchedObjects == nil) {
// Handle the error
}
[fetchRequest release];
[sortDescriptor release];
[sortDescriptors release];
One solution to this is to NOT use NSFetchedRequest but to act upon the passed Y object and sort the NSArray .
NSAssert1(yObject!=nil,#"yObject is null",1);
NSSet *zObjects = yObject.zobjectsinY;
NSArray *zObjectArray = [zObjects allObjects] ;
sortedQZArray = [zObjectArray sortedArrayUsingFunction:intSort context:NULL];
I feel that this is not an elegant solution and there should be a better way to query for these results .
Also another option is to be able to insert these values in the expected order when creating the data ? How can I specify the order for a record that is to be inserted in an entity object ?
Thanks in advance
If your getting the reverse of the sort order you want, just reverse the accending; parameter of the sort.
How can I specify the order for a
record that is to be inserted in an
entity object ?
You can't. Sorting is considered an attribute of the interface (because the same data may appear in with many different orders) so managed objects are kept in unordered sets until you fetch them with a sort descriptor.
If you want to save some kind of order in the data, you have to model the order using an attribute or relationship. Usually, however, you want to keep the order "virtual" in that it only appears as needed in the UI.
To get more complex sorts, create a sort with a comparator block. Be warned these can create unanticipated draws on memory and cycles so use them carefully.
You can also create a compare function for custom NSManagedObject subclasses that will compare any two objects of the class in anyway you wish. That is usually not necessary.
Related
// deleted all datas for entity before adding
for (int i=0; i<[tempArray count]; i+=3)
{
userData=(UserData *)[NSEntityDescription insertNewObjectForEntityForName:#"UserData" inManagedObjectContext:managedObjectContext];
[userData setUserIcon:tempArray[i]];
[userData setUserID:tempArray[i+1]];
[userData setUserName:tempArray[i+2]];
NSLog(#"loop values%#",[userData userName]);
}
The logged values give names in correct order. But the values stored in core data is not in proper order. E.g. value inside loop shows (apple,ball,cat,dog) but core data stored value shows (apple,cat,ball,dog). I checked values in core data by :
NSFetchRequest *fetchRequest1 = [NSFetchRequest fetchRequestWithEntityName:#"UserData"];
fetchRequest1.resultType = NSDictionaryResultType;
[fetchRequest1 setPropertiesToFetch:[NSArray arrayWithObjects:#"userName", nil]];
fetchRequest1.returnsDistinctResults = YES;
NSArray *dictionaries1 = [self.managedObjectContext executeFetchRequest:fetchRequest1 error:nil];
NSLog (#"names after: %#",dictionaries1);
Any idea why order is mismatching ???
Also alternatively, can I add the values into core data for a particular index so that I can order the table myself?
Or can i use sorting, but i should sort the entire row as the attribute in a row are dependent to each other.
Let me know if you need more information.
I agree with previous answer - your design based on insert order has no practical sense because such order can not be guaranteed and hard to maintain.
The best practice is to apply sort predicates.
For instance you may go to this SO question
My app sends a get request to a server with a date (date of last update) to update its content (2 entities in core data, no relationships, all attributes are strings)... most of the time it only receives new content but sometimes it also receives the old content that needs to be updated instead of just inserted. 1 table is pretty straight forward I just get the ids (id from server not form core data) of the items that are going to be updated in an array and I make a fetch of those items and then delete them. After that I insert the updated items as they were new. This is how I delete them:
-(void)deleteOfEntity:(NSString*)entityName theItemsWithIds:(NSArray*)ids{
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:entityName inManagedObjectContext: [self managedObjectContext]];
[fetchRequest setEntity:entity];
[fetchRequest setIncludesPropertyValues:NO];
[fetchRequest setPredicate:[NSPredicate predicateWithFormat:#"(id IN %#)", ids]];
NSError *error;
NSArray *fetchedObjects = [[self managedObjectContext] executeFetchRequest:fetchRequest error:&error];
if(fetchedObjects && [fetchedObjects count]>0){
for (NSManagedObject* toDelete in fetchedObjects) {
[[self managedObjectContext] deleteObject:toDelete];
}
}
}
because the attribute name which identifies each item is the ID as usually. But the other table needs 2 attributes to identify items, like a composite key. How do I build up the array of "ids"? an array with arrays of 2 values indicating the composite key? and the predicate? I just want to know if it is possible to do this efficiently, if not I can always fetch all the items and check 1 by 1 but for that I need a for inside another for and that is to ugly. Any help is appreciated.
When you designed the database you should have created a unique key field, even if it is just a composite of the two values. That would have made this question go away.
However, to solve the problem now you need to do a fetch on one key similar to what you have above and then loop over the second key. However, you do not need to do a loop within a loop. You can use a second NSPredicate against that returned array to get the objects to modify.
Less ugly and quite efficient since you are only going to disk once and the second filter is happening in memory.
Update
#DuncanGroenwald is correct that you still must loop through every object, but there is looping and there is looping.
A developer writing a for loop and then doing a string compare inside of that for loop is significantly less efficient then letting the frameworks perform the same option. How? With a NSPredicate against the array:
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"someValue IN %#", idArray];
NSArray *results = [origArray filteredArrayWithPredicate:predicate];
If you test both options, the predicate will run significantly faster.
Well what I did is to create another attribute named "identifier" which is a stringWithFormat:#"%#%#",key1,key2, it doesn't matter the extra string in coredata because it suppose to have just a few managed objects in that entity
I'm trying to build app with UITableView with data from NSFetchedResultsController. The problem is - I need to show some additional data from other models in table view.
Assume I have 2 models: Students and Groups (I think the relation is obvious: group consists of many students and student can be only in one group). I'm trying to build UITableView with list of groups. I would also like number of students in each group.
(NSFetchedResultsController *) fetchController {
if (!_fetchController) {
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:#"GroupModel" inManagedObjectContext:[NSManagedObjectContext MR_defaultContext]];
[fetchRequest setEntity:entity];
NSPredicate *messageInChatPredicate = [GroupModel getMyChatsPredicate];
[fetchRequest setPredicate:messageInChatPredicate];
NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:#"name" ascending:YES];
[fetchRequest setSortDescriptors:[NSArray arrayWithObject:sort]];
_fetchController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:[NSManagedObjectContext MR_defaultContext] sectionNameKeyPath:nil cacheName:#"main"];
_fetchController.delegate = self;
}
return _fetchController;
}
I wonder how I can I add additional information (such as number of students in each group)?
Why not just use group.students.count to get the value from the group entity? You only need one fetchedResultsController and I think you will find Core Data performance is fine.
Your terminology is a bit confusing, usually a model is a core data model created in Xcode and has a number of entities e.g. Group, Student. Sometimes you can use two different models and/or different versions of the models.
Your objects are usually know as Entities and both would belong to the same Core Data model.
EDIT:
To get the count of subgroups, such as girls or boys you can do the following
NSMutableArray *boys = [[NSMutableArray alloc] init];
[NSPredicate predicateWithFormat:#"gender = %#",#"MALE"];
[boys addObjectsFromArray:[group.students filteredArrayUsingPredicate:predicate]];
NSLog(#" boys count is %d", boys.count);
Assuming of course that gender is an attribute of Student.
Create another NSFetchedResultsController that is responsible for the other model (i.e student).
You have basically two options to get additional information such as the number of students in each group.
The first option is to count the students in the Group-Student relation. To serve this request the NSManagedObjectContext will load all students in memory to get a suitable result. Personally, I don't really like this option.
The second option is to store the number of students who belong to a group in a property of the group so that it can be directly add. Yes, you have to maintain the correct number manually. Depending on the amount of data that has to be loaded, this approach is more preferable. Crucially since it's way fas
I know there have been several discussions about this but none of them resolved my simple problem.
I have an Entity called Character and inside there are 4 columns:
character_id, episode_id, title, desc
there can be several same character_ids values but with different episode_id.
When I perform fetch\select I do it for whole table and wishes to get it distinctly by character_id. so this is what I do:
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:entityName inManagedObjectContext:moc];
[fetchRequest setEntity:entity];
// Add a sort descriptor. Mandatory.
if(sortDescriptors != nil) {
[fetchRequest setSortDescriptors:sortDescriptors];
}
fetchRequest.predicate = predicate;
// Required! Unless you set the resultType to NSDictionaryResultType, distinct can't work.
// All objects in the backing store are implicitly distinct, but two dictionaries can be duplicates.
// Since you only want distinct names, only ask for the 'name' property.
fetchRequest.resultType = NSDictionaryResultType;
fetchRequest.propertiesToFetch = [NSArray arrayWithObject:[[entity propertiesByName] objectForKey:#"title"]];
fetchRequest.returnsDistinctResults = YES;
NSArray *fetchResults = [moc executeFetchRequest:fetchRequest error:&error];
The 'fetchResults' array contains 3 out of 10 rows which is the right result!
The problem: None of the object within the array is accessible.
If I try the following:
NSDictionary item1 = [fetchResults objectAtIndex:0];
NSString *title = [item1 objectForKey:#title"];
I get an exception!
What am I doing wrong?? how can I translate back the dictionary into NSManagedObjects??
Thank you!
First, when using Core Data you should not use foreign keys. Rather, it is preferable to use Core Data's relationships and let the framework deal with the ids in an opaque manner. Maybe you are synching with a web service and need to keep track of the ids but your program logic actually should not depend on it.
Second, if you need an object, it is really better to use the NSManagedObjectResultType rather than the NSDictionaryResultType. You can still obtain distinct results. If you are not experiencing performance issues, this is the preferred pattern. The code is also much more readable.
Your data structure would be this, with a many-to-many relationship:
Character <<--->> Episode
All characters of an episode or all episodes with a certain character is simple. These will be "distinct" results dictated by the logic of the data model:
NSArray *allCharactersInEpisode = episode.characters;
NSArray *allEpisodesWithCharacter = character.episodes;
To select all characters of all episodes you just select all characters. Much simpler than a "distinct" query.
In the FRC documentation, it says that it is intended to efficiently manage the results returned from a Core Data fetch request to provide data for a UITableView object.
I am trying to setup my Core Data stack to handle the storage and retrieval of some VERY basic data. Can I not use FRC? I need to display a value set in the db to a UILabel, what is the best Core Data method to use for that?
I have a core data book I am trying to get through, but the going is rough, so any help here would go a long way. Thanks!!
NSFetchedResultsController may be overkill if all you want is to fetch an object and display one of its attributes in UILabel. Take a look at NSFetchRequest and start with something like this:
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setEntity:[NSEntityDescription entityForName:entityDescription
inManagedObjectContext:managedObjectContext]];
NSPredicate *predicate = /* define predicate here */;
[fetchReqest setPredicate:predicate];
NSError *error = nil;
NSArray *results = [managedObjectContext executeFetchRequest:fetchRequest error:&error];
[fetchRequest release];
If you want to sort your results, read up on NSSortDescriptor. You will need to set sort descriptors prior to -executeFetchRequest: call.
Your results will be in 'results' array - and they should include NSManagedObjects that you can get attribute values from.