好的。这是与此处完全相同的问题:Why is NSFetchedResultsController loading all rows when setting a fetch batch size?
但他的解决方案并不能解决我的问题。
我的屏幕上有几千条记录,加载它们的速度很慢。我将批处理大小设置为 30(大约是屏幕上单元格的三倍),但由于某种原因它仍然循环加载所有批处理。
这是代码
- (NSFetchedResultsController *)guestCardFetchedResultsController
{
if (guestCardFetchedResultsController != nil) {
return guestCardFetchedResultsController;
}
// SELECT * from GuestCard
NSFetchRequest* fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"GuestCard" inManagedObjectContext:self.context];
[fetchRequest setEntity:entity];
// ORDER BY updated DESC
NSSortDescriptor* updatedSortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"created" ascending:NO];
[fetchRequest setSortDescriptors:@[updatedSortDescriptor]];
fetchRequest.fetchBatchSize = 30;
NSString *cacheName = self.isReportProblemView ? @"reportProblemGuestCardsAll" : @"guestCardsAll";
[NSFetchedResultsController deleteCacheWithName:cacheName];
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.context sectionNameKeyPath:@"sectionIdentifier" cacheName:cacheName];
aFetchedResultsController.delegate = self;
self.guestCardFetchedResultsController = aFetchedResultsController;
// Clean up
NSError *error = nil;
if (![[self guestCardFetchedResultsController] performFetch:&error]) {
}
return self.guestCardFetchedResultsController;
}
在这种情况下,我没有做任何非常有趣的事情。下面是一些委托(delegate)代码(不包括单元格创建,我确认它只针对屏幕上的单元格数量调用):
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
if ([self.guestCardFetchedResultsController.fetchedObjects count] == 0) {
return 1;
}
// Return the number of sections.
return [[self.guestCardFetchedResultsController sections] count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if ([self.guestCardFetchedResultsController.fetchedObjects count] == 0) {
return 1;
}
// Return the number of rows in the section.
id <NSFetchedResultsSectionInfo> sectionInfo = [guestCardFetchedResultsController sections][section];
return [sectionInfo numberOfObjects];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
if ([self.guestCardFetchedResultsController.fetchedObjects count] == 0) {
return @"";
}
return [[self.guestCardFetchedResultsController sections][section] name];
}
最佳答案
看完docs on the NSFetchedResultsController initializer ,我认为问题在于您在获取请求(创建)中有一个排序自然不会与部分键路径(sectionIdentifier)相同。我正在查看的文档中的具体句子是这样说的:
If this key path is not the same as that specified by the first sort descriptor in fetchRequest, they must generate the same relative orderings
我建议修改您的获取请求以先对 sectionIdentifier 进行排序,然后再创建。我认为这会解决您的问题。
NSSortDescriptor* updatedSortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"created" ascending:NO];
NSSortDescriptor* sectionSortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"sectionIdentifier" ascending:NO];
// It's critical the sectionSortDescriptor is first
[fetchRequest setSortDescriptors:@[sectionSortDescriptor, updatedSortDescriptor]];
请注意,如果 created 或 sectionIdentifier 属性实际上是实体类上的方法,那肯定会强制 Core Data 在排序之前加载所有数据/部分,因为它需要先在每个实体上执行该方法。
关于ios - NSFetchedResultsController 正在加载所有行,即使我已经设置了批处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18906945/
我试图获取一个长度在1到10之间的字符串,并输出将字符串分解为大小为1、2或3的连续子字符串的所有可能方式。例如:输入:123456将整数分割成单个字符,然后继续查找组合。该代码将返回以下所有数组。[1,2,3,4,5,6][12,3,4,5,6][1,23,4,5,6][1,2,34,5,6][1,2,3,45,6][1,2,3,4,56][12,34,5,6][12,3,45,6][12,3,4,56][1,23,45,6][1,2,34,56][1,23,4,56][12,34,56][123,4,5,6][1,234,5,6][1,2,345,6][1,2,3,456][123
鉴于我有以下迁移:Sequel.migrationdoupdoalter_table:usersdoadd_column:is_admin,:default=>falseend#SequelrunsaDESCRIBEtablestatement,whenthemodelisloaded.#Atthispoint,itdoesnotknowthatusershaveais_adminflag.#Soitfails.@user=User.find(:email=>"admin@fancy-startup.example")@user.is_admin=true@user.save!ende
当我的预订模型通过rake任务在状态机上转换时,我试图找出如何跳过对ActiveRecord对象的特定实例的验证。我想在reservation.close时跳过所有验证!叫做。希望调用reservation.close!(:validate=>false)之类的东西。仅供引用,我们正在使用https://github.com/pluginaweek/state_machine用于状态机。这是我的预订模型的示例。classReservation["requested","negotiating","approved"])}state_machine:initial=>'requested
我收到这个错误:RuntimeError(自动加载常量Apps时检测到循环依赖当我使用多线程时。下面是我的代码。为什么会这样?我尝试多线程的原因是因为我正在编写一个HTML抓取应用程序。对Nokogiri::HTML(open())的调用是一个同步阻塞调用,需要1秒才能返回,我有100,000多个页面要访问,所以我试图运行多个线程来解决这个问题。有更好的方法吗?classToolsController0)app.website=array.join(',')putsapp.websiteelseapp.website="NONE"endapp.saveapps=Apps.order("
我有这个html标记:我想得到这个:我如何使用Nokogiri做到这一点? 最佳答案 require'nokogiri'doc=Nokogiri::HTML('')您可以通过xpath删除所有属性:doc.xpath('//@*').remove或者,如果您需要做一些更复杂的事情,有时使用以下方法遍历所有元素会更容易:doc.traversedo|node|node.keys.eachdo|attribute|node.deleteattributeendend 关于ruby-Nokog
我已经构建了一些serverspec代码来在多个主机上运行一组测试。问题是当任何测试失败时,测试会在当前主机停止。即使测试失败,我也希望它继续在所有主机上运行。Rakefile:namespace:specdotask:all=>hosts.map{|h|'spec:'+h.split('.')[0]}hosts.eachdo|host|begindesc"Runserverspecto#{host}"RSpec::Core::RakeTask.new(host)do|t|ENV['TARGET_HOST']=hostt.pattern="spec/cfengine3/*_spec.r
这里有一个很好的答案解释了如何在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返回它复制的字节数,但是当我还没有下
我想获取模块中定义的所有常量的值:moduleLettersA='apple'.freezeB='boy'.freezeendconstants给了我常量的名字:Letters.constants(false)#=>[:A,:B]如何获取它们的值的数组,即["apple","boy"]? 最佳答案 为了做到这一点,请使用mapLetters.constants(false).map&Letters.method(:const_get)这将返回["a","b"]第二种方式:Letters.constants(false).map{|c
我正在尝试解析一个文本文件,该文件每行包含可变数量的单词和数字,如下所示:foo4.500bar3.001.33foobar如何读取由空格而不是换行符分隔的文件?有什么方法可以设置File("file.txt").foreach方法以使用空格而不是换行符作为分隔符? 最佳答案 接受的答案将slurp文件,这可能是大文本文件的问题。更好的解决方案是IO.foreach.它是惯用的,将按字符流式传输文件:File.foreach(filename,""){|string|putsstring}包含“thisisanexample”结果的
我一直致力于让我们的Rails2.3.8应用程序在JRuby下正确运行。一切正常,直到我启用config.threadsafe!以实现JRuby提供的并发性。这导致lib/中的模块和类不再自动加载。使用config.threadsafe!启用:$rubyscript/runner-eproduction'pSim::Sim200Provisioner'/Users/amchale/.rvm/gems/jruby-1.5.1@web-services/gems/activesupport-2.3.8/lib/active_support/dependencies.rb:105:in`co