2011-03-01 19 views
4

我想用一個NSArrayController提供數據給一個NSTableView。我現在面臨的問題是,我不希望我的所有數據預先加載到一個數組,然後使用陣列控制器setContent:方法。我的數據模型是一個龐大的現有代碼庫,可管理數百萬條記錄。它包含有效地返回一組數據行的方法。NSArrayController的不加載一個大的數據集到一個數組

下面的例子我發現在限制NSArrayController中的對象數量時,我嘗試了繼承NSArrayController並覆蓋了arrangedObjects:方法來返回我寫的數組代理類。陣列代理類提供count:objectAtIndex:方法。由objectAtIndex:返回的對象是一個NSDictionary。當我試圖從arrangedObjects:方法既count:objectAtIndex:被調用返回我的數組代理,但我也得到一個無法識別的選擇錯誤在我的陣列代理類_valueForKeyPath:ofObjectAtIndex:。這看起來像一個私人方法,所以我沒有繼續下去。

我也想過從arrangedObjects:返回一個更小的數據陣列,但無法弄清楚我將如何確定NSTableView試圖顯示哪些行。

一個DataSource「正確」的方式與我現有的數據模型,接口或者是有一些方法,使一個NSArrayController工作?

回答

2

NSArrayController的工作已經與代理和索引和延遲加載和整個shabang。你有沒有嘗試過直接使用它?如果事後您覺得需要微管理數據加載,請使用NSFetchRequest。子類NSArrayController的,並添加一個初始沿着這些線路:

+ (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; 
} 

你必須做一些研究的各種可能性和配置,但你得到的圖片。

相關問題