views:

152

answers:

2

Hello,

I have an application that reads an rss feed, parses the xml and adds it to my database using Core Data (this is so the user can see the feed even if no internet connection is available) this all works fine. The way I am doing the parsing is: on the didStartElement i create a new Entity such as:

NewsDB *newsDB = [NSEntityDescription insertNewObjectForEntityForName:@"NewsDB" inManagedObjectContext:managedObjectContext];
self.currentObject = newsDB;

and in the didendDocument i just save everything with something such as:

- (void)parserDidEndDocument:(NSXMLParser *)parser {

NSError *error = nil;

if (![managedObjectContext save:&error])
{
 NSLog(@"Error saving %@", error);
}

This al works perfectly fine, in fact my program works just the way I want it now. But my question is when the managed object context gets saved the items seem to be added randomly, this is the first created object in the context may not be the first row in the database. I fixed this by adding a column that tells me the position in the xml, and then simply sorting by this column in my fetchedResultsController.

I know I could just save the context every time an item ends, but that doesn't sound like a good approach, so I just save them all at the end.

My question is why do they get added randomly?, is this the normal behavior?. Thank you.

-Oscar

A: 

This is normal behavior. You may have also noticed that when you create a to-many relationship that the related property is an unordered collection (NSSet).

When order matters, simply add a number attribute for sorting as you have done.

gerry3
Thank you, I did not realize that this was a NSSet.
OscarMk
+2  A: 

The currency of Core Data are NSSet* instances. Sets are unordered, so anything you add and then fetch back will come back to you without any intrinsic ordering.

So you just need to apply an NSSortDescriptor when you initialize your NSFetchedResultsController. You can apply as many sort orderings as you like, e.g.:

NSSortDescriptor *lastNameDescriptor = [[NSSortDescriptor alloc] initWithKey:@"lastName" ascending:YES selector:@selector(caseInsensitiveCompare:)];
NSSortDescriptor *firstNameDescriptor = [[NSSortDescriptor alloc] initWithKey:@"firstName" ascending:YES selector:nil];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:lastNameDescriptor, firstNameDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
[sortDescriptors release];
[lastNameDescriptor release];
[firstNameDescriptor release];

...

NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:@"keyInitial" cacheName:@"MyObjects"];
self.fetchedResultsController = aFetchedResultsController;
self.fetchedResultsController.delegate = self;
[aFetchedResultsController release];
[fetchRequest release];

NSError *error;
if (![self.fetchedResultsController performFetch:&error]) {
    // handle error...
}
Alex Reynolds