gpt4 book ai didi

cocoa 核心数据: Setting default entity property values?

转载 作者:行者123 更新时间:2023-12-03 16:27:45 25 4
gpt4 key购买 nike

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

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

如何将“idNumber”属性默认设置为比前一个对象的 idNumber 大 1?

谢谢,奥利

编辑:我的尝试的相关代码(现已更正)

- (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 方法只是为一次性变量调用。

有什么线索吗?

最佳答案

解决了!

好吧,当从 awakeFromInsert 方法中执行提取请求时,就会出现双重输入问题。引用自 docs :

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

我的工作代码现在如下:(注意,我已将上面使用的大部分获取代码放入一个类别中以稍微整理一下,这允许我使用方法 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 核心数据: Setting default entity property values?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3729768/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com