0

我已经为 Mac OSX 创建了一个文档应用程序来使用 CoreDataModel。

现在,我尝试在单击按钮时以编程方式将值保存在 coredata 中。

我可以在应用程序启动时保存一个值:

- (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,所以我认为这没有引用正确的实体。

谁能解释我如何做到这一点或如何工作?

谢谢麦芽酒

编辑:

我会尽量说得更清楚。

我有一个带有开始和停止按钮的计时计数器。我想使用另一个按钮保存计时器的值,当停止时,在 coredata 中。计数器的管理是在一个 NSObject CounterObject 中。

我该怎么做?现在,我只能从 PersistentDocument 中写入核心数据,并从 windowControllerDidLoadNib 调用函数。我想调用一个在 coredata 中写入我的计数器值的函数,但是如果我从 CounterObject 调用 PersistentDocument 中的函数,我插入的日志显示 0 个元素而不是 4。所以它没有正确传递。

这是代码:

// 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"]];
}
4

1 回答 1

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];
....
}
于 2012-05-19T11:20:36.147 回答