2012-11-04 7 views
2

나는 @sum 내 프로그램의 열 결합에 문제가 있습니다Coredata 및 NSPersistentDocument : 열 번호 충돌의 합계

가 나는 Coredata, NSPersistentDocument 기반 프로그램을하고 있어요합니다. IB, 데이터 모델 생성, NSArrayController 및 NSTableView에서 대부분 모든 작업을 수행합니다 ...

62 개의 속성 (61 NSString 및 1 NSNumber)을 가진 엔티티가 하나뿐입니다. 12722 개의 레코드가있는 CSV 파일을 가져옵니다. 가져 오기가 잘 작동하고 XML, 바이너리, sqlite에 저장할 수 있습니다 ... 전체 프로세스가 완벽하게 작동하는지 두 번 확인했습니다. 저장/불러올 수 있습니다. 모든 것이 있습니다.

내가 가지고있는 문제 : NSNumber 속성을 사용하여 @sum에 바인드 한 레이블을 만들었습니다. 이것은 내가 한 방법입니다

>  label->Bindings Inspector->Value 
>  Bind to: My_Entity_NSArrayController 
>  Controller Key: selection 
>  Model Key Path: @sum.myNumericAttribute 

프로그램을 실행할 때 가져 오기를 클릭하고 모든 행을 선택하면 @sum이 잘 작동합니다. 그것은 빠르고, 그러나 여기에 첫 번째 문제가 있습니다 : 한번 파일을 저장하면 (이진/xml/sqlite 모두 시도) 나중에 로드하고 ALL을 다시 시도하면 오류없이 프로그램이 중단됩니다..

"프로필"-> 할당 시도. 나는주의 :

  • 을 내가 디스크에서로드 할 때 메모리 누수
  • 이 다음 모두 선택하지 않습니다 extremelly 천천히 이동합니다. 5 분이 지나지 않아 (아직 멈추지 않았습니다.) 45MB의 CFNumber (라이브 바이트)와> 1.500.00 #을 보았습니다. 그래서, 여기에 뭔가 잘못되었습니다. Interger32 유형의 12722 행/레지스터에 대해 말하고 있습니다.

두 번째 문제는 동일하지만 다른 각도에서 재현됩니다. "selection"을 사용하는 대신 "arrangedObjects"를 사용하려고했습니다. 이 경우 문제는 CSV에서 가져 오는 중에도 나타납니다. 속도가 매우 느려지고 결국 충돌합니다. 이미 생성 된 파일을 열려고하면 크래시가 발생합니다.

이것은 레이블 -> 바인딩 Inspector-> 값

> label->Bindings Inspector->Value 
>  Bind to: My_Entity_NSArrayController 
>  Controller Key: arrangedObjects 
>  Model Key Path: @sum.myNumericAttribute 

은 당신이 또는 어디에 문제가 나를 찾을 수 있습니다 아이디어 무엇을 찾아야할지에 대한 몇 가지 빛으로 저를 도와주세요 수 있습니까 않았다 어떻게?

고마워요.

루이스


---- 더 많은 연구 AFTER NEW EDIT ---- 내가 정말, 제발 댓글/답변을 이해 '망가 해결 방법을 발견했습니다

고맙습니다.

내 프로그램에서는 Coredata (SQLite), NSPersistentDocument, NSTableView 및 NSArrayController를 사용합니다. @Sum Collection 작업에 바인드 된 작업 NSTextField를 갖고 싶습니다.

문제점 : SQLite DB가 채워진 기존 문서를 열면 NSWindowController의 @samples.t_24_Bookings에 바인딩하려고합니다. 프로그램 충돌.

내 초기 추측이 내가 처음으로 성공하지 않고 다음과 같이 가져 오기 수행의 추천을 따랐다 그러나 Cannot access contents of an object controller after a nib is loaded에 관련이 : 내가 찾은

- (void) awakeFromNib 
{ 
    : 
    BOOL ok = [[self out_CtEn_Transaction] fetchWithRequest:nil merge:NO error:&error]; 
    : 

이 아이디어를 계속을 그 내가 만들 경우 " 진짜 "완전한 Fetch + 나는 Document 서브 클래스에서 @sum 접근을 수행한다.

해결 방법을 사용하기 위해 내가 작성한 주석이있는 코드는 다음과 같습니다.

ABDocument 인터페이스

(A NSPersistentDocument 서브 클래스)

@interface ABDocument : NSPersistentDocument { 

    BOOL  ivNewDocument; 
    NSArray  *ivFetchedTransactions; 
    NSNumber *ivTotalBookings; 
} 

@property (nonatomic, getter=isNewDocument) BOOL newDocument; 
@property (nonatomic, retain) NSArray *fetchedTransactions; 
@property (nonatomic, retain) NSNumber *totalBookings; 

: 

ABDocument 구현

#import "ABDocument.h" 
#import "ABWindowController.h" 

@implementation ABDocument 

@synthesize newDocument     = ivNewDocument; 
@synthesize totalBookings    = ivTotalBookings; 
@synthesize fetchedTransactions   = ivFetchedTransactions; 

: 

/** @brief Create one instance of my custom NSWindowController subclass (ABWindowController) 
* 
* In my NSPersistentDocument I do override makeWindowControllers, where I create 
* one instance of my custom NSWindowController subclass and use addWindowController: 
* to add it to the document. 
* 
*/ 
- (void) makeWindowControllers 
{ 
    // Existing Document? 
    if (![self isNewDocument]) { 
     // NSLog(@"%@:%@ OPENING EXISTING DOCUMENT", [self class], NSStringFromSelector(_cmd)); 

     // Opening existing document (also has an existing DDBB (SQLite)), so 
     // make sure I do perform a first complete "fetch + @sum" to void issues 
     // with my NIB bind's. 
     [self firstFetchPreventsProblems]; 
    } 

    // Now I can create the Window Controller using my "MainWindow.xib". 
    ABWindowController *windowController = [[ABWindowController alloc] init]; 
    [self addWindowController:windowController]; 
    [windowController release];  
} 


/** @brief First complete "fetch + @sum" to void issues with my NIB bind's. 
* 
* Before I create the Window Controller with "MainWindow.xib" I have to perform a 
* first Fetch AND also retrieve a @sum of an NSNumber column. 
* 
* My NIB has an NSTextField BOUND to @[email protected]<property> through a NSArrayController 
* If I don't call this method before the NIB is loaded, then the program will crash. 
* 
*/ 
- (void) firstFetchPreventsProblems { 

    // Prepare the Fetch 
    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Transaction"]; 

    // 1) Perform the Fetch 
    NSError *error = nil; 
    [self setFetchedTransactions:[[self managedObjectContext ] executeFetchRequest:request error:&error]]; 
    if ([self fetchedTransactions] == nil) 
    { 
     NSLog(@"Error while fetching\n%@", 
       ([error localizedDescription] != nil) ? [error localizedDescription] : @"Unknown Error"); 
     exit(1); 
    } 

    // 2) Execute Collection Operation @sum 
    [self setTotalBookings:[[self fetchedTransactions] valueForKeyPath:@"@sum.t_24_Bookings"]]; 
} 

ABWindowController (내 NIB를로드 컨트롤러)

- (void)windowDidLoad 
{ 

: 
     // PROGRAM CRASH HERE 
     // IF [self firstFetchToPreventsProblems]; is NOT CALLED 
     // ABDocument's "makeWindowControllers:" 
     [[self totalSumField] bind: @"value" toObject: [self out_CtEn_Transaction] 
         withKeyPath:@"[email protected]_24_Bookings" options:nil]; 

} 

제발 당신이 정말로 감사하게 말할 수 있다면, 나는 해결책을 얻었지만 나는 왜 그런지 이해하지 못합니다.

탱크,
루이스

답변

0

나는 며칠이 연구 후에도 문제 나 자신을 발견했다. 그것은 (내가 아는 지금) 쉽다. :

병렬로 나는 보조 스레드를 만들고 있었고 두 개의 다른 스레드에서 데이터 모델에 액세스하고 있었다. 그것은 여러 Q에 설명되어있어 & 여기 Stackoverflow에서와 같이 매우 위험합니다.

보조 스레드에서 보조 MOC를 만드는 몇 가지 게시물에 주석 솔루션을 적용했습니다.

이제 내 코드는 coredata 관련 작업마다 스레드로부터 안전하므로 프로그램이 충돌하지 않습니다.

커뮤니티에 다시 한 번 감사드립니다.

루이스