AssetType List View Doesn't Stay In Order


#1

My app is working but while testing and generally running the app, I’ve noticed that asset type list sometimes does’t always present in alphabetical order.

Is there a reason for this? I thought the code would always force it to be in alphabetical order as all assets are in alphabetical order (in the code). Have I missed something in the book?

Thanks in advance
Diggerbyte


#2

Following as per the book, your Asset Types will be returned in a random order each time.

If you look at the code for loadAllItems:, you will see the lines:

NSSortDescriptor *sd = [NSSortDescriptor sortDescriptorWithKey:@"orderingValue" ascending:YES]; [request setSortDescriptors:[NSArray arrayWithObject:sd]];

All items will be returned in the order that they were saved, because we have created the attribute orderingValue in the data model.

The code for allAssetTypes: does not invoke a sort for the returned items, so the asset types are returned in a random order each time.

I have tried setting a sortDescriptor in allAssetTypes, however it doesn’t work properly:

NSSortDescriptor *sd = [NSSortDescriptor sortDescriptorWithKey:@"label" ascending:YES selector:@selector(caseInsensitiveCompare:)]; [request setSortDescriptors:[NSArray arrayWithObject:sd]];

If you have implemented the Silver Challenge at the end of this chapter, when adding a new asset type, it simply goes to the bottom of the table view.

The asset types do not appear in order again until you quit and reopen the application.

Nick
http://myfirstiphoneapp.co.uk
http://easyintervalsapp.co.uk


#3

Hi and thanks again for your help.

I was wondering if the better implementation would be an indexed list like a sectioned table (Sudan, 3rd ed., ch 11, p. 581)? Another thought is to have all the assets in a text file and read/write them in order to the table view.

This may require a substantial rewrite to our existing code? Any thoughts?

Thanks in advance
Diggerbyte


#4

Even if you had the asset types in a text file, you would still need a way of ordering them alphabetically when they were displayed in the table view (sectioned or not).

This could be done by reading the contents of the text file into an array and sorting that array using something like:

However, why add another means of storing the asset types when the application is already using core data?

All you need to do is figure out how to sort the asset types, either from the core data end, or in the table view.

Nick


#5

OK, I’ll try that. Thanks for your help. I’ll check the class reference for NSSortDescriptor to see what other options are available.

Diggerbyte


#6

Hi Fujilla

I read the Apple reference on NSSortDescriptor and used your code fragment and it worked. It identified my duplicate assets as I have put in about 30 items. Here’s what I did.

[code] NSFetchRequest *request = [[NSFetchRequest alloc] init];
  NSEntityDescription *e = [[model entitiesByName] objectForKey:@"BNRShoeColor"];
  [request setEntity:e];
   NSSortDescriptor *sd = [NSSortDescriptor sortDescriptorWithKey:@"label"
                                                                         ascending:YES selector:@selector(caseInsensitiveCompare:)];
   [request setSortDescriptors:[NSArray arrayWithObject:sd]]; // This sorts the array
   NSError *error;
    NSArray *result = [context executeFetchRequest:request error:&error];
    if (!result) {
        [NSException raise:@"Fetch failed"
                        format:@"Reason: %@", [error localizedDescription]];
    }
    allAssetTypes = [result mutableCopy];[/code]

Hope this helps.

Also, have you any ideas to solve my other problem: Can’t Get Asset Type To First Screen? Its the Topic above this one.

Thanks again
Diggerbyte