jjzjj

ios - cellForRowAt indexPath 中 Realm 对象的 UITableView 性能

coder 2023-09-10 原文

我的 tableView 中有许多不同的类别部分是从一个名为 allProducts 的变量加载的它包含我所有的 Realm 对象(类型为 Results<Product> )。

但是,自从我引入这段代码以来,每个部分都加载了正确的产品:

switch productViewSegmentedControl!.selectedSegmentIndex {

    case 0:
        allProductsInSection = allProducts.filter("itemgroup = %@", allProductSections[indexPath.section])
    case 1:
        allProductsInSection = allProducts.filter("itembrand = %@", allProductSections[indexPath.section])
    case 2:
        allProductsInSection = allProducts.filter("itemtype = %@", allProductSections[indexPath.section])
    default:
        allProductsInSection = allProducts.filter("itemgroup = %@", allProductSections[indexPath.section])
    }

进入我的cellForRowAt indexPath方法,在有很多项目的 tableView 部分滚动时,UI 会滞后。

tableView 中的每个单元格都包含 let product = allProductsInSection![indexPath.row] . product常量保存我的 Product 中每个项目的属性模型类。

如何提高 UI 的性能?

附言每个单元格都已被重用:

let cell = tableView.dequeueReusableCell(withIdentifier: "ProductCell") as? OrderFormViewCell
            ?? UITableViewCell(style: .subtitle, reuseIdentifier: "ProductCell") as! OrderFormViewCell

最佳答案

tableView(_:cellForRowAt:) 方法中使用您描述的 switch 语句意味着您强制 Realm 在每次单元格时重新过滤产品即将显示。从您共享的代码来看,您似乎有一个分段控件来控制要显示的产品。不要在准备每个单元格时检查分段控件的选择,而是应该对其选择更改使用react,并将您的产品过滤到 tableView(_:cellForRowAt:) 可以使用的实例变量中。这将允许 Realm 仅在标准发生变化时过滤产品,而不是每次要显示单元格时。

由于您需要在 TableView 的每个部分中使用不同的值进行过滤,因此您需要为 TableView 的每个部分准备一个 Results。在分段控件的选择更改时调用的方法中:

var productsPerSection = []
for section in 0..numberOfSections {
    let productsInSection: Results<Product>
    switch productViewSegmentedControl!.selectedSegmentIndex {
        case 0:
            productsInSection = allProducts.filter("itemgroup = %@", allProductSections[section])
        case 1:
            productsInSection = allProducts.filter("itembrand = %@", allProductSections[section])
        case 2:
            productsInSection = allProducts.filter("itemtype = %@", allProductSections[section])
        default:
            productsInSection = allProducts.filter("itemgroup = %@", allProductSections[section])
    }
    productsPerSection.append(productsInSection)
}

self.productsPerSection = productsPerSection

然后在您的 tableView(_:cellForRowAt:) 方法中,使用 self.productsInSection[indexPath.section] 而不是过滤来创建 allProductsInSection.

注意:该代码是未经测试的草图,有望传达有关该方法的总体思路,但可能无法编译。

关于ios - cellForRowAt indexPath 中 Realm 对象的 UITableView 性能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43263158/

有关ios - cellForRowAt indexPath 中 Realm 对象的 UITableView 性能的更多相关文章

  1. ruby - 如何从 ruby​​ 中的字符串运行任意对象方法? - 2

    总的来说,我对ruby​​还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用

  2. ruby-on-rails - 按天对 Mongoid 对象进行分组 - 2

    在控制台中反复尝试之后,我想到了这种方法,可以按发生日期对类似activerecord的(Mongoid)对象进行分组。我不确定这是完成此任务的最佳方法,但它确实有效。有没有人有更好的建议,或者这是一个很好的方法?#eventsisanarrayofactiverecord-likeobjectsthatincludeatimeattributeevents.map{|event|#converteventsarrayintoanarrayofhasheswiththedayofthemonthandtheevent{:number=>event.time.day,:event=>ev

  3. ruby-on-rails - 如何验证非模型(甚至非对象)字段 - 2

    我有一个表单,其中有很多字段取自数组(而不是模型或对象)。我如何验证这些字段的存在?solve_problem_pathdo|f|%>... 最佳答案 创建一个简单的类来包装请求参数并使用ActiveModel::Validations。#definedsomewhere,atthesimplest:require'ostruct'classSolvetrue#youcouldevencheckthesolutionwithavalidatorvalidatedoerrors.add(:base,"WRONG!!!")unlesss

  4. Ruby 写入和读取对象到文件 - 2

    好的,所以我的目标是轻松地将一些数据保存到磁盘以备后用。您如何简单地写入然后读取一个对象?所以如果我有一个简单的类classCattr_accessor:a,:bdefinitialize(a,b)@a,@b=a,bendend所以如果我从中非常快地制作一个objobj=C.new("foo","bar")#justgaveitsomerandomvalues然后我可以把它变成一个kindaidstring=obj.to_s#whichreturns""我终于可以将此字符串打印到文件或其他内容中。我的问题是,我该如何再次将这个id变回一个对象?我知道我可以自己挑选信息并制作一个接受该信

  5. ruby-on-rails - 如果 Object::try 被发送到一个 nil 对象,为什么它会起作用? - 2

    如果您尝试在Ruby中的nil对象上调用方法,则会出现NoMethodError异常并显示消息:"undefinedmethod‘...’fornil:NilClass"然而,有一个tryRails中的方法,如果它被发送到一个nil对象,它只返回nil:require'rubygems'require'active_support/all'nil.try(:nonexisting_method)#noNoMethodErrorexceptionanymore那么try如何在内部工作以防止该异常? 最佳答案 像Ruby中的所有其他对象

  6. ruby-on-rails - 未在 Ruby 中初始化的对象 - 2

    我在Rails工作并有以下类(class):classPlayer当我运行时bundleexecrailsconsole然后尝试:a=Player.new("me",5.0,"UCLA")我回来了:=>#我不知道为什么Player对象不会在这里初始化。关于可能导致此问题的操作/解释的任何建议?谢谢,马里奥格 最佳答案 havenoideawhythePlayerobjectwouldn'tbeinitializedhere它没有初始化很简单,因为你还没有初始化它!您已经覆盖了ActiveRecord::Base初始化方法,但您没有调

  7. ruby - 如何在 Rails 4 中使用表单对象之前的验证回调? - 2

    我有一个服务模型/表及其注册表。在表单中,我几乎拥有服务的所有字段,但我想在验证服务对象之前自动设置其中一些值。示例:--服务Controller#创建Action:defcreate@service=Service.new@service_form=ServiceFormObject.new(@service)@service_form.validate(params[:service_form_object])and@service_form.saverespond_with(@service_form,location:admin_services_path)end在验证@ser

  8. ruby - 如何验证 IO.copy_stream 是否成功 - 2

    这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下

  9. Ruby 文件 IO 定界符? - 2

    我正在尝试解析一个文本文件,该文件每行包含可变数量的单词和数字,如下所示:foo4.500bar3.001.33foobar如何读取由空格而不是换行符分隔的文件?有什么方法可以设置File("file.txt").foreach方法以使用空格而不是换行符作为分隔符? 最佳答案 接受的答案将slurp文件,这可能是大文本文件的问题。更好的解决方案是IO.foreach.它是惯用的,将按字符流式传输文件:File.foreach(filename,""){|string|putsstring}包含“thisisanexample”结果的

  10. ruby - 一个 YAML 对象可以引用另一个吗? - 2

    我想让一个yaml对象引用另一个,如下所示:intro:"Hello,dearuser."registration:$introThanksforregistering!new_message:$introYouhaveanewmessage!上面的语法只是它如何工作的一个例子(这也是它在thiscpanmodule中的工作方式。)我正在使用标准的ruby​​yaml解析器。这可能吗? 最佳答案 一些yaml对象确实引用了其他对象:irb>require'yaml'#=>trueirb>str="hello"#=>"hello"ir

随机推荐