NSArrayController没有将大型数据集加载到数组中 [英] NSArrayController without loading a large dataset into an array
问题描述
我想使用NSArrayController向NSTableView提供数据。我面临的问题是,我不想预先加载所有的数据到数组,然后使用数组控制器 setContent:
方法。我的数据模型是一个大型的现有代码库,管理着数百万条记录。它包含有效地返回一组数据行的方法。
我发现了一个限制NSArrayController中对象数量的例子,我试图子类化NSArrayController并覆盖 arrangeObjects:
方法返回一个数组代理类我写的。数组代理类提供了 count:
和 objectAtIndex:
方法。 objectAtIndex:
返回的对象是NSDictionary。当我尝试从 arrangeObjects:
方法返回我的数组代理 count:
和 objectAtIndex:
得到调用,但我也得到一个无法识别的选择器错误在我的数组代理类 _valueForKeyPath:ofObjectAtIndex:
。这看起来像一个私有方法,所以我没有继续沿着这条路。
我还想到从 arrangeObjects:
返回一个较小的数据数组,确定NSTableView尝试显示哪些行。
是一个数据源是正确的方式与我现有的数据模型接口,还是有一些方法使NSArrayController工作?
< NSArrayController已经工作,代理和索引,延迟加载和整个shabang。你试过只是使用它是什么?如果之后你觉得需要微管理数据加载,请使用NSFetchRequest。子类NSArrayController,并沿着这些行添加一个初始化器:
+(id)arrayControllerWithEntityName:(NSString *)entityName error: *)error
{
id newInstance = [[[self alloc] initWithContent:nil] autorelease];
[newInstance setManagedObjectContext:[[NSApp delegate] managedObjectContext]];
[newInstance setEntityName:entityName];
[newInstance setAutomaticallyPreparesContent:YES];
[newInstance setSelectsInsertedObjects:YES];
[newInstance setAvoidsEmptySelection:YES];
[newInstance setAlwaysUsesMultipleValuesMarker:YES];
NSFetchRequest * dontGobbleRequest = [[[NSFetchRequest alloc] init] autorelease];
//使用fetchLimit和fetchOffset配置请求所有
NSError * err;
if([newInstance fetchWithRequest:dontGobbleRequest merge:YES error:& err] == NO){
//更好地检查文档是否合并:YES是你想要的
if &&&& err){
* error = err;
}
return nil;
}
return newInstance;
}
您必须对各种可能性和配置进行一些研究,你得到的图片。
I would like to use an NSArrayController to provide data to an NSTableView. The problem I am facing is that I do not want to pre-load all my data into an array and then use the array controllers setContent:
method. My data model is a large existing code base that manages millions of records. It contains methods to efficiently return a set of data rows.
Following an example I found on limiting the number of objects in an NSArrayController, I tried subclassing NSArrayController and overriding the arrangedObjects:
method to return an array proxy class I wrote. The array proxy class provided count:
and objectAtIndex:
methods. The object returned by objectAtIndex:
is an NSDictionary. When I tried returning my array proxy from the arrangedObjects:
method both count:
and objectAtIndex:
get called, but I also get an unrecognized selector error on my array proxy class for _valueForKeyPath:ofObjectAtIndex:
. This looked like a private method, so I did not continue down this path.
I also thought of returning a smaller array of data from arrangedObjects:
, but could not figure out how I would determine which rows the NSTableView was trying to display.
Is a datasource the "correct" way to interface with my existing data model or is there some way to make an NSArrayController work?
NSArrayController already works, with proxies and indexes and lazy-loading and the whole shabang. Have you tried just using it as-is? If afterwards you feel the need to micro-manage the data-loading, use NSFetchRequest. Subclass NSArrayController and add an initializer along these lines:
+ (id)arrayControllerWithEntityName: (NSString *)entityName error:(NSError **)error
{
id newInstance = [[[self alloc] initWithContent:nil] autorelease];
[newInstance setManagedObjectContext:[[NSApp delegate] managedObjectContext]];
[newInstance setEntityName:entityName];
[newInstance setAutomaticallyPreparesContent:YES];
[newInstance setSelectsInsertedObjects:YES];
[newInstance setAvoidsEmptySelection:YES];
[newInstance setAlwaysUsesMultipleValuesMarker:YES];
NSFetchRequest *dontGobbleRequest = [[[NSFetchRequest alloc] init] autorelease];
//configure the request with fetchLimit and fetchOffset an' all that
NSError *err;
if ([newInstance fetchWithRequest:dontGobbleRequest merge:YES error:&err] == NO) {
//better check docs whether merge:YES is what you want
if(*error && err) {
*error = err;
}
return nil;
}
return newInstance;
}
You'll have to do some research into the various possibilities and configurations, but you get the picture.
这篇关于NSArrayController没有将大型数据集加载到数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!