2012-05-19 3 views
0

CoreDataModel과 함께 작동하는 Mac OSX 용 문서 응용 프로그램을 만들었습니다.NSManagedObjectContext가 PersistentDocument 외부에 있음

이제 단추를 클릭 할 때 프로그래밍 방식으로 값을 저장하려고합니다.

- (void)windowControllerDidLoadNib:(NSWindowController *)aController 
{ 
[super windowControllerDidLoadNib:aController]; 

NSManagedObjcetContext *moc = [self managedObjectContext]; 
NSSet *session = [moc fetchObjectsForEntityName:@"Sessions" withPredicate:nil]; 
NSLog(@"%d", (int)[session count]); 
NSManagedObject *obj = [NSEntityDescription insertNewObjectForEntityForName:@"Sessions" 
                inManagedObjectContext:moc]; 
[obj setValue:@"TEST" forKey:@"name"]; 
[obj setValue:[NSDate dateWithTimeIntervalSinceReferenceDate:112700] forKey:@"start"]; 
[obj setValue:[NSDate dateWithTimeIntervalSinceReferenceDate:118700] forKey:@"stop"]; 
} 

하지만 난 NSObject의 내부 카운터의 값을 저장하려면 : 응용 프로그램이 시작할 때

나는 값을 절약 할 수 있어요. 그래서 PersistentDocument에서 이전에 값을 전달하는 것과 같은 함수를 만들려고했지만 coredata 요소의 수는 0이므로 정확한 Entity를 참조하지 않는 것 같습니다.

아무에게도 내가 어떻게하는지 또는 어떻게 작동하는지 설명 할 수 있습니까?

감사 에일

편집 : 좀 더 명확하게하려고합니다

.

START 및 STOP 버튼이있는 크로노 카운터가 있습니다. 다른 버튼을 사용하여 coredata에서 SAVE가 중지되면 타이머의 값을 저장하려고합니다. 카운터 관리는 NSObject CounterObject에 있습니다.

어떻게하면됩니까? 이제 windowControllerDidLoadNib에서 함수를 호출하는 PersistentDocument에서만 핵심 데이터를 쓸 수 있습니다. coredata에 카운터 값을 쓰는 함수를 호출하고 싶지만 CounterObject에서 PersistentDocument의 함수를 호출하면 삽입 한 로그에 4가 아닌 0 요소가 표시됩니다. 올바르게 전달되지 않았습니다. 여기

코드입니다 :

// Document 
#import "Document.h" 
#import "NSManagedObjectContext.h" 

@implementation Document 

- (id)init 
{ 
self = [super init]; 
if (self) { 

} 
return self; 
} 

- (NSString *)windowNibName 
{ 
// Override returning the nib file name of the document 
// If you need to use a subclass of NSWindowController or if your document supports multiple NSWindowControllers, you should remove this method and override -makeWindowControllers instead. 
return @"Document"; 
} 

- (void)windowControllerDidLoadNib:(NSWindowController *)aController 
{ 
[super windowControllerDidLoadNib:aController]; 
[self saveTimeMOC:[NSDate dateWithString:@"11:23:34"]]; 
} 

-(IBAction)saveTimeMOC:(NSDate *)time { 

NSManagedObjectContext *moc = [self managedObjectContext]; 
NSSet *session = [moc fetchObjectsForEntityName:@"Sessions" withPredicate:nil]; 
NSLog(@"%d", (int)[session count]); 
NSManagedObject *obj = [NSEntityDescription insertNewObjectForEntityForName:@"Sessions" 
                inManagedObjectContext:moc]; 
[obj setValue:@"TEST" forKey:@"name"]; 
[obj setValue:time forKey:@"start"]; 
[obj setValue:[NSDate dateWithTimeIntervalSinceReferenceDate:118700] forKey:@"stop"]; 

} 

+ (BOOL)autosavesInPlace 
{ 
return YES; 
} 

@end 

// CounterObject 
#import "CounterObject.h" 
#import "Document.h" 

@implementation CounterObject 

@synthesize contText, startButton, stopButton; 

-(id)init { 
self = [super init]; 
if (self) { 

} 
return self; 
} 

- (IBAction)startContatore:(id)sender { 
stopButton.title = @"Stop"; 
[stopButton setEnabled:YES]; 
[startButton setEnabled:NO]; 
timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(gestioneTimer) userInfo:nil repeats:YES]; 
} 

- (IBAction)stopContatore:(id)sender { 
if (timer != nil) { 
    [timer invalidate]; 
    timer = nil; 
} 
if (stopButton.title != @"Reset") { 
    [startButton setEnabled:YES]; 
    stopButton.title = @"Reset"; 
    startButton.title = @"Continue"; 
} else if (stopButton.title == @"Reset") { 
    stopButton.title = @"Stop"; 
    [stopButton setEnabled:NO]; 
    startButton.title = @"Start"; 
    timerCont = 0; 
    contText.stringValue = [NSString stringWithFormat:@"00:00"]; 
} 
} 

- (void)gestioneTimer { 
timerCont += 1; 

int minutes = floor(timerCont/60); 
int seconds = trunc(timerCont - minutes * 60); 

contText.stringValue = [NSString stringWithFormat:@"%02i:%02i", minutes, seconds]; 
} 

- (IBAction)saveTime:(id)sender { 
Document *moc = [[Document alloc] init]; 
[moc saveTimeMOC:[NSDate dateWithString:@"13:45:22"]]; 
} 
+0

질문이 명확하지 않습니다. – samir

+0

나는 더 명확히하려고 노력할 것입니다. – stinkokenzo

+0

지금까지 가지고있는 문제를 이해하는 것은 어렵습니다. CounterObject가 업데이트를 수행하기 위해 사용하는 작동하지 않는 코드를 게시 하시겠습니까? – paulmelnikow

답변

1

는 핵심 데이터에 개체를 추가하려면, 당신이 할 수 있습니다 :

- (void)windowControllerDidLoadNib:(NSWindowController *)aController 
{ 
[super windowControllerDidLoadNib:aController]; 

NSManagedObjcetContext *moc = [self managedObjectContext]; 
// But be sure that [self managedObjectContext] is the correct one; you can do this to see if //it's not a null value : NSLog(@"%@",[self managedObjectContext]); 

NSManagedObject *obj = [NSEntityDescription insertNewObjectForEntityForName:@"Sessions" 
                inManagedObjectContext:moc]; 
[obj setValue:@"TEST" forKey:@"name"]; 
[obj setValue:[NSDate dateWithTimeIntervalSinceReferenceDate:112700] forKey:@"start"]; 
[obj setValue:[NSDate dateWithTimeIntervalSinceReferenceDate:118700] forKey:@"stop"]; 
} 

그리고 코어 데이터에 대한 요청은 다음과 같습니다

-(voi)testRequestCoreData { 
NSError *error =nil; 
NSFetchRequest *fetchRequest = [[[NSFetchRequest alloc] init]autorelease]; 
NSEntityDescription *entity = [NSEntityDescription 
    entityForName:@"Sessions" inManagedObjectContext:moc]; 
[fetchRequest setEntity:entity]; 
NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error]; 
.... 
} 
+0

답장을 보내 주셔서 감사합니다. 나는 그것을 할 수있다. SAVE 버튼을 사용하여 persistentdocument보기에서 가져온 값을 코어 데이터에 삽입 할 수 없습니다. – stinkokenzo

+0

당신의 코드 문제는 다음과 같다고 생각합니다 : [NSDate dateWithString @ "13:45:22"], NSDate 클래스의 문서와 메소드 dateWithString – samir

관련 문제