【问题标题】:How can I improve the performance for obtaining indexed sections for UITableView如何提高获取 UITableView 的索引部分的性能
【发布时间】:2013-04-15 16:51:32
【问题描述】:

我正在为表格视图创建索引部分,以显示 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 秒。

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

【问题讨论】:

    标签: performance uitableview mpmediaitemcollection


    【解决方案1】:

    由于没有其他建议,我发现我可以通过将以下 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: 以轻松获取艺术品和字幕)。它仍然可以使用一些改进,但更好。

    【讨论】:

      【解决方案2】:

      更好的处理方法是使用 NSOperationQueue 和 NSOperation。可以在here 找到与此问题相关的不错的教程。

      【讨论】:

        猜你喜欢
        • 2021-05-05
        • 1970-01-01
        • 2022-06-17
        • 2011-09-17
        • 1970-01-01
        • 1970-01-01
        • 2013-09-15
        • 2020-06-22
        • 2015-04-28
        相关资源
        最近更新 更多