i have the code below that iterate on every book on my bookObjects array, my problem is addObject is inserting/adding my aBook object on random index instead of adding it at the end of self.books
i just want it to follow the order of the books in bookObjects, no specific order or sorting.
books is an NSMutableArray
NSArray *bookObjects = [element childrenNamed:@"books"];
int i = 0;
for(SMXMLElement *child in bookObjects)
{
NSString *isbn = [child valueWithPath:@"isbn"];
NVBook *aBook = [self getBookByISBN:isbn];
if(nil == aBook)
{
aBook = [NVBook insertBookInContext:[self managedObjectContext]];
aBook.isbn = isbn;
[self.books addObject:aBook];
}
aBook.indexInList = i++;
[aBook updateContentWithElement:child];
}
additional code
+ (NVBook *)insertBookInContext:(NSManagedObjectContext*)context
{
return [NSEntityDescription insertNewObjectForEntityForName:@"Book" inManagedObjectContext:context];
}
- (NVBook *)getBookByISBN:(NSString *)aISBN
{
NVBook *aBook = nil;
for(NVBook *book in self.books)
{
if([book.isbn isEqual:aISBN])
{
aBook = book;
break;
}
}
return aBook;
}
NSLog(@"%@", aBook);
inside your loop to verify that it is called in the order you expect. – James Sep 24 '12 at 2:26