Cocoa Core数据:设置默认实体属性值? [英] Cocoa Core Data: Setting default entity property values?

查看:153
本文介绍了Cocoa Core数据:设置默认实体属性值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我可以在数据模型中或在实体类的-awakeFromInsert方法中设置默认值。例如,要使date属性默认为当前日期:

I know I can set default values either in the datamodel, or in the -awakeFromInsert method of the entity class. For example, to make a "date" property default to the current date:

- (void) awakeFromInsert
{
NSDate *now = [NSDate date];
self.date = now;
}

如何使idNumber属性默认为大于上一个对象的idNumber?

How though can I make an "idNumber" property default to one greater than the previous object's idNumber?

感谢,Oli

编辑:我的尝试的相关代码)

- (void) awakeFromInsert
{
self.idNumber = [NSNumber numberWithInt:[self maxIdNumber] + 1];
}

-(int)maxIdNumber{
NSManagedObjectContext *moc = [self managedObjectContext];
NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"Flight" inManagedObjectContext:moc];
NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease];
[request setEntity:entityDescription];

// Set example predicate and sort orderings...
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"idNumber > %@", [NSNumber numberWithInt:0]];
[request setPredicate:predicate];

[request setFetchLimit:1];

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"idNumber" ascending:NO];
[request setSortDescriptors:[NSArray arrayWithObject:sortDescriptor]];
[sortDescriptor release];

NSError *error;
NSArray *array = [moc executeFetchRequest:request error:&error];
if (array == nil | array.count == 0)
{
    return 0;
} 
return [[[array objectAtIndex:0] valueForKey:@"idNumber"] intValue];
}

如果调用maxIdNumber方法,则将新对象添加到表中两次!? (但使用正确的idNumber)。表中的两个条目被链接 - 编辑/删除一个也编辑/删除另一个。因为这个原因,我相信它与管理对象上下文有关。对于它的价值,无论awakFromNib中调用maxIdNumber方法的次数,结果(两个副本)是相同的;即使self.idNumber只是设置为[NSNumber numberWithInt:5],maxIdNumber方法只是为一次性变量调用。

If the maxIdNumber method is called, the new object is added to the table twice!? (but with the correct idNumber). The two entries in the table are linked - editing / removing one also edits / removes the other. For this reason I believe it has something to do with the managed object context. For what its worth, the outcome (two copies) is the same no matter how many times the maxIdNumber method is called in the awakFromNib; even if self.idNumber is just set to [NSNumber numberWithInt:5] and the maxIdNumber method is just called for a throwaway variable.

任何线索

推荐答案

好的,当从awakeFromInsert方法中执行获取请求时,发生双重输入的问题。引用自 docs

Ok, the problem of double entry occurs when a fetch request is performed from within the awakeFromInsert method. Quoting from the docs:


通常不建议在awakeFromInsert的实现中执行提取操作。虽然允许,但是获取请求的执行可以触发内部Core Data通知的发送,这可能会产生不必要的副作用。例如,在Mac OS X上,NSArrayController的一个实例最终可能会将一个新对象插入其内容数组两次。

You are typically discouraged from performing fetches within an implementation of awakeFromInsert. Although it is allowed, execution of the fetch request can trigger the sending of internal Core Data notifications which may have unwanted side-effects. For example, on Mac OS X, an instance of NSArrayController may end up inserting a new object into its content array twice.

解决方法是使用perfromSelector:withObject:afterDelay方法(我只允许发布一个超链接:(): http://www.cocoabuilder.com/archive/cocoa/232606-auto-incrementing-integer-attribute-in-awakefrominsert。 html

A way to get around it is to use the perfromSelector:withObject:afterDelay method as outlined here (I am only allowed to post one hyperlink :( ):http://www.cocoabuilder.com/archive/cocoa/232606-auto-incrementing-integer-attribute-in-awakefrominsert.html.

我的工作代码现在如下:(注意,我把上面使用的大部分获取代码放入一个类别整理它一点,这允许我使用方法fetchObjectsForEntityName:withPredicate:withFetchLimit:withSortDescriptors:)

My working code is now as follows: (note, I have put the bulk of the fetching code used above into a category to tidy it up a little, this allows me to use the method fetchObjectsForEntityName:withPredicate:withFetchLimit:withSortDescriptors:)

- (void) awakeFromInsert
{
[self performSelector:@selector(setIdNumber) withObject:nil afterDelay:0];
self.date = [NSDate date];
}


-(void)setIdNumber
{
int num  = 0;

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"idNumber" ascending:NO];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"idNumber > %@", [NSNumber numberWithInt:0]];

NSArray *array = [[self managedObjectContext] fetchObjectsForEntityName:@"Flight" 
                                                          withPredicate:predicate 
                                                         withFetchLimit:0
                                                    withSortDescriptors:[NSArray arrayWithObject:sortDescriptor]];
[sortDescriptor release];   

if (array != nil & array.count != 0)
{
    num = [[[array objectAtIndex:0] valueForKey:@"idNumber"] intValue]; 
} 

num ++;

[self setIdNumber:[NSNumber numberWithInt:num]];
}

让我知道你的想法!

这篇关于Cocoa Core数据:设置默认实体属性值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆