如何提高获取UITableView的索引部分的性能

时间:2013-04-15 16:51:33

标签: performance uitableview mpmediaitemcollection

我正在为表格视图创建索引部分,以显示iPod库中的歌曲标题。为了实现这一点,我正在使用sectionForObject:collat​​ionStringSelector:循环遍历歌曲查询中的所有mediaItems。以下代码在viewDidLoad中执行,以获取表的每个部分的节数和行数:

        // Go through the collection of songs and collate them into sections A-Z
        for (MPMediaItemCollection *arrayItem in itemsFromSongQuery) {
            MPMediaItem *representativeItem = [arrayItem representativeItem];   // grab the next object from the collection
            NSString *songName = [representativeItem valueForProperty: MPMediaItemPropertyTitle];
            MPMediaItemArtwork *artWork = [representativeItem valueForProperty: MPMediaItemPropertyArtwork];

            channelButtonTitles *aChannelButtonTitle = [[channelButtonTitles alloc] init];  // create an object to hold both the title and section number
            aChannelButtonTitle.channelTitle = songName;                                                               // save the song name string in this object
            aChannelButtonTitle.channelSubTitle = [representativeItem valueForProperty: MPMediaItemPropertyArtist];    // save the artists string in this object
            aChannelButtonTitle.artwork = [artWork imageWithSize:CGSizeMake(30, 30)];                                   // save the artwork UIImage object
            aChannelButtonTitle.persistentID = [representativeItem valueForProperty:MPMediaEntityPropertyPersistentID]; // save this unique number in case the user wants to play this

            // Then determine which section number it belongs to.
            // The collator will use the channelTitle property of the aChannelButtonTitle object to make this determination
            NSInteger sect = [theCollation sectionForObject:aChannelButtonTitle collationStringSelector:@selector(channelTitle)];
            aChannelButtonTitle.sectionNumber = sect;   // Save the section number that this title string should be assigned to.

            [tempTitles addObject:aChannelButtonTitle]; // Copy the channelButtonTitles object that contains the title and section number in the temp array
            [aChannelButtonTitle release];              // Release the channelButtonTitles object
        }

不幸的是,对于2000首歌曲,这需要将近15秒。分配channelButtonTitle对象的时间和sectionForObject:collat​​ionStringSelector:的执行时间都可以忽略不计。

似乎大部分时间花在了获得valueForProperty的行中,尽管这行:

MPMediaItem *representativeItem = [arrayItem representativeItem];

需要5秒钟。

如何提高创建索引部分的性能(或者至少缩短用户等待事情发生的时间)?

2 个答案:

答案 0 :(得分:0)

由于缺乏任何其他建议,我发现我可以通过将以下2行移动到tableView:cellForRowAtIndexPath:

来缩短时间。
        MPMediaItemArtwork *artWork = [representativeItem valueForProperty: MPMediaItemPropertyArtwork];
        aChannelButtonTitle.artwork = [artWork imageWithSize:CGSizeMake(30, 30)];                                   // save the artwork UIImage object
        aChannelButtonTitle.channelSubTitle = [representativeItem valueForProperty: MPMediaItemPropertyArtist];    // save the artists string in this object

因此,viewDidLoad仍会捕获歌曲标题(用于准备索引表)和persistentID(以启用tableView:cellForRowAtIndexPath:轻松抓取图稿和副标题)。它仍然可以使用一些改进,但它更好。

答案 1 :(得分:0)

更好的方法是使用NSOperationQueue和NSOperation。可以找到与此问题相关的精彩教程here